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:
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));
}
}
});
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.
Related
I enabled notifications in my app and its working fine but how can I expire an old notification if a new one arrives or expire it after a certain duration?
The "flutter_local_notifications" plugin has a method to dismiss notifications. However, there is currently no method to get a list of the present notifications. You need to track them on your own (e.g. some persistent storage like SharedPreferences).
Retrieving pending notification requests :
final List<PendingNotificationRequest> pendingNotificationRequests =
await flutterLocalNotificationsPlugin.pendingNotificationRequests();
cancel the notification with id value of zero
await flutterLocalNotificationsPlugin.cancel(0);
Cancelling/deleting all notifications
await flutterLocalNotificationsPlugin.cancelAll();
you can achieve this with the flutter_local_notifications.
I am trying to add the custom sound in the push notification. I have added the showNotification in the background handler on the client-side (flutter), but now when the application is in the background or terminated I receive duplicate notifications, one with default sound and the other with my custom sound. Can anyone tell me how to stop showing the default background notification?
You're getting the duplicate notification because
you're sending a notification message and it is automatically displayed by the FCM SDK and,
you're implementing a custom notification display yourself.
The solution is to send a data message instead of a notification message.
With FCM, you can send two types of messages to clients:
Notification messages, sometimes thought of as "display messages."
These are handled by the FCM SDK automatically.
Data messages, which
are handled by the client app.
Source
My app is setup to use firebase for push notification. I see the push and the data when I send a message thru firebase
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('Got a message whilst in the background!');
print('Message data: ${message.data}');
});
//trap when app is in foreground
FirebaseMessaging.onMessage.listen(
(RemoteMessage message) {
}
Now , another third party vendor(iterable) is sending push message. I see the popup but how do I get the data from the message. The firebase handler does not get triggered
Thanks
You can't get the message data from the FirebaseMessaging if you are not using Firebase.
Firebase messaging is exclusive to firebase, so you can't use it to get other third-party notifications. Your third-party notification provider will provide SDK, you need to integrate it into your app, in order to get the data from the message.
I am implementing a chat feature in my flutter app. I use FCM to send a notification when a new message comes. I propagate the FCM message to flutter local notification when the notification came while the app is in the foreground. My problem is I don't want to show the local notification when the user is actively chatting with the sender.
I have firebase messaging configuration in app.dart and I have another stateful widget to show the message details and chat. I will get fromUid from notification data. I have also open chat message UID's in the message details widget. I am trying to compare these two UIDs values from notification and message detail widget and if the UIds are the same don't notify.
I try using change notifier and RouteObserver but it's not working fine. I will love any help and recommendations.
I have implemented a similar feature in my app. This is what I did.
I am using flutter and hive for local storage.
Do this when entering chat view
Hive.box("settings").put("currentChatId", xxx)
And on popping the page, set it to null again
On receiving notifications, before showing local notification you can just use .get("currentChatId")!= null && id==notificationId to check if it should show it or not.
Attempt to receive data message from HMS Push Kit when my app is closed.
I am able to receive data message when app is foreground:
// Registering
componentDidMount(){
this.listener = HmsPushEvent.onRemoteMessageReceived(event => {
const RNRemoteMessageObj = new RNRemoteMessage(event.msg);
const msg = RNRemoteMessageObj.parseMsgAllAttribute(event.msg);
console.log("Data message received : "+msg);
}
}
// Unregistering
componentWillUnmount(){
this.listener.remove();
}
By using this method I wont able to receive the data message when app is closed since the listener had removed.
Any ideas?
Push Kit supports two types of messages: notification messages and data messages. For the sake of saving power and not disturbing users, your app will not be launched by Push Kit after being stopped, and no data messages can be delivered to your app. In this case, you can determine whether to use notification messages based on your services. The delivery of data messages depends on the resident status of your app. However, notification messages can still be delivered even if your app is not launched.
To allow users to open a specified page of your app after they tap a notification message, please kindly refer to:
https://stackoverflow.com/a/64100678/14006527
Alternatively, you can set high-priority data messages to forcibly launch your stopped app to receive and process the messages. To do so, you need to apply for special permission by referring to the related description in FAQs.