android - Kotlin+Compose: can I call a function from within a notification action? - Stack Overflow

admin2025-05-01  1

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

Share Improve this question asked Jan 2 at 20:34 FriendlyUser3FriendlyUser3 552 silver badges5 bronze badges 1
  • It would be better if you explained what you want to achieve. For now, it looks like senseless. In current case can use intent to run some activity(usually main activity in compose project) then handle intent in main activity and make a call of you ViewModel fun. For now as I see you don't understand what is intent and how it works. – Eugene Troyanskii Commented Jan 2 at 22:24
Add a comment  | 

1 Answer 1

Reset to default 1

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.

转载请注明原文地址:http://anycun.com/QandA/1746097790a91634.html