Android: How to consolidate multiple notification icons in android - 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.

Related

Is it possible to put a foreground service notification in a notification channel with IMPORTANCE_MIN?

I am currently working on transitioning an application to Android O, and I am currently working on notification channels.
I have made different channels with different importance levels and since the application has a foreground service that has to run at all times until we transition to a new architecture (more push oriented), I thought about putting that notification in a channel that has its importance set as IMPORTANCE_MIN, so that it is there, but it doesn't bother the user, and doesn't place an icon in the status bar.
However, when I do that, and I put my application in the background (with Home or Back buttons), I get an Android System notification telling me that my app is running in the background, like so:
If I change my channel and make it use IMPORTANCE_LOW, the problem goes away, however, the notification is more prominent.
So, my question is - is it possible to do what I am trying at all? I get that the system would not allow the developers to do this, because if you have a foreground service, it should be visible to the user, but that's just a guess, and I found no documentation regarding this, and that's why I'm posting this question.
My second question is - prior to O, if you set the priority of your notification to PRIORITY_MIN, can you bind that notification to a service to make it a foreground service, or was that a no-go since always?
Edit: Confirmed that the Android System shows the notification for channels with importance IMPORTANCE_MIN (thanks, M66B), so the question that remains now is why? Does anyone know the reasoning behind this, or can find any documentation anywhere? Is this maybe a bug that should be reported to the tracker?
This behavior is now documented: https://developer.android.com/reference/android/app/NotificationManager.html#IMPORTANCE_MIN
Min notification importance: only shows in the shade, below the fold.
This should not be used with Service.startForeground since a
foreground service is supposed to be something the user cares about so
it does not make semantic sense to mark its notification as minimum
importance. If you do this as of Android version O, the system will
show a higher-priority notification about your app running in the
background.
And also here: https://material.io/guidelines/patterns/notifications.html#notifications-settings
In Android O, a channel’s default importance level for foreground
service notifications must be at least IMPORTANCE_LOW so that it shows
an icon in the status bar.
Channels using the less-prominent IMPORTANCE_MIN level will trigger an
extra notification from Android at IMPORTANCE_LOW, stating that the
app is using battery.
Sidenote:
This is a real pain for us, since prior to O we used to dynamically switch between PRIORITY_DEFAULT and PRIORITY_MIN when our foreground notification had no interesting information to present. With channels we can't change the IMPORTANCE dynamically anymore, and had to remove that feature.

Is it possible to move a notification from heads-up to the status bar?

Background
Android Lollipop (API 21) introduced a way to show notifications outside of the status bar (AKA "notifications bar"), so that the user can handle them right away. It's called "Heads-up notifications".
The trigger for showing them may vary between devices/roms/manufacturers.
The problem
Sometimes, showing such notifications can annoy users, and most of the times there are no settings for those cases.
If the user dismisses heads-up notifications, they won't show as a normal notification. There is no way to hide them and continue with what's on the screen. You can only wait (and it's quite a long time of waiting too).
In fact, there are multiple Google-Group issues that were opened about it, just because it can annoy people (link here and here).
What I've found
Starting with API 18, it is possible to listen to notifications events and even read them, by using "NotificationListenerService" and "StatusBarNotification" , and maybe other classes.
However, other than dismissing notifications (of other apps), I can't find any other action that can be done to them.
The question
Is it possible that in the lifetime of my app, I will be able to listen to notifications that are shown as heads-up, and put them back as a status-bar notifications?
Maybe even set a different timeout for them? or choose to convert them to normal status-bar notifications when they get dismissed?
Maybe before even doing those operations, I should ask: how can I know if a notification that I've found (of other apps) is showing as a heads-up notification ?
I don't know how to do implement this. But answering the "Is it possible that...?" question, yes, there are apps like this one that block/only show notifications in the notification bar.

Checking for PRESENCE of any Android Notifications

I want to be able to programmatically check if there are any notifications present (preferably only non-permanent notifications) in the status bar. This should include all notifications and not just ones created by my app. I don't need information about the specific notifications or even the total number, I just want to know IF there are notifications being displayed in the status bar currently or not.
I have researched and found that I'm likely going to have to deal with the accessibility service. That's fine, but so far I haven't found a solution to check for current notifications, all solutions so far only show how to detect NEW notifications. I want to be able to query at any instant and check for presence of notifications.
One (ugly) option would be to constantly keep track of all new notifications and clearing of notifications, but I fear this may not be reliable. I want to know if there any simpler or cleaner methods.
Any guidance will be really helpful.

Multiple notifications with actions

My app issues multiple notifications that all may have Jelly Bean style actions [1].
It seems that when I issue multiple notifications, only the last one will have action buttons and I cannot figure out why.
I use a custom intent action for the pendingIntents of every notification and action.
Does anybody know how to create two notifications at the same time that both have action buttons visible?
[1] http://developer.android.com/reference/android/app/Notification.Builder.html#addAction(int,%20java.lang.CharSequence,%20android.app.PendingIntent)
Per the Notification's documentation,
A notification's big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture
If you have multiple notifications, be sure to expand the lower ones using one of the gestures as explained on the design page for notifications - I expect that they do have the action controls but are just not expanded.
This is an interesting question. Is there a reason you need more than one notification with an action button? Although action buttons are a nice way of connecting "instantly" with an app, they're primarily for use with something that's running even though you want the UI to be minimal. The best example of this is a media player: you don't want to shut it down, but you don't want to force users to go to the app to control play/pause.

How to stack notifications on Jelly Bean?

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.

Categories

Resources