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 someonMessageReceived()
function. - How can I update the notificationCount in my database when a local notification is received (scheduled)?
Description
In my project I am using:
- Firebase Cloud Messaging (FCM)
flutter_local_notifications
package.
When an event is scheduled in my app, the process is the following:
POST
request to FCM with data only message.- My app receives the message through the
onMessageReceived()
callback. - Almost instantly I get
'Got a message whilst in the foreground!'
message. This was triggered by the instant FCM data message. - The data inside the message triggers
flutter_local_notifications
to schedule a notification. - 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));
}
}
});