How to print when notification is in background in iOS

32 Views Asked by At

I'm using flutter_local_notifications package from pub.dev and i successfully implemented notification action button in iOS, but problem is if the app is in background or phone is locked/ sleep and notification receives and if i press on action button then it is not printing instead it is opening the app. What is the missing link or requirement to print on background mode, below is the sample dart code

class NotificationService {
  final FlutterLocalNotificationsPlugin notificationsPlugin =
      FlutterLocalNotificationsPlugin();
  final List<DarwinNotificationCategory> darwinNotificationCategories =
      <DarwinNotificationCategory>[
    DarwinNotificationCategory('plainCategory',
        actions: <DarwinNotificationAction>[
          DarwinNotificationAction.plain('id_3', 'SNOOZE',
              options: <DarwinNotificationActionOption>{
                DarwinNotificationActionOption.foreground,
              }),
        ],
        options: <DarwinNotificationCategoryOption>{
          DarwinNotificationCategoryOption.hiddenPreviewShowTitle
        })
  ];
  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 {},
      notificationCategories: darwinNotificationCategories,
    );

    var initializationSettings = InitializationSettings(
        android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
    await notificationsPlugin.initialize(
      initializationSettings,
      onDidReceiveNotificationResponse:
          (NotificationResponse notificationResponse) async {
        switch (notificationResponse.notificationResponseType) {
          case NotificationResponseType.selectedNotification:
            break;
          case NotificationResponseType.selectedNotificationAction:
            if (notificationResponse.actionId == "id_3") {}
            break;
        }
      },
      onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
    );
  }
  @pragma('vm:entry-point')
  static void notificationTapBackground(
      NotificationResponse notificationResponse) async {
    print("test");  // not printing in iOS Device
  }
  notificationDetails() {
    return const NotificationDetails(
        android: AndroidNotificationDetails('channelId', 'channelName',
            importance: Importance.max,
            actions: <AndroidNotificationAction>[
              AndroidNotificationAction('1', 'Snooze',
                  showsUserInterface: true),
            ],
            priority: Priority.max,
            sound: RawResourceAndroidNotificationSound('notification'),
            playSound: true),
        iOS: DarwinNotificationDetails(
            sound: 'notification.wav', categoryIdentifier: 'plainCategory'));
  }
}
0

There are 0 best solutions below