![](/img/trans.png)
[英]Set action in multiple local notification in android giving same id for notification in broadcast receiver
[英]Cancelling multiple notification by sending unique id to broadcast receiver
我使用AlarmManager
创建多个通知。 每个通知都正确弹出,我想分别取消每个通知。 我有一个AlarmReceiver
来创建通知并为其设置唯一的 id。 我正在发送意图中的唯一 ID(通过putExtra
)。 这样,我可以在NotificationReceiver
检索它并取消通知。 但是,它不起作用。
你知道为什么我在使用intent.getIntExtra("UNIQUE_ID_NAME",0)
时总是得到默认值吗? 这是实现取消这些通知的目标的正确方法吗? 这是我的代码:
主活动类:
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ReminderBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
long ctime = System.currentTimeMillis();
long tensec = 1000 * 5;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, ctime+tensec, 1000*10 , pendingIntent);
}
警报接收器类:
@Override
public void onReceive(Context context, Intent intent) {
int notification_id = generateRandom();
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.putExtra(NotificationReceiver.UNIQUE_ID_NAME, notification_id);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent bYesIntent = new Intent(context, NotifReceiver.class);
bYesIntent.putExtra(NotificationReceiver.YES_ACTION_ID, "yesID");
PendingIntent actionYesIntent = PendingIntent.getBroadcast(context, 0, bYesIntent, 0);
Intent bNoIntent = new Intent(context, NotifReceiver.class);
bNoIntent.putExtra(NotificationReceiver.NO_ACTION_ID, "noID");
PendingIntent actionNoIntent = PendingIntent.getBroadcast(context, 0, bNoIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "channel1")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Notif")
.setContentText("Text")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(contentIntent)
.addAction(R.mipmap.ic_launcher, "YES", actionYesIntent)
.addAction(R.mipmap.ic_launcher, "NO", actionNoIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notification_id, builder.build());
}
public int generateRandom(){
Random random = new Random();
return random.nextInt(9999 - 1000) + 1000;
}
通知接收器类:
public class NotificationReceiver extends BroadcastReceiver {
public static String YES_ACTION_ID = "YesID";
public static String NO_ACTION_ID = "NoID";
public static String UNIQUE_ID_NAME = "notification_id";
@Override
public void onReceive(Context context, Intent intent) {
int notification_id= intent.getIntExtra(UNIQUE_ID_NAME,0);
System.out.println("Notif ID: " + notification_id); // here always is 0;
String actionYes = intent.getStringExtra(YES_ACTION_ID);
String actionNo = intent.getStringExtra(NO_ACTION_ID);
if(actionYes!=null){
Toast.makeText(context, "YES", Toast.LENGTH_SHORT).show();
}
if(actionNo!=null){
Toast.makeText(context, "NO", Toast.LENGTH_SHORT).show();
}
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(notification_id);
}
}
我通过更新当前意图和请求代码解决了这个问题。 通知正在重用相同的 Intent,因此应该使用添加FLAG_UPDATE_CURRENT
来分别取消所有通知。
对于 contentIntent:
PendingIntent contentIntent = PendingIntent.getActivity(context, notification_id, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
对于行动YesIntent
PendingIntent actionYesIntent = PendingIntent.getBroadcast(context, notification_id, bYesIntent, PendingIntent.FLAG_UPDATE_CURRENT);
对于 actionNoIntent:
PendingIntent actionNoIntent = PendingIntent.getBroadcast(context, notification_id, bNoIntent, PendingIntent.FLAG_UPDATE_CURRENT);
您没有为按下 Yes 或 No 按钮的情况添加notification_id
,
Intent bYesIntent = new Intent(context, NotifReceiver.class);
bYesIntent.putExtra(NotificationReceiver.UNIQUE_ID_NAME, notification_id);
bYesIntent.putExtra(NotificationReceiver.YES_ACTION_ID, "yesID");
PendingIntent actionYesIntent = PendingIntent.getBroadcast(context, 0, bYesIntent, 0);
Intent bNoIntent = new Intent(context, NotifReceiver.class);
bNoIntent.putExtra(NotificationReceiver.UNIQUE_ID_NAME, notification_id);
bNoIntent.putExtra(NotificationReceiver.NO_ACTION_ID, "noID");
PendingIntent actionNoIntent = PendingIntent.getBroadcast(context, 0, bNoIntent, 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.