如何将多个附加项附加到捆绑通知中的 Intent?
How can I attach multiple extras to an Intent in the Bundled notification?
我收到了好几条通知,每一条都有自己独特的id。
理想的情况是每个 通知 都有自己的 extra。
但是当我 store/stash notifications 在 Bundle object all 通知被分配了相同的额外-(即属于[=的额外 39=] 到最后一个 通知).
我已经确认了这一点,因为当我点击任何通知时,我被发送到最后一个post我收到。
虽然我想发送到通知对应的post我才点了
下面是相关的代码片段:-
public void createNotif(Poste poste, Comment comment, Boolean checkGroupSon) {
if (comment == null) {
emeteur = poste.getEmmet();
message = poste.getTx();
pathImg = poste.getImg();
} else {
emeteur = comment.getEmmet();
message = comment.getComment();
pathImg = comment.getImage();
}
//onDismiss Intent
Intent intent = new Intent(mContext, NotificationControllerReceiver.class);
PendingIntent mBroadcastIntentController = PendingIntent.getBroadcast(mContext, 0, intent, 0);
Intent notificationIntent = new Intent(mContext, CommentPhotoActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Intent.FLAG_ACTIVITY_CLEAR_TASK|
notificationIntent.putExtra("pid", poste.getId());
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//TEST notification.setLatestEventInfo(getApplicationContext(), "YuYu", "Vous avez reçu un nouveau poste", contentIntent);
//TEST notification.flags = Notification.FLAG_AUTO_CANCEL;
//TEST
// NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext()).setContentTitle("Poste: "+emeteur).setSmallIcon(R.drawable.ic_launcher).setContentIntent(contentIntent).setContentText(message).setDeleteIntent(mBroadcastIntentController).setPriority(Notification.PRIORITY_MAX);
NotificationCompat.Builder notificationBuilder = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder = new NotificationCompat.Builder(mContext, "poste");
if (pathImg == null)
notificationBuilder.setContentTitle(emeteur).setSmallIcon(R.drawable.ic_launcher).setContentIntent(contentIntent).setContentText(emeteur + ": " + message).setDeleteIntent(mBroadcastIntentController).setPriority(NotificationCompat.PRIORITY_MAX);
else
notificationBuilder.setContentTitle(emeteur).setSmallIcon(R.drawable.ic_launcher).setContentIntent(contentIntent).setContentText(emeteur + ": Photo \uD83D\uDCF7 " + message).setDeleteIntent(mBroadcastIntentController).setPriority(NotificationCompat.PRIORITY_MAX);
}
// notificationBuilder.setContentTitle("Poste: "+emeteur).setSmallIcon(R.drawable.ic_launcher).setContentIntent(contentIntent).setContentText(message).setDeleteIntent(mBroadcastIntentController).setPriority(Notification.PRIORITY_MAX);
}
// long[] v = {500,1000};
// notificationBuilder.setVibrate(v);
notifications = null;
boolean b = notifHashMap.containsKey(poste.getId());
if (b) {
notifications = notifHashMap.get(poste.getId());
// Add your All messages here or use Loop to generate messages
if (pathImg == null)
notifications.add(emeteur + ":" + message);
else
notifications.add(emeteur + ": Photo \uD83D\uDCF7 " + message);
notifHashMap.put(poste.getId(), notifications);
} else {
notifications = new ArrayList<String>();
// Add your All messages here or use Loop to generate messages
if (pathImg == null)
notifications.add(emeteur + ":" + message);
else
notifications.add(emeteur + ": Photo \uD83D\uDCF7 " + message);
notifHashMap.put(poste.getId(), notifications);
}
// if (inboxStyle == null)
// inboxStyle = new NotificationCompat.InboxStyle();
//else
inboxStyle = new NotificationCompat.InboxStyle(notificationBuilder);
if (notifications.size() > 1) {
if (poste.getImg() == null)
inboxStyle.setBigContentTitle("Poste:" + poste.getTx());
else
inboxStyle.setBigContentTitle("Poste: \uD83D\uDCF7 " + poste.getTx());
inboxStyle.setSummaryText("Vous avez " + notifications.size() + " notifications.");
for (int i = 0; i < notifications.size(); i++) {
inboxStyle.addLine(notifications.get(i));
}
} else if (notifications.size() == 1) {
inboxStyle.addLine(notifications.get(0));
}
notificationBuilder.setStyle(inboxStyle);
notificationBuilder.setVibrate(new long[]{0L});
//notificationBuilder.setNumber(value++);
if (!checkGroupSon) {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(alarmSound);
}
NotifManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
/* Create or update. */
mChannel = new NotificationChannel("poste", "YuYu", NotificationManager.IMPORTANCE_LOW);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.canShowBadge();
mChannel.setVibrationPattern(new long[]{ 0 });
mChannel.enableVibration(true);
/* if (!checkGroupSon) {
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
} */
assert NotifManager != null;
notificationBuilder.setChannelId("poste");
// NotifManager.deleteNotificationChannel("poste");
NotifManager.createNotificationChannel(mChannel);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
summaryNotification = new NotificationCompat.Builder(mContext, "poste")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.InboxStyle()
.addLine("1")
.addLine("2")
.setBigContentTitle("nouveau messages")
.setSummaryText("Nouveau message"))
.setPriority(NotificationCompat.PRIORITY_LOW)
.setGroup("example_group")
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN)
.setGroupSummary(true).build();
}
Notification notification = notificationBuilder.setGroup("example_group").build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
// assert NotifPostMsgManager != null;
//TEST manager.notify(ID_NOTIFICATION, notification);
NotifManager.notify(poste.getId(), notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
NotifManager.notify(1, summaryNotification);
}
您在 NotifManager.notify(1, summaryNotification) //id == 1
中使用相同的 ID,这就是为什么您得到相同的意图结果,您应该使用不同的 ID,并且它应该作为 sharedPreferences
[=12] 存储在一些已保存的数据中=]
我收到了好几条通知,每一条都有自己独特的id。
理想的情况是每个 通知 都有自己的 extra。
但是当我 store/stash notifications 在 Bundle object all 通知被分配了相同的额外-(即属于[=的额外 39=] 到最后一个 通知).
我已经确认了这一点,因为当我点击任何通知时,我被发送到最后一个post我收到。
虽然我想发送到通知对应的post我才点了
下面是相关的代码片段:-
public void createNotif(Poste poste, Comment comment, Boolean checkGroupSon) {
if (comment == null) {
emeteur = poste.getEmmet();
message = poste.getTx();
pathImg = poste.getImg();
} else {
emeteur = comment.getEmmet();
message = comment.getComment();
pathImg = comment.getImage();
}
//onDismiss Intent
Intent intent = new Intent(mContext, NotificationControllerReceiver.class);
PendingIntent mBroadcastIntentController = PendingIntent.getBroadcast(mContext, 0, intent, 0);
Intent notificationIntent = new Intent(mContext, CommentPhotoActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Intent.FLAG_ACTIVITY_CLEAR_TASK|
notificationIntent.putExtra("pid", poste.getId());
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//TEST notification.setLatestEventInfo(getApplicationContext(), "YuYu", "Vous avez reçu un nouveau poste", contentIntent);
//TEST notification.flags = Notification.FLAG_AUTO_CANCEL;
//TEST
// NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext()).setContentTitle("Poste: "+emeteur).setSmallIcon(R.drawable.ic_launcher).setContentIntent(contentIntent).setContentText(message).setDeleteIntent(mBroadcastIntentController).setPriority(Notification.PRIORITY_MAX);
NotificationCompat.Builder notificationBuilder = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder = new NotificationCompat.Builder(mContext, "poste");
if (pathImg == null)
notificationBuilder.setContentTitle(emeteur).setSmallIcon(R.drawable.ic_launcher).setContentIntent(contentIntent).setContentText(emeteur + ": " + message).setDeleteIntent(mBroadcastIntentController).setPriority(NotificationCompat.PRIORITY_MAX);
else
notificationBuilder.setContentTitle(emeteur).setSmallIcon(R.drawable.ic_launcher).setContentIntent(contentIntent).setContentText(emeteur + ": Photo \uD83D\uDCF7 " + message).setDeleteIntent(mBroadcastIntentController).setPriority(NotificationCompat.PRIORITY_MAX);
}
// notificationBuilder.setContentTitle("Poste: "+emeteur).setSmallIcon(R.drawable.ic_launcher).setContentIntent(contentIntent).setContentText(message).setDeleteIntent(mBroadcastIntentController).setPriority(Notification.PRIORITY_MAX);
}
// long[] v = {500,1000};
// notificationBuilder.setVibrate(v);
notifications = null;
boolean b = notifHashMap.containsKey(poste.getId());
if (b) {
notifications = notifHashMap.get(poste.getId());
// Add your All messages here or use Loop to generate messages
if (pathImg == null)
notifications.add(emeteur + ":" + message);
else
notifications.add(emeteur + ": Photo \uD83D\uDCF7 " + message);
notifHashMap.put(poste.getId(), notifications);
} else {
notifications = new ArrayList<String>();
// Add your All messages here or use Loop to generate messages
if (pathImg == null)
notifications.add(emeteur + ":" + message);
else
notifications.add(emeteur + ": Photo \uD83D\uDCF7 " + message);
notifHashMap.put(poste.getId(), notifications);
}
// if (inboxStyle == null)
// inboxStyle = new NotificationCompat.InboxStyle();
//else
inboxStyle = new NotificationCompat.InboxStyle(notificationBuilder);
if (notifications.size() > 1) {
if (poste.getImg() == null)
inboxStyle.setBigContentTitle("Poste:" + poste.getTx());
else
inboxStyle.setBigContentTitle("Poste: \uD83D\uDCF7 " + poste.getTx());
inboxStyle.setSummaryText("Vous avez " + notifications.size() + " notifications.");
for (int i = 0; i < notifications.size(); i++) {
inboxStyle.addLine(notifications.get(i));
}
} else if (notifications.size() == 1) {
inboxStyle.addLine(notifications.get(0));
}
notificationBuilder.setStyle(inboxStyle);
notificationBuilder.setVibrate(new long[]{0L});
//notificationBuilder.setNumber(value++);
if (!checkGroupSon) {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(alarmSound);
}
NotifManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
/* Create or update. */
mChannel = new NotificationChannel("poste", "YuYu", NotificationManager.IMPORTANCE_LOW);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.canShowBadge();
mChannel.setVibrationPattern(new long[]{ 0 });
mChannel.enableVibration(true);
/* if (!checkGroupSon) {
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
} */
assert NotifManager != null;
notificationBuilder.setChannelId("poste");
// NotifManager.deleteNotificationChannel("poste");
NotifManager.createNotificationChannel(mChannel);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
summaryNotification = new NotificationCompat.Builder(mContext, "poste")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.InboxStyle()
.addLine("1")
.addLine("2")
.setBigContentTitle("nouveau messages")
.setSummaryText("Nouveau message"))
.setPriority(NotificationCompat.PRIORITY_LOW)
.setGroup("example_group")
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN)
.setGroupSummary(true).build();
}
Notification notification = notificationBuilder.setGroup("example_group").build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
// assert NotifPostMsgManager != null;
//TEST manager.notify(ID_NOTIFICATION, notification);
NotifManager.notify(poste.getId(), notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
NotifManager.notify(1, summaryNotification);
}
您在 NotifManager.notify(1, summaryNotification) //id == 1
中使用相同的 ID,这就是为什么您得到相同的意图结果,您应该使用不同的 ID,并且它应该作为 sharedPreferences
[=12] 存储在一些已保存的数据中=]