【问题标题】:Deleting the scheduled notification删除预定通知
【发布时间】:2015-01-17 05:36:16
【问题描述】:

我正在安排事件并每周或每天创建相同的通知。通知正在正确的时间显示。

问题 一旦计划的事件被删除,我想删除通知。但即使事件被删除,我也会收到通知。我尝试通过传递用于创建该意图的相同 unique_id(eventId) 来删除 Pending Intent,但它没有被删除。请在下面找到我的代码:

创建通知

Intent intent = new Intent(this, MyReceiver.class);
        HABIT_NAME  = habitName.getText().toString();
        intent.putExtra("HABIT_NAME", habitName.getText().toString());

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), eventId, intent, 0);
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        if(daysNo.size() == 7){           //alarm for every day
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,reminderLifeTS.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 
        }else{                            // specific days of week
            for(int i=0;i<daysNo.size();i++){
                int day = getWeek(daysNo.get(i));
                Calendar c = Calendar.getInstance();
                c.set(Calendar.DAY_OF_WEEK, day);
                c.set(Calendar.HOUR_OF_DAY, reminderLifeTS.get(Calendar.HOUR_OF_DAY));
                c.set(Calendar.MINUTE, reminderLifeTS.get(Calendar.MINUTE));    
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                        c.getTimeInMillis(),7*24*60*60*1000, pendingIntent);
            }
        }

服务类

mManager = (NotificationManager) this.getApplicationContext().
                   getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);

           Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);

           intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

           PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
                   this.getApplicationContext(),0, intent1,PendingIntent.FLAG_ONE_SHOT);

           // megha
           NotificationCompat.Builder mBuilder =
                   new NotificationCompat.Builder(this)
                   .setSmallIcon(R.drawable.app_icon)
                   .setContentTitle("DharmaLife")
                   .setContentText(AddScheduleEventActivity.HABIT_NAME)
                   .setContentIntent(pendingNotificationIntent);

           Notification note = mBuilder.build();
           note.defaults |= Notification.DEFAULT_VIBRATE;
           note.defaults |= Notification.DEFAULT_LIGHTS;
           note.flags|= Notification.FLAG_AUTO_CANCEL;

           PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
           WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
                   PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
           wakeLock.acquire();

删除通知

Intent intent = new Intent();
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), eventId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            pendingIntent.cancel();
            AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);

            am.cancel(pendingIntent);

编辑 为了保留创建的所有待处理意图的列表,我将其连同其唯一 ID 一起保存在 arrayList 中:

notificationData.setAlarmManager(alarmManager);
notificationData.setPendingIntent(pendingIntent);
notificationData.setEventId(eventId);
LifeLiteral.NOTIFICATION_STACK.add(notificationData);

删除通知:

for(NotificationData i : LifeLiteral.NOTIFICATION_STACK){
    if(i.getEventId() == eventId){
    pendingIntent = i.getPendingIntent();
    alarmManager = i.getAlarmManager();
    alarmManager.cancel(pendingIntent);
    pendingIntent.cancel();
    }
}

但通知仍然没有被删除。请指导。

【问题讨论】:

    标签: android notifications android-pendingintent


    【解决方案1】:

    尝试删除 PendingIntent 时,您没有传递与原始 Intent 匹配的 Intent(因为传递给 getBroadcast()Intent 为空)。将您的删除代码更改为:

    Intent intent = new Intent(this, MyReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(),
        eventId, intent, 0);
    AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    am.cancel(pendingIntent);
    // Cancel the `PendingIntent` after you've canceled the alarm
    pendingIntent.cancel();
    

    【讨论】:

    • 我仍然收到通知。
    • 你确定你使用的是同一个eventId来删除它们吗?
    • 我很困惑。什么触发通知?闹钟响起时会发生这种情况吗?
    • @Async- 请在您的问题和代码中发布一个新问题,我们可以为您提供帮助。在此处发表评论对您或其他任何人都没有帮助。
    • 你救救我! tks!
    猜你喜欢
    • 2018-01-08
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多