skip to Main Content

I’m simply adding a local notification, however in real device notification is appears but without making a sound. For certain real devices, the notification is enabled by default, but we have to manually enable it in some devices. can anyone know how should i add permission in this code

 import 'package:flutter_local_notifications/flutter_local_notifications.dart';
    import 'package:timezone/timezone.dart' as tz;
    
    class NotificationService {
      final FlutterLocalNotificationsPlugin notificationsPlugin =
          FlutterLocalNotificationsPlugin();
    
      Future<void> initNotification() async {
        AndroidInitializationSettings initializationSettingsAndroid =
            const AndroidInitializationSettings('mipmap/ic_launcher');
    
        var initializationSettingsIOS = DarwinInitializationSettings(
            requestAlertPermission: true,
            requestBadgePermission: true,
            requestSoundPermission: true,
            onDidReceiveLocalNotification:
                (int id, String? title, String? body, String? payload) async {});
    
        var initializationSettings = InitializationSettings(
            android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
        await notificationsPlugin.initialize(initializationSettings,
            onDidReceiveNotificationResponse:
                (NotificationResponse notificationResponse) async {});
      }
    
      notificationDetails() {
        return const NotificationDetails(
            android: AndroidNotificationDetails('channelId', 'channelName',
                importance: Importance.max,
                playSound: true,
                ),
            iOS: DarwinNotificationDetails());
      }
    
      Future showNotification(
          {int id = 0, String? title, String? body, String? payLoad}) async {
        return notificationsPlugin.show(
            id, title, body, await notificationDetails());
      }
    
      Future scheduleNotification(
          {int id = 0,
          String? title,
          String? body,
          String? payLoad,
          required DateTime scheduledNotificationDateTime}) async {
        return notificationsPlugin.zonedSchedule(
            id,
            title,
            body,
            tz.TZDateTime.from(
              scheduledNotificationDateTime,
              tz.local,
            ),
            await notificationDetails(),
            androidAllowWhileIdle: true,
            uiLocalNotificationDateInterpretation:
                UILocalNotificationDateInterpretation.absoluteTime);
      }
    }

2

Answers


  1. You can use permission_handler plugin to request permission on iOS, android and windows.

    For Example:

            PermissionStatus status = await Permission.notification.request();
            if (status.isGranted) {
              // notification permission is granted 
            }
            else { 
             // Open settings to enable notification permission
            }
    

    if you are using for iOS then don’t forget to add this in your pod file

    post_install do |installer|
      installer.pods_project.targets.each do |target|
        ... # Here are some configurations automatically generated by flutter
    
        # Start of the permission_handler configuration
        target.build_configurations.each do |config|
    
          # You can enable the permissions needed here. For example to enable camera
          # permission, just remove the `#` character in front so it looks like this:
          #
          # ## dart: PermissionGroup.camera
          # 'PERMISSION_CAMERA=1'
          #
          #  Preprocessor definitions can be found in: https://github.com/Baseflow/flutter-permission-handler/blob/master/permission_handler_apple/ios/Classes/PermissionHandlerEnums.h
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
            '$(inherited)',
    
            ## dart: PermissionGroup.notification
            'PERMISSION_NOTIFICATIONS=1',
          ]
    
        end 
        # End of the permission_handler configuration
      end
    end
    
    Login or Signup to reply.
  2. A more holistic answer.

    • In Android: If [androidSdkInt] <= 33 (Android Version <= 12) it is auto-granted, for Android Verion >12 handle it the same way as above answer says
    • In iOS: Same as the above answer
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search