Reputation: 1078
I have an Android application which will receive push notification. The notification have wearable support hence the notification will also be visible in Android wear with action button.
I am looking to pass data when the notification reaches onMessageReceived
in FirebaseMessagingService
class. Tried setting data in Intent and passed that with Pending Intent.
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.data = Uri.parse("12345")
intent.putExtra("user_id", "USER ID")
intent.putExtra("date", "DATE")
val pendingIntent = PendingIntent.getActivity(
applicationContext,
System.currentTimeMillis().toInt() , intent,
0
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel =
NotificationChannel(CHANNEL_ID,
CHANNEL_NAME,
CHANNEL_IMPORTANCE)
val notificationManager =
this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(notificationChannel)
val notificationBuilder = NotificationCompat.Builder(this,CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setContentText(body)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.extend(NotificationCompat.WearableExtender()
.addAction(NotificationCompat.Action(R.drawable.icon,
"Explore",
pendingIntent))
)
notificationManager.notify(0, notificationBuilder.build())
}
When click on the notification from Wear, capturing the intent in onNewIntent
to get the data passed. But couldn't find the data which were passed.
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
setIntent(intent)
// intent.getStringExtra("date")
// intent.extras.getString("date")
// intent.extras.get("date")
// intent.data
}
Couldn't get the data. Is there any way to get the intent passed with the pending intent ? Or how to get the values passed with Pending Intent.
Upvotes: 0
Views: 2351
Reputation: 95646
My assumption is that onNewIntent()
is not being called.
If you specify FLAG_ACTIVITY_CLEAR_TOP
and your app is already running and there is an instance of MainActivity
in the task stack, Android will remove the existing instance of MainActivity
and create a new one. The Intent
with "extras" will then be delivered in onCreate()
and onNewIntent()
will not be called.
If your app is not running, tapping the notification will start your app and create a new instance of MainActivity
and the Intent
with the "extras" will be delived in onCreate()
and onNewIntent()
will not be called.
Also, since the Intent
that you put in the PendingIntent
will be used by Android to launch an Activity
from a non-Activity
context, you need to specify Intent.FLAG_ACTIVITY_NEW_TASK
.
Also, you should always specify FLAG_UPDATE_CURRENT
when getting a PendingIntent
with "extras", because if you do not, you may get an existing PendingIntent
with some old "extras" or maybe none at all. Using FLAG_UPDATE_CURRENT
will ensure that your Intent
"extras" are copied into the PendingIntent
even if it already exists. Like this:
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.data = Uri.parse("12345")
intent.putExtra("user_id", "USER ID")
intent.putExtra("date", "DATE")
val pendingIntent = PendingIntent.getActivity(
applicationContext,
System.currentTimeMillis().toInt() , intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
Upvotes: 0