我正在安排活动并每周或每天为其创建相同的通知。通知正在正确的时间显示。
问题 我想删除预定事件后删除通知。但即使事件被删除,我也会收到通知。我尝试通过传递我用于创建该意图的相同unique_id( eventId )来删除待定意图,但不删除。请在下面找到我的代码:
创建通知
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);
修改 要保留创建的所有待处理意图的列表,我将它保存在arrayList中以及它的唯一ID:
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();
}
}
但仍然没有删除通知。请指导。
答案 0 :(得分:4)
尝试删除PendingIntent
时,您未通过与原Intent
匹配的Intent
(因为Intent
传递给getBroadcast()
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();
} 是空的)。将删除代码更改为:
{{1}}