将不同的 Extras 传递给不同的 setDisplayIntent,但收到的是相同的

Passing different Extras to different setDisplayIntent, but same is received

我遇到的情况是我的 Watch 应用程序更新了数据,并将在多个页面中显示这些数据。

根据这个答案: ,我为我的每个页面创建了一个 displayIntent。

List extras = new ArrayList();

//This is a loop to create every individual Notification
for (Route aRoute : myRoutes) {
    Intent viewIntent = new Intent(getApplicationContext(), MainActivity.class);
    String toSend = aRoute.detail;
    Log.d("CVE",toSend);
    viewIntent.putExtra("KEY", toSend);

    PendingIntent displayendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,viewIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification aNotif = new NotificationCompat.Builder(getApplicationContext())
        .setLargeIcon(bitmap)
        .extend(new NotificationCompat.WearableExtender()
        .setDisplayIntent(displayendingIntent)
        .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
            .setSmallIcon(R.mipmap.ic_launcher)
        .build();

    extras.add(aNotif);
}

//This is "main" notification.
NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this)
    .setContentTitle(title)
    .setContentText(desc)
    .setSmallIcon(R.mipmap.ic_launcher);

Notification notification = builder1
                        .extend(new NotificationCompat.WearableExtender()
.addPages(extras))
.build();

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, notification);

直到这里,它看起来还不错,但是当我阅读每个通知上发生的事情时,我意识到数据是相同的。

这是我的简化版 Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextView = (TextView) findViewById(R.id.text_view);
    mTextView.setText("Value: "+getIntent().getStringExtra("KEY"));
}

问题是Log(见代码第一部分第6行)正确显示:

"Text 1" "Text 2" "Text 3"

,但实际上,每个 Notification 中都会显示 "Text 3"!

我应该如何解决这个问题?

不要使用 0 作为您的 PendingIntent 的请求代码,而是为您的请求的每个对象使用不同的唯一值。例如

int counter = 0;
for (Route aRoute : myRoutes) {
    // other code
    PendingIntent displayendingIntent  
         = PendingIntent.getActivity(getApplicationContext(), counter++, 
                             viewIntent, PendingIntent.FLAG_UPDATE_CURRENT);
   // other code
}