For a given Notification ID is it possible (using standard android packages) not to set a notification if there is an existing notification from the app ?
I have a news application notifying users on breaking news headlines, one of the requirement is not to over write a breaking news if the user has not cleared it or has not clicked to view it.
Im using a SharedPreference to set a flag to figure out if Im already showing a headline plus a delete intent that'll clear it. However the deleteIntent isn't always invoked when the user clears all notification.
Thanks in advance.
Sandeep
For anyone following this later:
There's no easy way to not to overwrite a existing Notification ID using the SDK. The way to do this is:
use a boolean flag via SharedPreference, if this flag is set then don't write the notification
reset this flag when the user "Clear"s all notification. To do this setup a deleteIntent (via a Service), make sure you clear this flag in the onStartCommand() and onStart()
optionally reset the flag when the app is launched
If your requirement is :
one of the requirement is not to over write a breaking news if the user has not cleared it or has not clicked to view it.
You can use NotificationManager for getting list of all the active notifications posted by your application using getActiveNotifications()
And check if the user has dismissed or opened the notification and take the required action.
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.
Hope it helps.
Related
In android, some apps post a notification when an event occurs (for example when your receive a message).
But until you dismiss this notification or open the app to see this event, the app will continue to post notifications about the same event.
In my app I only want to react once to a specific event.
When the NotificationListenerService notify me of a notification how can i check if it's the first one or one of the following repeated ones ?
Thanks.
You need to store the notification's specific details in local database after every notification that will notify.Then upon new notifications you need to check if that notification already exists in database if exists then don't notify it if it doesn't exits notify it and don't forget to store it to database after notifying.
Hope this logic would help you. That's how i did it in my application.
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
}
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()
So, I've an issue as such:
I've written a service that has the notification receiver and which also calls the function notify(). My problem is this, similar to twitter, on a successful update I want to notify the user that the update was posted. And, if the update wasn't then I want to notify the user that the update has failed. When the update is posted, and user clicks on the notification, he or she will be taken to one activity. If the update isn't posted, and the user clicks on the notification, then the user will be taken to another activity that might have the drafts. Notification text/image would also change depending upon if the post was done or not. As of now, I'm able to post just one single text and not able to call notify() function from anywhere else thus becoming a huge trouble in building this notification system. Any help would be nice.
Create notification ("Updating..."), keep it's tag
on success -> cancel the "updating" notification (using it's tag) and create a new notification ("success")
on failure -> cancel the "updating" notification (using it's tag) and create a new notification ("failure")
Each of these notifications should have different PendingIntents (with differnet ids) which perform a different action (e.g. open a different activity) when clicked.
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.