我想编程一个应用程序,发送两种类型的通知。用户应该同时看到它们。
直到现在,通知彼此更新,即使我改变了pendingintent的标志。
这是我的代码:
Calendar calendar = Calendar.getInstance();
Intent intent = new Intent(this, Push.class);
Intent intent2 = new Intent(this, Push2.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()+10, pendingIntent);
alarmManager2.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()+10, pendingIntent2);
创建2个不同的通知生成器对象,
例子第一个通知对象
Notification.Builder builder = new Notification.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.some_img)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.your_notif_title))
.setContentText(res.getString(R.string.your_notif_text));
Notification n1 = builder.build();
第二个通知对象
Notification.Builder builder2 = new Notification.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.some_img)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.your_notif_title))
.setContentText(res.getString(R.string.your_notif_text));
Notification n2 = builder2.build();
NotificationManager nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
使用通知管理器显示通知
nm.notify(YOUR_NOTIF_ID, n1);
nm.notify(YOUR_NOTIF_ID_2, n2);
请注意,参考代码取自此回答