Long story short: I’m trying to get notifications actions to work in Flutter when the app is closed (killed) in Android and iOS.
I’m developing with:
- Flutter version 3.16.9
- Dart version 3.2.6
- flutter_local_notifications package version 16.3.2
I’m using the flutter_local_notifications plugin that has a onDidReceiveBackgroundNotificationResponse
callback, but no matter how I set it, the compiler returns this error:
The backgroundHandler needs to be either a static function or a top level function to be accessible as a Flutter entry point.
My main.dart
is now set this way:
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
const String channelId = 'channel_id';
const String channelName="Channel Name";
const String darwinNotificationActionCategory = 'plainCategory';
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
@pragma('vm:entry-point')
void notificationTapBackground(NotificationResponse notificationResponse) {
print('Test entry point');
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.requestNotificationsPermission();
flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()?.requestPermissions(
alert: true,
badge: true,
sound: true
);
const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
final DarwinInitializationSettings initializationSettingsDarwin = DarwinInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
notificationCategories: [
DarwinNotificationCategory(
darwinNotificationActionCategory,
actions: <DarwinNotificationAction>[
DarwinNotificationAction.plain(
'action',
'Action!',
options: <DarwinNotificationActionOption>{
DarwinNotificationActionOption.foreground,
},
),
],
options: <DarwinNotificationCategoryOption>{
DarwinNotificationCategoryOption.hiddenPreviewShowTitle,
}
)
],
onDidReceiveLocalNotification: (int id, String? title, String? body, String? payload) async {
NotificationService.onNotificationClick(id);
}
);
final 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 can see, the notificationTapBackground
callback is a top-level function and it’s still not working. I tried also creating it as a static method but it doesn’t work.
Can anyone tell me what I’m doing wrong?
Thanks in advance!