I'd like to bring up a notification with a button within the notification. When that button is pressed it should call a function elsewhere in my code. I've got the following code running but it doesn't make the call to the function when I press the button.
The code below brings up the notification and gives the user the option to select 'accept' but I can't figure out how to connect it to the function I want.
fun showActionNotification(
subscription: String,
challenge: String,
) {
val acceptIntent = Intent( context, ChallengesViewModel::class.java )
.apply{ /* Can I make a call to a function in ChallengesViewModel here? */ }
val acceptPendingIntent = PendingIntent.getActivity(context, 0, acceptIntent, PendingIntent.FLAG_IMMUTABLE)
val notification = NotificationCompat.Builder(context, "challenge_notification_id")
.setContentTitle(subscription)
.setContentText(challenge)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.addAction(
R.drawable.ic_launcher_foreground,
"Accept",
acceptPendingIntent
)
.setAutoCancel(true)
.build() // finalizes the creation
notificationManager.notify(Random.nextInt(), notification)
}
}
...also, is PendingIntent.getActivity() what I want or should I be using PendingIntent.getService()?
I'd like to bring up a notification with a button within the notification. When that button is pressed it should call a function elsewhere in my code. I've got the following code running but it doesn't make the call to the function when I press the button.
The code below brings up the notification and gives the user the option to select 'accept' but I can't figure out how to connect it to the function I want.
fun showActionNotification(
subscription: String,
challenge: String,
) {
val acceptIntent = Intent( context, ChallengesViewModel::class.java )
.apply{ /* Can I make a call to a function in ChallengesViewModel here? */ }
val acceptPendingIntent = PendingIntent.getActivity(context, 0, acceptIntent, PendingIntent.FLAG_IMMUTABLE)
val notification = NotificationCompat.Builder(context, "challenge_notification_id")
.setContentTitle(subscription)
.setContentText(challenge)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.addAction(
R.drawable.ic_launcher_foreground,
"Accept",
acceptPendingIntent
)
.setAutoCancel(true)
.build() // finalizes the creation
notificationManager.notify(Random.nextInt(), notification)
}
}
...also, is PendingIntent.getActivity() what I want or should I be using PendingIntent.getService()?
When that button is pressed it should call a function elsewhere in my code
There is no guarantee that your process is running when the user clicks the button. This is why Notification
does not offer a simple callback mechanism, but instead uses a PendingIntent
— we have to handle the case where we need to re-launch your app process.
I can't figure out how to connect it to the function I want
You need to rework your plans to fit within the PendingIntent
structure and Android's overall architecture.
is PendingIntent.getActivity() what I want or should I be using PendingIntent.getService()?
That depends on what you want to happen overall when the user clicks the button. If "the function" is tied to some part of your app UI, then using PendingIntent.getActivity()
to get you to your activity would be a reasonable choice. Your activity can then react to the existence of an Intent
extra or other signal that this button was clicked, and the activity can call your function.