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 acces开发者_Go百科s 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.
精彩评论