So, whenever someone creates a Post, I want others notified. Hence, I send out a Notification.
I know the logistics of setting the number with setNumber(int num), and how to create an InboxStyle to display multiple posts.
The problem is if a new notification comes in, it has no knowledge of whether or not another notification exists. I can't just request that Notification by its notification_id that I assigned it and then append a String with addLine(String string), nor can I update the number of posts with the setNumber method above. I'm new to creating Notifications and I've been reading about InboxStyle, using it, but every guide leaves out how to keep in sync with if that notification is still up, and how to update it. They brush that off as a trivial matter.
Yes, I understand a Notification can be updated via the NotificationManager.notify(int, notification) method, but unless you recreate what the new notification will look like, it simply overwrites your current notification.
Is there something I'm missing or not understanding about how to simply update notifications?
You should not be trying to "keep in sync with if that notification is still up". Your Notification should reflect the contents of your data model, and it should be updated when the data model changes.
For example, suppose you are writing an email client. You want a Notification for unread messages. When there are no unread messages, you have no Notification. When your background logic retrieves new messages, you update your data store to reflect those messages, and you call some heyLetTheUserKnowBoutDemEmails() method. That method queries the data store, finds out how many unseen messages there are, and calls notify() on NotificationManager to post a suitable Notification, using setNumber(), addLine(), and so forth, based on what is in the data store. Whether or not there happens to already be a Notification, for some previous heyLetTheUserKnowBoutDemEmails() call, is immaterial. When the user responds to the Notification, or otherwise opens up the inbox, your UI would then update the data store to mark the messages as seen (i.e., no longer need to be notified about) and clears the Notification. Lather, rinse, repeat.
So, it's not so much that it is a "trivial matter", but that:
It is merely a matter of replacing the existing Notification, and
The rules for when you should replace the existing Notification are almost completely dependent upon your app and its business rules, which are outside the scope of Android documentation and many blog posts, Stack Overflow answers, and the like.
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 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.
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 little app sends some notifications. We get a callback via a Pendingintent when the notification is clicked on. However, when a notification is simply removed without being clicked on, I don't get any kind of notification and thus wouldn't know if a notification has been removed by the user.
My ultimate goal is to limit the number of active notifications sent by my app to no more than 3. But I haven't been able to find a way to enumerate or simply get the count of active notifications sent by my app. The number of methods available in NotificationManager is rather limited.
Any help will be appreciated.
You can set a PendingIntent with setDeleteIntent() which will be called when the notification is removed from the notification tray (such as when the user swipes to dismiss it).
Do note that the notification design guidelines state:
If a notification of a certain type is already pending when your app tries to send a new notification of the same type, combine them into a single summary notification for the app. Do not create a new object.
A summary notification builds a summary description and allows the user to understand how many notifications of a particular kind are pending.
I.e., don't do this:
Do this (this example uses an InboxStyle notification as is recommended):
Make sure you are not posting multiple notifications of the same type.
the method "Notification.deleteIntent" you can use to set a PendingIntent which the notification was removed by system will be called .And then you can do something you want .
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.