How to deal with multiple intents in notification in Android? - android

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.

Related

Summary of notification group is not automatically dismissed when child notifications are programatically canceled

I have been struggling a lot with notifications lately.
Notifications can be easily dismissed after being clicked by using
notificationBuilder.setAutoCancel(true);
Summary notifications are required for grouping those individual notifications. Whenever child notifications are present, the summary notification is not displayed as is (titles, texts and actions are ignored, for example). When all have been individually dismissed, then the summary notification is fully displayed, with its texts and actions. This feels weird: why would we need a summary after having seen all notifications individually? Who knows... Anyway, we can get rid of this unexpected behaviour using
summaryNotificationBuilder.setAutoCancel(true);
Now, when the their action has been clicked, notifications do not automatically dismiss even though notificationBuilder.setAutoCancel(true) has been called. As explained in How to dismiss notification after action has been clicked, we need to keep track of their unique id we have provided when displaying the notification
notificationManager.notify(uniqueNotificationId, notificationBuilder.build());
From the receiving activity or broadcast receiver, we would call
notificationManager.cancel(uniqueNotificationId);
Let's be honest: this is horrible.. but it seems to be the way to go. I haven't been able to find any good alternative. Now, dismissing notifications like previously stated seems to interfere with summaryNotificationBuilder.setAutoCancel(true). When I individually dismiss all child notifications by clicking them or swiping them away, the summary notification is automatically cancelled. But when I trigger an action from any of those (thus triggering the mechanism of creating an Intent passing an extra uniqueNotificationId, and then calling NotificationManagerCompat.from(context).cancel(uniqueNotificationId) from the receiving activity or broadcast receiver, then the summary notification is not automatically canceled, even though summaryNotificationBuilder.setAutoCancel(true) was used.
Any idea on how to fix this?
I didn't find a way to programmatically figure out how many child notifications are still visible in order to also manually dismiss the summary notification, for example. Edit: Actually, now I'm trying to unconditionally dismiss the summary notification... without success.

In android, how to know if a notification is a new one or a repeated one?

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.

How does one retrieve a Notification to update it?

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.

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 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.

Don't overwrite existing Notification

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.

Categories

Resources