Android: Right Handling with multiple Notifications - android

I receive Push-Notifications from GCM and want them to be shown in the Notification-Bar. So I generation the Notifications in the GCMIntentService-class.
If there is already a notification in the notification-area, this notification have to be appended with the new message (with use of the BigTextStyle). But how do I know if there is a notification from my app in the notification-area?

You cannot get existing notification, but you can update existing notification by setting up Notification ID.
NotificationManager.notify(NotificationId, NotificationObject);

No, you can't find out if there's already a notification posted; this is something you should keep track of in your app.
Fortunately, however, the API for updating an existing notification is identical to the API for creating a new one: notify(). That is, once you get new information, add it to some internal buffer (possibly just a StringBuilder) representing the complete set of received push notifications, then build a new notification with Notification.Builder and call notify() with the same ID and tag you used last time. Any existing notification with that ID and tag will be replaced with the new content.

Related

Android: Right place to create notification channel

I know how to create notification and notification channel in an Android app.
I showed many examples that say create notification channel while you generate a notification from FCM listener. So when the app receives notification at that point it generates channel.
But I saw in many apps, it create all channels without receiving any notification.
Question: From where we should create a notification channel?
From the docs:
Creating an existing notification channel with its original values
performs no operation, so it's safe to call this code when starting an
app.

Can FCM notification on Android overwrite previous one?

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,
..
}

How to know if an notification has been cancelled?

My little app sends some notifications. We get a callback via a Pendingintent when the notification is clicked on. However, when a notification is simply removed without being clicked on, I don't get any kind of notification and thus wouldn't know if a notification has been removed by the user.
My ultimate goal is to limit the number of active notifications sent by my app to no more than 3. But I haven't been able to find a way to enumerate or simply get the count of active notifications sent by my app. The number of methods available in NotificationManager is rather limited.
Any help will be appreciated.
You can set a PendingIntent with setDeleteIntent() which will be called when the notification is removed from the notification tray (such as when the user swipes to dismiss it).
Do note that the notification design guidelines state:
If a notification of a certain type is already pending when your app tries to send a new notification of the same type, combine them into a single summary notification for the app. Do not create a new object.
A summary notification builds a summary description and allows the user to understand how many notifications of a particular kind are pending.
I.e., don't do this:
Do this (this example uses an InboxStyle notification as is recommended):
Make sure you are not posting multiple notifications of the same type.
the method "Notification.deleteIntent" you can use to set a PendingIntent which the notification was removed by system will be called .And then you can do something you want .

Notifications sample only showing the last received on Android

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.

Send data to Notification each X second?

Is there a way to first create a Notification and set some default message (the line below the title of a notification) and then to change that message every X seconds?
I tried finding solution, but those I found proposed constant killing and creating Notification which I do not find a proper way to achieve what I need.
I need to implement a small counter inside the same notification which will be updated every X seconds.
PS. Please do not confuse this with sending data from notification to an Activity. I need the reverse process - send data from activity to the notification.
Please have a look at android documentation
Updating notifications
To set up a notification so it can be updated, issue it with a
notification ID by calling NotificationManager.notify(ID,
notification). To update this notification once you've issued it,
update or create a NotificationCompat.Builder object, build a
Notification object from it, and issue the Notification with the same
ID you used previously. If the previous notification is still visible,
the system updates it from the contents of the Notification object. If
the previous notification has been dismissed, a new notification is
created instead.
The following snippet demonstrates a notification that is updated to
reflect the number of events that have occurred. It stacks the
notification, showing a summary:
Probably setting a notification id and accessing the same notification by id is the solution to your problem. Here is the link to document.
Edit:
Probably I missed one aspect of your question - i.e. creating new instance of notification. To resolve it you can declare an instance of notification in a singleton class. This way, you will not have to create an instance of notification again and again. Just modify the content of notification and use the same instance again and again.

Categories

Resources