I'm using FCM to send notifications to Android devices. When the app is in background, if I send 10 notifications, the devices will show up 10 entries on the notification bar.
I want FCM to make only one entry on notification bar, i.e. the newer one will overwrite old ones. I don't find a key to set this at https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream.
Is there a way to do, or it is impossible? Thanks.
To achieve this,
in your notification payload, use the tag key
{
"notification" : {
"title" : "Notification Title",
"body" : "Notification Body",
"tag" : "your_unique_tag"
}
}
Cheers.
It's possible. Two approaches.
First is you make use of the collapse_key parameter to set the message as a collapsible message. Referring to the FCM docs:
A collapsible message is a message that may be replaced by a new message containing the same collapse key if it has yet to be delivered to the device.
It's actually included in the link you provided (first parameter under Options):
collapse_key - This parameter identifies a group of messages (e.g., with collapse_key: "Updates Available") that can be collapsed, so that only the last message gets sent when delivery can be resumed. This is intended to avoid sending too many of the same messages when the device comes back online or becomes active.
Note that there is no guarantee of the order in which messages get sent.
Note: A maximum of 4 different collapse keys is allowed at any given time. This means a FCM connection server can simultaneously store 4 different send-to-sync messages per client app. If you exceed this number, there is no guarantee which 4 collapse keys the FCM connection server will keep.
Second approach is Notification bundling/stacking/grouping. As per my answer here:
By grouping the notification, I'm presuming you mean stacking or bundling notifications.
This is more on how you handle the notification in your client app. You simply have to make use of the setGroup() to add all your notifications to a single group then calling notify() to let the NotificationManager of the changes.
This Add Each Notification to a Group documentation pretty much sums it all up.
Update:
From one of the linked posts, using the tag parameter is also an option:
Identifier used to replace existing notifications in the notification drawer.
If not specified, each request creates a new notification.
If specified and a notification with the same tag is already being shown, the new notification replaces the existing one in the notification drawer.
Use same id for the notification that will overwrite/update previous ones
notification:{
...
id:125,
..
}
Related
I am using firebase for notification. I want to send notification to the group of users... If any one of them press accept notification action button then, the same notification must be remove from the other users device. Is it possible to do using firebase.
Ex. I want to send notification to 5 delivery boys for collecting parcel from XYZ place, if any of them accept the collecting parcel order notification then the same notification must be remove from other 4 delivery boys autometically.
I study and practically done an example of https://firebase.google.com/docs/cloud-messaging/android/device-group Notification send to all 5 del. boys but notification still there after accepting the collecting order.
Please, help.
Firebase Cloud Messaging has two distinct types of messages:
Notification messages that are handled and displayed by the OS when the app is not active, and handled by your application code when the app is active.
Data messages that are always handled by your application code, which can then choose to display or update UI notifications as needed.
What you're describing sounds like you want to use data messages only, and then use Android's notification UI/API to display, update, or hide a notification.
We want to send push notifications in Android and iOS to a group of users that match a certain condition. We can check if an user match this condition calling an API.
Our problem is that the users information changes very quickly and we don't have any way to keep track of this in our push notifications engine (OneSignal).
The only alternative we can think is to send the push notification to all users and client side call the API to get the latest user information. Depending on this we could choose to show the notification or ignore it.
Is this possible on Android and iOS?
It is 100% possible for sure. in the receiver class you call an intentservice, which can hit an api and get the latest information, based on the information you create notification using notification builder, and avoid if you dont want.
Yes, with the help of NotificationExtenderService this is possible.
Receive a notification in the background. Notification data is wrapped up in OSNotificationReceivedResult object. Now, read the notification id/title which should be unique for every notification.
Send this unique notification title/id along with user id to an API, which will return whether to show the notification to the user or not. If true then read notification title and message/description from OSNotificationReceivedResult and make a notification via NotificationCompat otherwise just ignore it.
Reference
I have been reading many posts/articles/tutorials I can find about updating an active notification. I fear I may have a fundamental misunderstanding about how one may update an Android push notification.
So far: I can update an active notification based on its ID, and I haven't been able to get any results out of Builder.setGroup()
My problem: When I update an active (not dismissed) notification, I want to be able to get the text from the previous, active notification, parse and add the new notification text and update it.
I'm starting to think that this might not be possible without a local DB and that my approach to this problem is no good.
Is there a way to get the content of the last notification (or one with a specific ID)?
EDIT: It seems to me that many of the examples I have seen are grouping a number of notifications all at once rather than successively.
Example
This is an example of what I want to do. The scenario I'm imagining is like this:
The owner of the device gets a notification that he/she has a new message from 'Alex Faaborg'. (See image)
The notification is not dismissed by the device owner
Another notification regarding a new message from 'Jeff Chang' comes in
Get 'Alex Faarborg's' name from the first notification, and 'Jeff Chang' from the second
Parse the info and display a summary of their notifications rather than have multiple notifications build up
4 is what I'd like to do
OK, so I realized I could do what I need to do to combine notifications by keeping track of users' unread messages(or invites etc..) on the backend, then if there's more than one, it will send out a summary of the notifications rather than each individually. On the client, this will overwrite any previous, related notifications (kept track with a JSON field sent to GCM (and subsequently, the client) that represents the 'topic' of the notification)
For whatever reason, I thought I should handle the grouping/summarizing of notifications on the client. I think the API is the way to go.
Okay, so I know at first glance it seems like I'm stupid because I can just set the "click_action" parameter in the notification JSON from the server and have it open the activity with the matching intent filter. However, I don't want to do this, because as the documentation goes on to say "If set, [click_action] corresponds to category in APNS payload," and setting it makes the notifications not be delivered on my iOS app (because there is no corresponding category on the iOS app nor do I want to make one as that requires, I believe, adding in notification actions which I don't want to get involved in yet).
On iOS tapping the notification just opens the equivalent of the Android "android.intent.category.LAUNCHER" action. Bottom line, is there any way to replicate this in Android (or solve the iOS not displaying notifications if the "click_action" is set)?
In GCM intent class, on Message receive method just parse all the content and then simply start activity by putting inside intent, which activity you want to open on msg received.
I'd suggest using different payloads for Android and iOS clients. This would require your clients to identify their platform when they submit their InstanceID tokens, but once you know what platform the client is you can formulate the message more specific to the target platform.
I'm running the Worklight's Notifications sample. I invoke the adapter to send a notification to the application, and it shows the notification into the top bar perfectly.
But when I send another notification, it don't stack with the previous one, it overwrites with the new one.
And I don't want that it being overwriten. How can I solve this?
This is a limitation that is currently imposed (by default) by Worklight.
In essence, in Worklight there is internally a GCMIntentService class that listens for received notifications and if received, creates and sends to the OS an object with a static ID. This is repeated for every incoming notification, replacing the previously received notification.
What you can do is:
Submit a feature request to be evaluated by Worklight's product designers: http://www.ibm.com/developerworks/rfe/
In your project, there is an empty class in android\native\src\com\app-name\GCMIntentService.java. What you could try to do, is basically implement your own "push mechanism" based on Google's documentation to have all notifications display rather than just the last received notification by not using a static id like how it is currently implemented in Worklight.
You need to set a diferent notification id in the client. In the notification manager create a notification id using the timestamp for exemple.