Yevgen
Yevgen

Reputation: 101

Not pass value from push notification to Activity

I can’t pass value from push notification to activity

I use Firebase Messaging Service for push notification. But I can’t pass value to activity using PendingIntent.

MyFirebaseMessagingService

val intent = Intent(this, MainActivity::class.java)
    intent.putExtra("key", "value")
    val contentIntent = PendingIntent.getActivity(
            this, 0, intent, 0)

    notificationBuilder.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.sa_logo_mini)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.sa_icon_big))
            .setContentTitle(title)
            .setContentText(content)
            .setContentIntent(contentIntent)
            .setContentInfo("Breaking")

    notificationManager.notify(1, notificationBuilder.build())

MainActivity

    val Y = intent.getStringExtra("key")
    Log.i(C.T, "CHECHE = "+Y)

After click on push notification MainActivity start but intent.getStringExtra("key") always equals null.

Where a mistake?

Upvotes: 0

Views: 64

Answers (1)

Sergio
Sergio

Reputation: 30655

Try to use some flags, for example use FLAG_ONE_SHOT and FLAG_CANCEL_CURRENT to indicate that PendingIntent can be used only once and if the described PendingIntent already exists, the current one should be canceled before generating a new one:

val contentIntent = PendingIntent.getActivity(this, 0, 
    intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);

Upvotes: 1

Related Questions