Android: How to distinguish CLEAR all events from notification bar from user action

丶灬走出姿态 提交于 2019-12-12 10:41:55

问题


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.




回答4:


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


来源:https://stackoverflow.com/questions/39206275/android-how-to-distinguish-clear-all-events-from-notification-bar-from-user-act

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!