I'm trying to add action for my notification displayed by flutter local notification. However I've added the action and the action button is also displayed on both android and ios but there is no event getting triggered on tap of that action button.
Complete code is like below
``//Initialisation :- static const initializationSettingsAndroid = AndroidInitializationSettings('@drawable/notification');
static DarwinInitializationSettings initializationSettingsDarwin =
DarwinInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
notificationCategories: [
DarwinNotificationCategory(
'',
actions: <DarwinNotificationAction>[
DarwinNotificationAction.plain('some_action', 'Some Action'),
],
options: <DarwinNotificationCategoryOption>{
DarwinNotificationCategoryOption.customDismissAction,
},
),
]);
final initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsDarwin);
flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onDidReceiveNotificationResponse:
(NotificationResponse notificationResponse) {
log('Notification onDidReceiveNotificationResponse');
},
onDidReceiveBackgroundNotificationResponse: (NotificationResponse notificationResponse) {
log('Notification onDidReceiveBackgroundNotificationResponse');
}
);
final AndroidFlutterLocalNotificationsPlugin? androidImplementation =
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>();
final bool? granted = await androidImplementation?.requestPermission();
log('granted app $granted');
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannelGroup(notificationHandler.channelGroup);
await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);`
// Add "Some action" action
`final someAction = AndroidNotificationAction(
'some_action', // Unique identifier for the action
'some action', // Action label
);
final androidNotificationDetails = AndroidNotificationDetails(
channelId, // Use fixed channelId for grouping
'Grouped Notifications', // Use a fixed channel name for grouping notifications
channelDescription: 'Notification description',
importance: Importance.max,
priority: Priority.high,
icon: '@drawable/notification',
styleInformation: bigTextStyleInformation,
visibility: NotificationVisibility.public,
subText: ''subtext,
groupKey:
'groupKey', // Use the groupKey for grouping notifications
setAsGroupSummary: false, // Not setting this as the summary notification
actions: [someAction],
groupAlertBehavior: GroupAlertBehavior.summary,
largeIcon: DrawableResourceAndroidBitmap(androidIcon),
);
`
`final iOSNotificationDetails = DarwinNotificationDetails(
presentAlert: true,
presentBadge: true,
presentSound: true,
subtitle: 'uniqueSubtitle',
threadIdentifier: 'uniqueThreadIdentifier',
interruptionLevel: InterruptionLevel.active,
attachments: [attachment], //
);
final notificationDetails = NotificationDetails(
android: androidNotificationDetails,
iOS: iOSNotificationDetails,
);`
// Show individual notifications
`await flutterLocalNotificationsPlugin.show(
message.data.hashCode,
title,
subtitle,
notificationDetails,
payload: message.data.toString(),
);`
// Show the group summary notification
`await flutterLocalNotificationsPlugin.show(
0, // Use a unique ID for the group summary notification
'', // Title of the group summary notification (leave it empty)
'', // Content of the group summary notification (leave it empty)
NotificationDetails(
android: AndroidNotificationDetails(
channelId, // Use the same channelId as individual notifications
'Grouped Notifications',
channelDescription: 'Notifications',
icon: '@drawable/notification',
importance: Importance.max,
priority: Priority.high,
subText: subtext,
setAsGroupSummary:
true, // Set this notification as the summary notification
groupKey:
groupKey, // Use the groupKey for grouping notifications
groupAlertBehavior: GroupAlertBehavior
.summary, // Show all notification content in the group summary
tag: uniqueTag, // Set a unique tag for each group of notifications
largeIcon: DrawableResourceAndroidBitmap(androidIcon),
),
iOS: iOSNotificationDetails,
),
payload: '', // Empty payload or customize as per your requirement
);`
// Expecting call back event
- where ideally call backs will go on tap of the action
- I'm expecting in "onDidReceiveNotificationResponse" and "onDidReceiveBackgroundNotificationResponse" , but these are not getting called on tap of the action.
Please guide.
Do this, edit the initialization method:
Note that when setting the notification action for Android don't forget to add the
showsUserInterface
property and set it totrue
like this:This will make the response callback to detect and return the action key set above.
For more insight, you can check the documentation please: https://pub.dev/packages/flutter_local_notifications#-usage
Remember this function runs (except Linux) in a separate isolate! This function also requires the
@pragma('vm:entry-point')
annotation to ensure that tree-shaking doesn't remove the code since it would be invoked on the native side.