简体   繁体   中英

flutter_local_notifications onNotificationReceived method - Flutter

Too long to read: The problem

Basically, I am looking for an onMessageReceived() callback but that works with flutter_local_notifications so I can handle and show the data to the user. This plugin only supports handling the onNotificationTap() action.

  • How am I expected to handle the message when the user receives it, for example, if they have Do Not Disturb on? Even if the local notification doesn't show, I need to show an Overlay at least, triggered by some onMessageReceived() function.
  • How can I update the notificationCount in my database when a local notification is received (scheduled)?

Description

In my project I am using:

  1. Firebase Cloud Messaging (FCM)
  2. flutter_local_notifications package.

When an event is scheduled in my app, the process is the following:

  1. POST request to FCM with data only message.
  2. My app receives the message through the onMessageReceived() callback.
  3. Almost instantly I get 'Got a message whilst in the foreground!' message. This was triggered by the instant FCM data message.
  4. The data inside the message triggers flutter_local_notifications to schedule a notification.
  5. This scheduled local notification, received at a later date, cannot be handled (no OnMessage() function).

I don't schedule a notification directly on FCM because you can't do that from a post request (weird) , but that would solve all my problems.

Problem

  • When the notification gets to the user's device, there is no way of handling the message (foreground or background)
  • I cannot display an Overlay with the notification, in case of the user being in the foreground
  • I cannot automatically update the notificationCount in my Firebase Realtime Database

Basically, I am looking for an onMessageReceived() callback but that works with flutter_local_notifications so I can handle and show the data to the user. This plugin only supports handling the onNotificationTap()` action.

Example of my process

This is what FCM has that flutter_local_notifications doesn't. Triggered when a notification is received by my app:

FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
print('Got a message whilst in the foreground!');

[...]

if (scheduledDate != null) {
//using flutter_local_notifications
  sendScheduledLocalNotification(itemTitle, 'Due Today', formattedDate);
//this notification, received at a later date, cannot be processed with this same function because it doesn't use FCM
}

//only shown with instant notifications (not scheduled)
if (notification != null) {
  showOverlayNotification((context) {
    return LocalNotificationOverlay(
      title: notification.title!,
      subtitle: notification.body!,
      imageUrl: notification.imageUrl!,
    );
  }, duration: Duration(seconds: 3));
}
  }

});

Yes, as far as I know we don't have the same stream option to listen to the notifications in the system tray for flutter_local_notifications as we have for FCM.

You can check this answer , It might help you.

I guess in the end you'd better do it in the back-end, and again this scheduled message being send/received via FCM.

I don't know about the mechanism that you're using and the function that sends the notification to user, but you can ignore sending notification to user if it has information about scheduling. Based on your code, scheduledDate and formattedDate are aware of it. So in your cloud function that sends the notifications you can instead of sending a notification, doing some task or triggering something(similar to the attached answer) and this will schedule sending that notification based on its time. Then in front-end for example you can update your databases every time you get a notification from FCM, because you didn't send them in the first place and you actually scheduled sending them in the planed time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM