How to stack notifications on Jelly Bean? - android

following my previous notification problems I wanted to test the new notification stack feature of Jelly Bean 4.1 as it is described here. I already found out, that notifications need to have different IDs, otherwise the older notification will just be updated with the content of the second notification. If I use different IDs, my Notifications will just be a list:
The small secondary icon is set with setSmallIcon() (see my first notification on the screenshot above), while the larger icon is set with setLargeIcon(). What I cannot control is when the number of pending notifications will be displayed. It is not controlled by the text lines mentioned in my other question.
So my guess is, that Jelly Bean will take care automatically of multiple notifications, but how can I trigger this? Maybe someone can give me a piece of code or a hint in the documentation.

You wrote the answer in your question: "otherwise the older notification will just be updated with the content of the second notification". However, your app needs to have the smarts to know what the "second notification" should show, to cover all events since the last time the user interacted with your app. Then, the "second notification" can contain information about both events, not just the second one.

You have to use the same notification id for all notifications and then just update the every notification with a new static number.

Related

How to Combine N Number of Related Android Push Notifications

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.

Android push notification - determine whether Notification.bigContentView is displayed

I'm creating a bigContentView notification layout as seen in this blog post.
For tracking purposes it is important for me to know whether the bigContentView layout was displayed or not? Is there a programmatic way to know that? It could be before I display the notification or after.
You can surely estabilish if the bigContentView is (at least) set in your notification because is API-level dependent (API 16): this means you can easly check if a session is eligible to display a big notification or not and trace your metric accordingly by simply verifying the system api level.
Regarding the current notification displaying state, the only one who knows how is really showing is the system; actually there's no way to know if it is expanded or not.
The closest I can think of at the moment to the notify event is at the GCM BroadcastReceiver's onReceive() method.
With regards to strictly bigContentView displays, this may prove to be a challenge. The documentation states that the "system UI may choose to show [bigContentView] instead of the normal content view at its discretion."

In Android how do I get the currently displayed notification for a given notification id?

My intention is to display a notification with a personalised icon. However, if a notification with the same id is already displayed and then I need to add a new notification with the same id, my program will instead prepare a "stacked" version of the notification where the icon is changed to some generic icon and the notification content shows excerpts from the last and current notifications. Similar to how Gmail does when there are multiple emails.
To implement that I need to check if there are notifications of my app, currently displayed. I do not see any API to retrieve my own notifications.
I cannot simply cache the notification details that I have displayed till now, since in that case I need to know when they will be dismissed by the user, and update my cache accordingly. I also do not see any API to listen for dismiss events.
If you observe the gmail app notification behaviour properly u will notice that even when u dismiss one notification gmail shows you the same notification again in the list when a new notification comes. It looks to me that gmail is relying on total unread/unopened messages rather than keeping a cache of notifications.
There is no direct api for ur suggested SDK version. You need to fallback to ur own implementation. However, there is a way to know if a already showed notification was dismissed - How to know when my notification is cleared via Clear button?
This is possible with android 4.3 upwards now
See http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()

Android: How to consolidate multiple notification icons in android

I am writing an application where I am pushing notifications to the client from the server. If server pushes many notification messages to the user, I see that there are multiple app notification icons in the notification bar(where there are other icons such as battery, connectivity etc) are displayed.
I want to limit it one but I still want to retain notifications in the notification drawer(in pull down pane).
I tried canceling notificationManager.cancel(int id), but it removes notification from the the notification drawer along with notification bar. Is there any extra flag or any api to get around this?
Thanks
You can update the notification content. Here are the docs to do it http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating
This is what the SMS/Whatsapp/gmail does. If there's only one message, they display it's content, otherwise they would display the amount of unread messages and or the number of people who wrote them. The point is that they only have one notification on the status bar, and they keep using the same one until the user goes in the app.
As has been pointed out, this is strictly speaking against the android design guidelines. However guidelines are not always right in every situation, and it seems to me that may be possible to do this, at least for some versions of Android.
In 4.4 and 6.0 (I didn't test other versions) some testing seems to indicate that if the notification priority is set to PRIORITY_MIN then the icon will not appear in the notification bar but it will appear in the drawer.
I do not know whether this behaviour is guaranteed though and the docs seem a bit vague. There is also an annoying consequence that the position of the notification in the drawer is affected by this, so this answer is not at all ideal.
I wish there were a better solution for people who actively choose to not follow the guidelines because of specific design requirements.

Having a maximum of one Android Notification at any point

I am currently developing an Android app that is supposed to inform the user of events that have happened. I have successfully created the code that allows for me to create the Android notifications and display them to the user etc.
The problem I am having is I should only ever have one notification. In the event that a previous notification hasn't been cleared yet, I would like to be able to access it (this is primarily to allow me to check the number property), so I can set the appropriate number property on the new notification. Unfortunately I have not been able to identify how I would go about retreiving a previous notification if it still exists. I don't particularly need anything more than the number from the notification as I will be running code that will cancel that notification prior to creating the new one.
I do have the ID for the notification that would have been raised (if any).
So in summary, how can I retrieve a previous notification, so I can get the number from it.
I am not certain why #xandy wrote a comment instead of an answer, but the approach described is correct.
Simply call notify() with your new Notification object and the same ID as before. If the Notification is still on-screen, it will be updated with your new number. If the Notification was cleared by the user, or was never there in the first place, your new Notification will be displayed. You can see that in this sample project -- just click the top button twice.
Note that Android 3.0 appears to have deprecated the number -- I cannot get it to show up on a XOOM, for example.

Categories

Resources