2012-09-30 76 views
22

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)以取消我重复报警。但是,我不明白该把代码放在哪里。只有当用户点击通知或解散通知时,我才需要取消重复提醒。谢谢你的帮助!

+2

没关系,我在这里找到我的答案:http://stackoverflow.com/questions/8811876/notification-deleteintent-does-not-work。 – NewGradDev

回答

15

我相信Notification.deleteIntent是你在找什么。该文档说:

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

+7

如何?你能通过提供一些示例代码来帮助吗? –

+0

此解决方案对我有帮助:https://stackoverflow.com/a/14723652/563509 – masterwok

0

对于所有那些未来的人 - 你可以注册一个广播接收器来侦听通知删除inents。

创建一个新的广播接收器:

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。这是您的通知的唯一字符串标识符。它通常遵循以下{命名空间} {} 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 
    ); 
} 

您的注册广播接收机应在您的通知被解除时收到意图。