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

时间:2015-05-09 06:47:40

标签: android android-intent notifications wear-os

我的Watch应用程序已更新数据,并将在多个页面中显示这些数据。

根据这个回答:https://stackoverflow.com/a/30133449/327402,我为每个页面创建了一个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);

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

这是我简化的活动:

@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"));
}

问题是日志(参见代码第一部分的第6行)显示正确:

“文字1” “文字2” “文字3”

,但实际上,每个通知中都会显示“Text 3”!

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

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

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