iOS Development

android – Flutter: how you can deal with notifications actions with app in background

Spread the love


Lengthy story brief: I am attempting to get notifications actions to work in Flutter when the app is closed (killed) in Android and iOS.

I am growing with:

  • Flutter model 3.16.9
  • Dart model 3.2.6
  • flutter_local_notifications bundle model 16.3.2

I am utilizing the flutter_local_notifications plugin that has a onDidReceiveBackgroundNotificationResponse callback, however irrespective of how I set it, the compiler returns this error:

The backgroundHandler must be both a static perform or a prime degree perform to be accessible as a Flutter entry level.

My foremost.dart is now set this fashion:

import 'bundle:flutter_local_notifications/flutter_local_notifications.dart';

const String channelId = 'channel_id';
const String channelName="Channel Identify";
const String darwinNotificationActionCategory = 'plainCategory';
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();


@pragma('vm:entry-point')
void notificationTapBackground(NotificationResponse notificationResponse) {
  print('Check entry level');
}

Future<void> foremost() async {
  WidgetsFlutterBinding.ensureInitialized();
  flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.requestNotificationsPermission();
  flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()?.requestPermissions(
      alert: true,
      badge: true,
      sound: true
  );

  const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
  ultimate DarwinInitializationSettings initializationSettingsDarwin = DarwinInitializationSettings(
      requestAlertPermission: true,
      requestBadgePermission: true,
      requestSoundPermission: true,
      notificationCategories: [
        DarwinNotificationCategory(
            darwinNotificationActionCategory,
            actions: <DarwinNotificationAction>[
              DarwinNotificationAction.plain(
                'action',
                'Action!',
                options: <DarwinNotificationActionOption>{
                  DarwinNotificationActionOption.foreground,
                },
              ),
            ],
            choices: <DarwinNotificationCategoryOption>{
              DarwinNotificationCategoryOption.hiddenPreviewShowTitle,
            }
        )
      ],
      onDidReceiveLocalNotification: (int id, String? title, String? physique, String? payload) async {
        NotificationService.onNotificationClick(id);
      }
  );

  ultimate InitializationSettings initializationSettings = InitializationSettings(
      android: initializationSettingsAndroid,
      iOS: initializationSettingsDarwin
  );

  await flutterLocalNotificationsPlugin.initialize(
      initializationSettings,
      onDidReceiveNotificationResponse: (NotificationResponse notificationResponse) => NotificationService.onNotificationClick(notificationResponse.actionId ?? ''),
      onDidReceiveBackgroundNotificationResponse: (NotificationResponse notificationResponse) => notificationTapBackground(notificationResponse)
  );

  [...]
}

As you’ll be able to see, the notificationTapBackground callback is a top-level perform and it is nonetheless not working. I attempted additionally creating it as a static methodology but it surely would not work.

Can anybody inform me what I am doing mistaken?

Thanks prematurely!

Leave a Reply

Your email address will not be published. Required fields are marked *