Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

everybody!

I am sending two local push notifications with different content texts. When clicking on each push, I expect that a certain activity (RecallActivity) will open with the text corresponding to the content text of the clicked notification. But I always get extra data from last notification, even if I click on the first one. I can’t understand why.

Code from my service that creates the Notification:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()
}

Code from my Activity that tries to fetch the extra parameter from the notification:

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

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

Please, help 🙂

>Solution :

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

Do not always use 0. Use a different requestCode value for each distinct PendingIntent.

I am sending two local push notifications with different content texts

Then you should be using two different requestCode values, such as 0 and 1.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading