[英]Android: 2 Different Notifications with different IDs and texts shows the same text
如前所述,当设备收到2个或更多通知时,我无法在通知中成功显示不同的消息。 我尝试了一些尝试过的解决方案,但是它们没有用。 我创建通知的代码如下:
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent showMessageIntent = new Intent(context, AdminMessageActivity.class);
showMessageIntent.putExtra(WorkspaceConstants.ADMIN_MESSAGE_KEY, message);
showMessageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
int Id = new Random().nextInt();
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, showMessageIntent, 0);
String messageShort = (message.length() > 25) ? message.substring(0, 25) + "..." : message;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(context.getText(R.string.admin_message))
.setContentText(messageShort)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
mNotificationManager.notify(Id, mBuilder.build());
我使用以下代码提取消息内容:
if(getIntent() != null && getIntent().getExtras() != null){
mMessageContent = getIntent().getExtras().getString(ADMIN_MESSAGE_KEY);
}
mMessage.setText(mMessageContent);
日志中没有异常,也没有崩溃。
如果要一次显示2条通知,则应使用不同的值调用showMessageIntent上的setData。
如果你只改变的额外Intent
传递给PendingIndent
,则默认情况下它会重复使用最初的Intent
,而不是更新的演员-中通PendingIntent.FLAG_UPDATE_CURRENT作为最后一个参数PendingIntent.getActivity
以确保您的额外更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.