如何检测通知是否已被取消?

28

在Android中有没有办法检测用户向左滑动并删除通知?我正在使用alarmmanager设置重复提醒,当用户取消通知时,我需要停止我的重复提醒。这是我的代码:

设置重复提醒:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), repeatFrequency, displayIntent);

我的通知代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Get the notification ID.
    int notifID = getIntent().getExtras().getInt("Reminder_Primary_Key");

    //Get the name of the reminder.
    String reminderName = getIntent().getExtras().getString("Reminder_Name");

    //PendingIntent stores the Activity that should be launched when the user taps the notification.
    Intent i = new Intent(this, ViewLocalRemindersDetail.class);
    i.putExtra("NotifID", notifID);
    i.putExtra("notification_tap", true);

    //Add FLAG_ACTIVITY_NEW_TASK to stop the intent from being launched when the notification is triggered.
    PendingIntent displayIntent = PendingIntent.getActivity(this, notifID, i, Intent.FLAG_ACTIVITY_NEW_TASK);

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notif = new Notification(R.drawable.flag_red_large, reminderName, System.currentTimeMillis());

    CharSequence from = "Here's your reminder:";

    CharSequence message = reminderName;        
    notif.setLatestEventInfo(this, from, message, displayIntent);

    //Pause for 100ms, vibrate for 250ms, pause for 100ms, and vibrate for 500ms.
    notif.defaults |= Notification.DEFAULT_SOUND;
    notif.vibrate = new long[] { 100, 250, 100, 500 };
    nm.notify(notifID, notif);

    //Destroy the activity/notification.
    finish();

}

我知道我需要调用 alarmManager.cancel(displayIntent) 来取消我的重复闹钟。但是,我不明白应该把这段代码放在哪里。只有当用户点击通知或将其关闭时,我才需要取消重复提醒。谢谢你的帮助!


2
没关系,我在这里找到了答案:https://dev59.com/Wmoy5IYBdhLWcg3wCpsz。 - NewGradDev
3个回答

24
我相信你要找的是Notification.deleteIntent。文档中写道:

当用户通过“清除全部”按钮或单独滑动通知时明确解除通知时,执行的意图。这个意图可能不应该启动一个活动,因为同时会发送多个。


备选文档:[NotificationCompat.Builder#setDeleteIntent(PendingIntent)](https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setDeleteIntent(android.app.PendingIntent)) - JJD

3
亲爱的未来读者们,你可以注册一个广播接收器来监听通知删除的意图。
创建一个新的广播接收器:
public class NotificationBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (action == null || !action.equals(Config.NotificationDeleteAction)) {
            return;
        }

        // Do some sweet stuff
        int x = 1;
    }
}

在您的应用程序类中注册广播接收器:

如果您的应用程序目标API级别为26或更高,则无法使用清单来声明大多数隐式广播的接收器(不会特别针对您的应用程序)。

Android文档。

  registerReceiver(
        new NotificationBroadcastReceiver(),
        new IntentFilter(Config.NotificationDeleteAction)
  );

您可能注意到了静态变量Config.NotificationDeleteAction。这是您通知的唯一字符串标识符。它通常遵循以下{namespace}{actionName}规则:

you.application.namespace.NOTIFICATION_DELETE

在您的通知构建器上设置删除意图:

notificationBuilder
       ...
        .setDeleteIntent(createDeleteIntent())
       ...

其中,createDeleteIntent 是下面的一个方法:

private PendingIntent createDeleteIntent() {
    Intent intent = new Intent();
    intent.setAction(Config.NotificationDeleteAction);

    return PendingIntent.getBroadcast(
            context,
            0,
            intent,
            PendingIntent.FLAG_ONE_SHOT
    );
}

当你的通知被取消时,你注册的广播接收器应该接收到intent。


谢谢。顺便说一下,我有 targetSdkVersion 30,我只能在清单中指定BroadcastReceiver - 并使用与 Config.NotificationDeleteAction 对应的 <intent-filter /> - ban-geoengineering

1
您还可以使用 Activity PendingIntent,如果您有一个可以处理解散的Activity,那么这可能更容易实现,因为您不必创建和配置广播接收器。
public static final String DELETE_TAG = "DELETE_TAG";

private PendingIntent createDeleteIntent(Context context) {
    Intent intent = new Intent(context, MyActivity.class);
    intent.putExtra(DELETE_TAG, true);

    return PendingIntent.getActivity(
            context,
            0,
            intent,
            PendingIntent.FLAG_ONE_SHOT
    );
}

在其onCreate()中,MyActivity将接收到意图,并且在此示例中,可以查找DELETE_TAG额外信息以识别它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接