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."
Related
For implementing notifications on Android TV below points needs clarification
As I have gone through SO and other articles, Android TV doesn't have notification tray,
Hence Notification has to be handled in a custom way. Therefore, please clarify:
Can we use Notification Manager.
Proper method to implementation Android TV notification.
Can we set the Notification priority
Possible way to find out whether the user has seen/interacted with notification or not
Handling list of notification messages by Local Database or any other method.
Any help would be much appreciated.
Notifications on Android TV OS are significantly different than mobile Android. There is an area to display notifications within the launcher, but it is limited to system-level notifications that are important for the user (e.g., issues with your account or info about OS updates). General app notifications do not show up, which means you need to display any kind of notification within your own app UI and not with the regular NotificationManager and related APIs.
Can we use Notification Manager. / Can we set the Notification priority
Yes, but it won't result in a notification being visible to the user and shouldn't be done on Android TV OS.
Proper method to implementation Android TV notification.
This should be handled within app UI. Most apps have a reserved space to show these on the main screen so that users see them as soon as they open the app.
Possible way to find out whether the user has seen/interacted with notification or not
Since you'll have to display it in your own UI, you can use regular View methods. For example, if you want to know if the user clicked the message, you can add that code to the OnClickListener.
Handling list of notification messages by Local Database or any other method.
This is a bit vague to give a specific answer, so you may want to post a separate question with more details about what you're trying to accomplish. One general way to go about it is that you have a server endpoint that understands the state of notifications for a given user and you sync that with your local database (easiest is probably Sqlite using Room). Your UI needs to be told if there's a relevant notification to display within the app, but the details of that depend on your app architecture.
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.
I am working towards managing the notifications that my app creates in the Android device. Is there any method that can provide me the number of notifications from my app that are active (i.e. still visible in the notification drawer) at any given time?
You can use NotificationManager for getting list of all the active notifications posted by your application using getActiveNotifications()
As per the reference doc for getActiveNotifications():
Recover a list of active notifications: ones that have been posted by the calling app that have not yet been dismissed by the user or cancelled by the app.
to expand on commonswares comment:
I see two approaches here:
1) manage this number via a count sharedpreferences or a database, etc. you will need to supply a deleteIntent which starts something to update this number when they dismiss the notification and a contentIntent for when they open the notification (this will also update your count).
2) read this number explicitly from a notification listener service.
https://developer.android.com/reference/android/service/notification/NotificationListenerService.html
#1 is much preferred because the user doesn't have to opt in to the behavior and asking for their full notification list is entirely unnecessary.
fun getNotificationCount(): Int {
return notificationManager.activeNotifications.size
}
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.
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.