简体   繁体   中英

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. 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以确保您的额外更新。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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