0

I am creating notification with button:

val builder = NotificationCompat.Builder(MyApp.appContext, FCM_CHANNEL)
                    .setSmallIcon(R.drawable.noticon)
                    .setLargeIcon(image)
                    .setContentTitle(user?.name)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setAutoCancel(true)

And adding pendingIntent:

val responseIntent = Intent(MyApp.appContext, ResponseBroadcastReceiver::class.java)
responseIntent.putExtra(ResponseBroadcastReceiver.USER_ID, fromUid)

  val flags: Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
                            PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_ONE_SHOT
                        else
                            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_ONE_SHOT
                        
val pendingIntent = PendingIntent.getBroadcast(MyApp.appContext, abs(fromUid.hashCode()), responseIntent, flags)

builder.addAction(0, "Reply", pendingIntent)
builder.setContentText("Please respond")

I have couple of questions:

  1. Because for each notification I create new pendingIntent how can I list all of them to see which active? As I understand I am limited to 35 (what happens after 35?) https://stackoverflow.com/a/20205696
  2. Does adding "setAutoCancel(true)" to NotificationCompat.Builder removes the pendingIntent when notification dismissed (that is what I want)
  3. I wish that bending intent only be triggered once (PendingIntent.FLAG_ONE_SHOT) and Mutable, is this the correct approach for handling different SDK versions?
Dim
  • 4,527
  • 15
  • 80
  • 139

1 Answers1

-2
  1. Listing all active PendingIntents: Android does not provide a native way to list all active PendingIntents. The reason is that the PendingIntent is a token that the application gives to another application (e.g., Notification Manager), and the other application uses that token to perform an operation on behalf of the application that owns the PendingIntent. The PendingIntent is not tracked by the system, so there is no central registry to query for active PendingIntents. However, you can manage the active PendingIntents within your app manually. For example, you can store them in a data structure like a list or a map when you create them and remove them when they are used or no longer needed.

  2. setAutoCancel(true) and PendingIntent: The setAutoCancel(true) method in NotificationCompat.Builder will only dismiss the notification when the user clicks on it. It does not explicitly remove or cancel the PendingIntent associated with the notification or its actions. However, since you're using PendingIntent.FLAG_ONE_SHOT, the PendingIntent can only be used once. After it has been used, it will be automatically canceled by the system and cannot be used again.

  3. FLAG_ONE_SHOT and Mutable PendingIntent: Your approach to handling different SDK versions is correct. Using PendingIntent.FLAG_ONE_SHOT ensures that the PendingIntent can only be used once. For SDK versions S (Android 12) and above, you are also using PendingIntent.FLAG_MUTABLE, which means that the PendingIntent's extras can be updated. For SDK versions below S, you're using PendingIntent.FLAG_UPDATE_CURRENT, which allows updating the PendingIntent if it already exists.

In summary, your approach for handling different SDK versions and making the PendingIntent one-shot and mutable is correct. Just remember that you'll need to manage the active PendingIntents within your app if you need to keep track of them.

weirdgyn
  • 886
  • 16
  • 47