为什么仅从最后一个通知向 activity 发送额外参数?

Why is an extra parameter sent only from a last notification to an activity?

各位!

我正在发送两个内容文本不同的本地推送通知。单击每次推送时,我希望某个 activity (RecallActivity) 将打开与单击通知的内容文本相对应的文本。但是我总是从上次通知中获得额外的数据,即使我点击了第一个。我不明白为什么。

创建通知的服务代码:

override fun doWork(): Result {

    val word = inputData.getString("WORD")

    val intent = Intent(applicationContext, RecallActivity::class.java).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        this.putExtra("word", word)
    }

    val pendingIntent: PendingIntent? = TaskStackBuilder.create(applicationContext).run {
        addNextIntent(intent)
        getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
    }

    val builder = NotificationCompat.Builder(context, "CHANNEL_ID")
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("Scheduled notification")
        .setContentText(word)
        .setContentIntent(pendingIntent)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setAutoCancel(true)

    with(NotificationManagerCompat.from(context)) {
        notify(nextInt(), builder.build())
    }

    return Result.success()
}

来自我的 Activity 的代码试图从通知中获取额外参数:

class RecallActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recall)

        wordTextView.text = intent.getStringExtra("word")
    }
}

求求你帮忙:)

    val pendingIntent: PendingIntent? = TaskStackBuilder.create(applicationContext).run {
        addNextIntent(intent)
        getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
    }

不要总是使用0。为每个不同的 PendingIntent.

使用不同的 requestCode

I am sending two local push notifications with different content texts

那么您应该使用两个不同的 requestCode 值,例如 01