我正在处理触发预定通知的应用程序。
我面临的是并非所有预定的通知都会被解雇。
在我的例子中,有时它只触发最后一个通知(event4),有时它会触发跳过第二个的第1个,第3个和第4个通知。 为什么会这样?
这是我的代码:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE,26);
cal.set(Calendar.MONTH,11);
cal.set(Calendar.YEAR,2015);
cal.set(Calendar.HOUR_OF_DAY, 15);
cal.set(Calendar.MINUTE, 9);
cal.set(Calendar.SECOND, 10);
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.DATE,26);
cal2.set(Calendar.MONTH,11);
cal2.set(Calendar.YEAR,2015);
cal2.set(Calendar.HOUR_OF_DAY, 15);
cal2.set(Calendar.MINUTE, 9);
cal2.set(Calendar.SECOND, 20);
Calendar cal3 = Calendar.getInstance();
cal3.set(Calendar.DATE,26);
cal3.set(Calendar.MONTH, 11);
cal3.set(Calendar.YEAR,2015);
cal3.set(Calendar.HOUR_OF_DAY, 15);
cal3.set(Calendar.MINUTE, 9);
cal3.set(Calendar.SECOND, 30);
Calendar cal4 = Calendar.getInstance();
cal4.set(Calendar.DATE,26);
cal4.set(Calendar.MONTH, 11);
cal4.set(Calendar.YEAR,2015);
cal4.set(Calendar.HOUR_OF_DAY, 15);
cal4.set(Calendar.MINUTE, 9);
cal4.set(Calendar.SECOND, 40);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alertIntent = new Intent(this, AlertReceiver.class);
alertIntent.putExtra("Notification Key", 1);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
alertIntent.putExtra("Notification Key", 2);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), PendingIntent.getBroadcast(this, 2, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
alertIntent.putExtra("Notification Key", 3);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal3.getTimeInMillis(), PendingIntent.getBroadcast(this, 3, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
alertIntent.putExtra("Notification Key", 4);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal4.getTimeInMillis(), PendingIntent.getBroadcast(this, 4, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
这是OnReceive
public class AlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Integer notificationId = intent.getIntExtra("Notification Key", -1);
switch(notificationId){
case 1:
createNotification(context, "title1", "event1", "event of today");
break;
case 2:
createNotification(context, "title2", "event2", "event of today");
break;
case 3:
createNotification(context, "title3", "event3", "event of today");
break;
case 4:
createNotification(context, "title4", "event4", "event of today");
break;
}
}
public void createNotification(Context context, String msg, String msgText, String msgAlert) {
PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.not)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
//intent to fire when notification clicked on
mBuilder.setContentIntent(notificIntent);
//how the person will be notified
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
//cancel notification when clicked in the taskbar
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager= (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0,mBuilder.build());
答案 0 :(得分:0)
通知ID对于已发布的通知不应该是唯一的
mNotificationManager.notify(0,mBuilder.build());
更改上面的代码,如
mNotificationManager.notify(some_random_number++,mBuilder.build());
使用任何随机数..您可以使用以下脚本生成随机数
Random rand = new Random2();
randomNum = minimum + rand.nextInt((maximum - minimum) + 1);