When building a notification in android, we provide a PendingIntent which contains an Intent object which also contains some data passed as extras.
// here we create the Intent and we add the extra model data to it
val notificationIntentActivity =
notificationService.getActivityIntent(context)
notificationIntentActivity.putExtra(ARG_MODEL, myModel)
// here we create the PendingIntent which contains the notificationIntentActivity
val pendingIntent = PendingIntent.getActivity(
context,
0,
notificationIntentActivity,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
// here we build the notification where we pass the pendingIntent
val mBuilder: NotificationCompat.Builder = NotificationCompat.Builder(
context,
channelId
)
.setContentIntent(pendingIntent)
mBuilder.build()
So now lets say that the user opens the application and i want to check the following
- If there is any available active notification about my app
- If there is any, then i want to extract the "ARG_MODEL" data from it
override fun onResume() {
super.onResume()
val nm = requireContext().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val mNotification = nm.activeNotifications.first().notification
// here how can i get the Intent Model data ("myModel") from the notification object??
}