Can I detect in my service when the user has pressed the "delete" button in notification bar to clean icon notification? It is a notification my service previously put there.
Use deleteIntent on the Notification to specify a PendingIntent to be invoked when the user clears it.
Related
According to the specification, .setDeleteIntent(pendingIntent) is associated to both actions (CLEAR all events from notification bar and user action like swiping).
My requirements are that when the user touches the notification that appears on the notification bar, he must be forwarded to the NotificationsList.class. This is done with my pendingInent:
PendingIntent sendPendingIntent = PendingIntent.getActivity(context, reminderId, new Intent(context, NotificationsList.class), PendingIntent.FLAG_UPDATE_CURRENT);
However, on clicking the CLEAR button, the user must not be navigated to the application at all. With the .setDeleteIntent(pendingIndent) I cannot fulfill the 2nd requirement. The user is still navigated to NotificationsList.class.
Is there a way to programmatically distinguish the CLEAR all notifications events fired from the CLEAR button from user actions like touch or swipe on the specific notification on the notification bar?
What you're describing is very obtuse behavior. You need only set the pending intent to your notification and when it is clicked, the intent that is backing it will be executed.
If your code is navigating the user back to the app when the notification is cleared, then you already have a problem with your design. If the user clears your notification you should NOT be trying to navigate them back. Hence the setDeleteIntent() should NOT be associated with starting any activity.
Note that the intent that is backed when you click the notification (setContentIntent()) and clear (setDeleteIntent()) the notification are basically two PendingIntents, they should not be the same, which is what your problem is describing.
You cannot distinguish the two events. As the documentation says:
Notifications remain visible until one of the following happens:
The user dismisses the notification either individually or by using "Clear All" (if the notification can be cleared).
The user clicks the notification, and you called setAutoCancel() when you created the notification.
You call cancel() for a specific notification ID. This method also deletes ongoing notifications.
You call cancelAll(), which removes all of the notifications you previously issued.
So there are basically three different events in the view of a programmer:
You dismisses the notification
The user clicks on the notification
The user dismisses the notification (either by swiping or clearing it)
The first event is fired by yourself by calling cancelAll() or cancel().
You can handle the second like (which you wanna do I think):
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
//....
.setContentIntent(sendPendingIntent);
And you can handle the third event like (as you have described above):
builder.setDeleteIntent(pendingIndent)
I don't recommend to start an activity after the user dismisses your notification, because the user won't expect it and it will be a bad user experience.
I hope I could help.
According to the design guidelines, the user can expect to interact with your notification using higher-level gestures like click, swipe, and pinch zoom. Responding instantly to a lower level event like touch would short circuit these gestures, so your requirements would violate the design guidelines and you should not implement it.
If the requirements are changed so that the user is forwarded when they click on the notification, there is no need to distinguish between swiping and clearing, which is impossible in any case.
So your issue should be resolved by changing one word in the requirements: touch --> click.
I googled deleteIntent to find some info for a problem which led me here.
English is my second language. Sorry for some misuse of words in advance. I'm an android newbie, just downvote the answer if it sucks :)
For your last question, just as #x-code and #bendaf said, it's impossible to
distinguish swiping and clearing.
I am following the codelabs about notifications and encountered the same question(The description in your title). So I decided to offer more detail about how to use .setDeleteIntent in your case. Maybe you had done that.
In your case, the wrapped intent is for starting an activity, so do the pendingIntent.
PendingIntent sendPendingIntent = PendingIntent.getActivity(context, reminderId, new Intent(context, NotificationsList.class), PendingIntent.FLAG_UPDATE_CURRENT);
But for performing a broadcast, e.g. doing some stuff when the notification is cleared, use:
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, NOTIFICATION_ID, new Intent(yourCustomActionString), PendingIntent.FLAG_UPDATE_CURRENT);
builder.setDeleteIntent(pendingIntent); // the pendingIntent will be sent when the notification is cleared
Then we need a custom broadcast receiver receive that custom action contained in the Intent object, in your case, this action relates to the clearing:
// Inside onCreate, register the broadcast receiver;
registerReceiver(new MyReceiver(), new IntentFilter(yourCustomActionString));
.
.
// Create an inner class
public class MyReceiver extends BroadcastReceiver {
public NotificationReceiver() {}
#Override
public void onReceive(Context context, Intent intent) {
// code inside will be executed when pendingIntent is sent
Log("taG", "Notification is cleared"); // a message will be logged if the notification is cleared
// for more than one action, using switch...case to decide
}
}
I'm implementing GCM in my app and keeping a hash of notifications to keep track of what is in the notification shade (I have to change intents based on if the user is in or out of app).
I set the deleteIntent PendingIntent for all my notifications. All this does is remove the Notification from my local hash so it won't be updated anymore. The intent is fired fine if I clear all or swipe to delete a notification. However, I also set my notifications to auto cancel. Clicking on a notification does not trigger the deleteIntent for my notification.
My question is, is there any way to be notified when my Notifications are auto-cancelled?
This bug has been reported, but it doesn't look like it has been investigated at all. To work around this here's what I did:
Turn off auto cancel
Use broadcast for both content and delete intents with different actions
Broadcast receiver checks action
Content action: Do both click and delete operations, and cancel notification manually
Delete action: Do delete operation only
For example:
Send Notification
Notification.Builder builder = new Notification.Builder(context)
// Set other properties (not auto-cancel)
.setContentIntent(PendingIntent.getBroadcast(context, 0, new Intent(NOTIFICATION_CLICKED_ACTION), 0))
.setDeleteIntent(PendingIntent.getBroadcast(context, 0, new Intent(NOTIFICATION_DELETED_ACTION), 0));
notificationManager.notify(NOTIFICATION_ID, builder.build());
Receive Broadcast
if (intent.getAction().equals(NOTIFICATION_CLICKED_ACTION)) {
startActivity(new Intent(context, MyActivity.class));
notificationManager.cancel(NOTIFICATION_ID);
}
// Do deletion behaviour here (for both click and delete actions)
This is the correct behaviour od the DeleteIntent as described here in the Android SDK documentation:
Supply a PendingIntent to send when the notification is cleared
explicitly by the user.
The DeleteIntent will only get called when the notification is explicitly cleared by the user by swiping it away or by using the "clear all" function of the notification menu. Tapping on the notification will ONLY trigger the ContentIntent EVEN IF the AutoCancel is set to True.
Documentation says here and here, that clicking on notification with FLAG_AUTO_CANCEL cancels it automatically. This behavior means also that regular contentIntent (if set) will fire along with automatic cancellation, because it is fired in response for user's click action.
Use contentIntent field along with deleteIntent to detect cancellation performed by explicit user tap.
I am currently making an application that uses Notifications.
I was able to display the notifications, and remove them from the notification list when the user taps them. However, I would also like my notifications to disappear if the user sees them but does not act on them.
For instance, the user displays the notification list, then taps on a notification that is not mine, or just closes the notification list. During those cases, I am trying to make my notifications get canceled and not displayed the next time the user displays the notification list even if he did not do anything to my notification.
Is this possible?
Thanks! :D
(edit: if you are wondering why I thought of this, a very simplified explanation would be: think of the notification as a toast instead; a toast that has a longer existence, and makes sure that the user actually saw it before disappearing.)
you may remove a notification by providing its id to the cancel method:
((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel(your_notification);
however, i wouldn't recommend you to do it as it may confuse the user
It is very easy, there is a cancel method for notifications:
public void cancel (int id)
Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar.
public void cancel (String tag, int id)
Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar.
public void cancelAll ()
Cancel all previously shown notifications. See cancel(int) for the detailed behavior.
Details are here:
http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)
Update
If you want to cancel your notification when user sees that notification, there is a flag for that, FLAG_AUTO_CANCEL:
Notification notification1 = new Notification(R.drawable.icon, "test",
System.currentTimeMillis());
notification1.flags |= Notification.FLAG_AUTO_CANCEL;
I have up to 4 separate alarms. When an alarm goes off, I display a status bar notification. If I have 2 alarms go off at the same time, I want to have only 1 status bar icon. If I cancel one of the two alarms, I still want to see that single status bar icon (since there is still an alarm going off, even though I cancelled the first alarm). When I cancel the last alarm on the screen I want to remove the status bar icon.
Is there a built in way to do this or do I have to keep track of what alarms are on the screen and only dismiss the notification if it's the last alarm?
Thanks for your help.
What I understood that you want only latest notification to be displayed and if you cancel your last alarm all should go. Here is my solution:
Call the same notification function every time you set notification as it will replace the previous one.
Pass an argument to notification function in case you want to cancel the notification.
If a notification is alredy there and now you want to cancel the next alarm and you want the previous notification should be there, then you should maintain a variable in sharedpreference which will tell you not to cancel the previous notification.You should check this before calling notification function
When you cancel the last alarm then you can pass the argument to notification function as you cancel any other alarm.
I was able to send a notification to the Android emulator. When I click on it, the activity opens. But the notification alone remains in status bar. Normally, when you get sms/notifications and click on them, you go to the particular activity and when you expand the status bar, you don't see the notifications, on which you have already clicked.
So, my question is - how can I make the notification dissapear after clicking on it? Is there a special function for this? Thanks.
Add Notification.FLAG_AUTO_CANCEL to the notification when you create it.
To clear the status bar notification when the user selects it from the Notifications window, add the "FLAG_AUTO_CANCEL" flag to your Notification object. You can also clear it manually with cancel(int), passing it the notification ID, or clear all your Notifications with cancelAll().
Reference - http://developer.android.com/guide/topics/ui/notifiers/notifications.html