使用特定的额外内容创建多个意图,始终读取相同的额外内容

Creating multiple intent with specific extra, always read the same extra

我正在设置一个应用程序,其中包含一些由某些警报触发的通知。为了设置不同的通知,我将当前的毫秒时间作为Id。但是,当我通过 extra 将它传递给 intent 时,它总是收到相同的值。

这是我的代码,它会更清楚: 在 mainActivity 中,我们有:

class MainActivity : AppCompatActivity() {
    private lateinit var alarmManager: AlarmManager
    private lateinit var pendingIntent: PendingIntent

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        createNotificationChannel() //create a channel

    }
    
    //Set a notification that will be triggered in a given time in ms.
    //you can pass a title/description and Id in parameter
    private fun setNotification(timeMS: Long, title: String, description: String, id: Int){
        alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
        val intent = Intent(this, ReminderBroadcastReceiver::class.java)
        intent.putExtra("title", title)
        intent.putExtra("description", description)
        intent.putExtra("id", id)

        pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)

        alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeMS, pendingIntent)

    }

    //this button trigger a basics notification in 1 sec
    //here we use an id based on current time. We may use some parsed part of the corresponding deadline later.
    fun triggerNotification(view:View) {
        var id = System.currentTimeMillis().toInt()
        setNotification(System.currentTimeMillis()+1000, "foo", "ouafouaf", id)
    }

然后在 ReminderBroadcastReceiver 中:

class ReminderBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        var channelId = "remindersChannel"
        var title = intent!!.getStringExtra("title")
        var content = intent!!.getStringExtra("description")
        var notifId = intent!!.getIntExtra("id", 0)

        val intent2 = Intent(context, MainActivity::class.java)
        intent2!!.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK


        val pendingIntent = PendingIntent.getActivity(context, 0, intent2, PendingIntent.FLAG_IMMUTABLE)

        val notifBuilder = NotificationCompat.Builder(context!!, channelId)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle(title + notifId.toString())
            .setContentText(content)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .setCategory(NotificationCompat.CATEGORY_REMINDER)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)

        val notificationManager = NotificationManagerCompat.from(context)
        notificationManager.notify(notifId, notifBuilder.build())
    }
}

我的问题是,如果我在不同时间点击按钮两次,收到的 ID 不会改变。当我把 System.currentTimeMillis().toInt() 直接放在 notify 的参数中时,效果很好。

知道如何解决这个问题吗?

对于每个不同的通知,PendingIntent.getBroadcast() 的第二个参数需要不同。

(只需使用 CommonsWare 的评论来回答并关闭此线程)