简体   繁体   English

Android:2个具有不同ID和文本的不同通知显示相同文本

[英]Android: 2 Different Notifications with different IDs and texts shows the same text

As mentioned I am not able to successfully show different messages in my notifications when the devices receives 2 or more notifications. 如前所述,当设备收到2个或更多通知时,我无法在通知中成功显示不同的消息。 I have tried some of the solutions which I tried but they didnt work. 我尝试了一些尝试过的解决方案,但是它们没有用。 My code to create notification is as below: 我创建通知的代码如下:

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());

I extract the message content with the below code: 我使用以下代码提取消息内容:

if(getIntent() != null && getIntent().getExtras() != null){ 
    mMessageContent = getIntent().getExtras().getString(ADMIN_MESSAGE_KEY); 
} 

mMessage.setText(mMessageContent);

No exception and no crash in the logs. 日志中没有异常,也没有崩溃。

如果要一次显示2条通知,则应使用不同的值调用showMessageIntent上的setData。

如果你只改变的额外Intent传递给PendingIndent ,则默认情况下它会重复使用最初的Intent ,而不是更新的演员-中通PendingIntent.FLAG_UPDATE_CURRENT作为最后一个参数PendingIntent.getActivity以确保您的额外更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM