0

I am trying to figure out if I can cancel (clear) an FCM notification from the notification tray on both iOS and Android.

An FCM RemoteMessage arrives with a RemoteNotificaiton class that contains an AndroidNotification class. This class only contains a tag, and not an ID. An AppleNotification does not contain a tag or an id.

The flutter_local_notifcations plugin requires an integer ID, but FCM does not provide an integer ID with its notification.

Is there anyway to cancel an FCM notification from the system tray, in flutter code only?

Scorb
  • 1,654
  • 13
  • 70
  • 144

2 Answers2

0

Unfortunately there is no way.

This is only possible using method channels.

https://stackoverflow.com/a/65565999/8912043

Taz
  • 1,737
  • 1
  • 15
  • 30
-1

You can achieve the same using flutter_local_notifications -

@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
  if (state == AppLifecycleState.resumed) {
    await flutterLocalNotificationsPlugin.cancelAll();
  }
}

If you want to clear a single FCM notification you can wrap Firebase and flutter_local_notifications -

Create a new AndroidNotificationChannel instance:

const AndroidNotificationChannel channel = AndroidNotificationChannel(
 'high_importance_channel', // id
 'High Importance Notifications', // title
 'This channel is used for important notifications.', // description
 importance: Importance.max,
);

Create the channel on the device (if a channel with an id already exists, it will be updated):

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

await flutterLocalNotificationsPlugin
 .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);

Once created, we can now update FCM to use our own channel rather than the default FCM one. To do this, open the android/app/src/main/AndroidManifest.xml file for your FlutterProject project. Add the following meta-data schema within the application component:

<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />

And then create local notification as -

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
  RemoteNotification notification = message.notification;
  AndroidNotification android = message.notification?.android;

  // If `onMessage` is triggered with a notification, construct our own
  // local notification to show to users using the created channel.
  if (notification != null && android != null) {
    flutterLocalNotificationsPlugin.show(
        notification.hashCode,
        notification.title,
        notification.body,
        NotificationDetails(
          android: AndroidNotificationDetails(
            channel.id, // Set your own Id!!
            channel.name,
            channel.description,
            icon: android?.smallIcon,
            // other properties...
          ),
        ));
  }
});

Now you have a Id for the FCM notification so you can use that Id to cancel a single FCM notification.

Reference :- Firebase

Hope it helps.

Nikulsinh Sodha
  • 381
  • 3
  • 14
  • This only clears all notifications, not a specific notification. – Scorb May 28 '22 at 14:32
  • Yea, and that is the behaviour when you open any modern app from notification or any other way its notifications are cleared... well mostly – Nikulsinh Sodha May 28 '22 at 16:18
  • I added a work around. You have to add some stuff but it may just work as you want just that all your FCM will be wrapped in local notifications. – Nikulsinh Sodha May 28 '22 at 16:58
  • This will not work. You have not implemented onBackground message, and in that case you would end up showing two notifications in the system tray. – Scorb May 29 '22 at 13:46
  • It's from the official docs and I guess cause the FCM is wrapped inside the local it won't have two notifications in background.And if it does maybe you could have implemented that. – Nikulsinh Sodha May 30 '22 at 03:33