如何检测通知是否已被解除?

时间:2012-09-30 21:07:01

标签: android alarmmanager alarm repeat notificationmanager

Android中是否有任何方法可以检测用户何时向左侧滑动通知并将其删除?我正在使用闹钟管理器来设置重复警报,当用户取消通知时,我需要停止重复警报。这是我的代码:

设置重复提醒:

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)才能取消重复闹铃。但是,我不明白在哪里放这个代码。我只需要在用户点击通知或解除通知时取消重复提醒。谢谢你的帮助!

3 个答案:

答案 0 :(得分:17)

我相信Notification.deleteIntent正是您所寻找的。医生说:

  

当用户明确驳回通知时执行的意图,使用"全部清除"按钮或单独滑动。这可能不应该启动一项活动,因为其中一些活动将同时发送。

答案 1 :(得分:0)

对于所有未来的人 - 您可以注册广播接收器以收听通知删除内容。

创建新的广播接收器:

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

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

您的注册广播接收者应在您的通知被驳回时收到意图。

答案 2 :(得分:0)

您还可以使用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
    );
}

MyActivity将在其onCreate()中接收intent,并且在此示例中,可以查找DELETE_TAG extra来识别它。