Android: How to distinguish CLEAR all events from

2019-06-20 08:41发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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.