问题描述
在我的 android 应用程序中,我需要在特定的一天以不同的时间间隔显示多个本地通知,我使用警报管理器和广播接收器来处理一个通知,但是当我尝试实现多个通知时,只有第二个通知显示.
In my android app I need to show multiple local notifications on a particular day at different time intervals,I used alarm manager and broadcast receiver to do for one notification but when I was trying to implement multiple notifications only the second one is been displayed.
这是我的主要活动
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.set(2015, 10, 23, 15, 03);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
这里是广播接收器
Intent notificationIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent1 = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle("TRR - 2016")
.setContentText("Station - 1 closed grace period only 10min")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent1).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(mCounter, notification);
Notification notification1 = builder.setContentTitle("TRR - 2016")
.setContentText("Station - 2 closed grace period only 10min")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent1).build();
NotificationManager notificationManager1 = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager1.notify(++mCounter, notification1);
推荐答案
每个通知必须有自己的通知 ID.
Each Notification must have its own Notification ID.
您的问题在这里:
notificationManager.notify(0, notification);
具体来说,0"是通知的ID.如果您不提供不同的 ID,Android 会认为您只是在更新已经存在的通知.
Specifically, the "0" is the ID of the Notification. If you do not provide a different ID, Android will think you are simply updating the Notification that already exists.
文档.
public void notify (int id, Notification notification)
发布要在状态栏中显示的通知.如果通知您的应用程序已经发布了具有相同 ID 的尚未取消,将以更新的信息代替.
Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.
你可以尝试这样的事情:
You could try something like this:
private int mCounter = 0;
...
notificationManager1.notify(++mCounter, notification1);
这篇关于如何在android中创建多个本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!