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.
Related
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.
The app is in the foreground. I am showing a local notification to the user. After showing up, I don't want the user to see this notification in the system tray. Can this package or other allow me to send notification without entering to the system tray, or delete this notification just after showing up.
The flutter_local_notifications plugin provides a method to cancel all notifications or cancel a single notification. You have to call them in the appropriate place for it to work
await flutterLocalNotificationsPlugin.cancelAll(); \\ Removes all Notifications
await flutterLocalNotificationsPlugin.cancel(1); \\ Removes notification with id = 1
If this does not work, you can also try this plugin
https://pub.dev/packages/eraser
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 have an app with a simple messenger component to it. When user1 sends a message to user2 I am using Azure Notification Hubs to send a push notification to user2. Since I am using Notification Hubs to register the user's devices for push notifications, I don't know which phone OS's they have registered with, so just queue a notification for each type I support:
NotificationOutcome outcome1 = await hub.SendAppleNativeNotificationAsync(jsoniOSPayload, tags);
NotificationOutcome outcome2 = await hub.SendGcmNativeNotificationAsync(jsonAndroidPayload, tags);
...(etc.)
However, we need to handle the case where an app has been deleted. When this happens, we need to send the user an email if a push notification could not be sent to any of their devices.
My question is: how can I tell if at least one notification was successfully delivered to a users device? I know about the NotificationHubClient.EnableTestSend property, which does cause the NotificationOutcome object to have a success count. This would work perfectly, but the documentation indicates this would not be optimal in production:
"When test send is enabled, the following occurs: All notifications
only reach up to 10 devices for each send call.The Send* methods
return a list of the outcomes for all those notification deliveries.
The possible outcomes are the same as displayed in telemetry. Outcomes
includes things like authentication errors, throttling errors,
successful deliveries, and so on.This mode is for test purposes only,
not for production, and is throttled."
Any suggestions would be appreciated!
how can I tell if at least one notification was successfully delivered to a users device?
As you mentioned that NotificationHubClient.EnableTestSend is used for debugging and limited to 10 devices.
If we want to get the count of successfully delivered, we can use function NotificationHubClient.GetNotificationOutcomeDetailsAsync(string notificationId), more details please refer to document.
Demo code:
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString("Endpoint=sb://notificationnamespace.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=xxxxxxx", "NotificationHub Name");
string message = "{\"title\":\"((Notification title))\",\"description\":\"Hello from Azure\"}";
var result = await hub.SendGcmNativeNotificationAsync(message); //GCM for example.
var notificationDetails = await hub.GetNotificationOutcomeDetailsAsync(result.NotificationId);
return notificationDetails;
Note: It is just for standard pricing tier.
Is there anyway to filter push notifications that im send to my app?
I mean, if my user logged out (fb connect) from my app and for somereason my server is miss sync with the loggin-out, i will send to the user notifications while he logged out, which is unwanted situation..
Thanks.
You need to do your filtering server-side. There is nothing your app can do on a device to filter its incoming notifications. Your app may not be running when the notification occurs, and may not be launched if the user disregards the notification.
You can set BOOL variable to false on log-out. so whenever Notification arrive, you check BOOL value, if false don't process Notification and if true do whatever you want to do.
this is just an example case, you can use same logic anywhere else to process Notifications.
You could use unregisterForRemoteNotifications method.