PendingIntent.getBroadcast() not working in Android 11

Viewed 708

What I want: I want to perform a certain action when the user manually removes my App's notification.

How I implemented it: I implemented this by creating a PendingIntent that will perform a broadcast using PendingIntent.getBroadcast(). I then pass this intent to the setDeleteIntent() method of the Notification.Builder.

This was working fine on previous Android versions, but in Android 11 the Broadcast is not being sent (or at least it is not received by my receivers). I assume the problem is either with PendingIntent.getBroadcast() (seems more likely to me) or with setDeleteIntent().

Hint 1: I made sure that my BroadcastReceivers are working fine: when I'm performing a broadcast with sendBroadcast(new Intent(BROADCAST_REMOVE_NOTIFICATION)); it works perfectly fine.

Hint 2: I just noticed that in a state where my App's notification was previously not removable (during playback of an audio file, where I started the player service as a foreground service) it is now removable in Android 11. Not sure if this has something to do with above problem though.

I have no idea what might cause this difference in behavior between Android 11 and previous versions. Can anybody help?

// Set up intent to stop service when notification is removed
Intent intent = new Intent(BROADCAST_REMOVE_NOTIFICATION);
PendingIntent deleteIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);

// Create a new notification
mNotificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
        // Hide the timestamp
        .setShowWhen(false)
        // Set the notification style
        .setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
                // Attach our MediaSession token
                .setMediaSession(mediaSession.getSessionToken())
                // Show our playback controls in the compat view
                .setShowActionsInCompactView(0, 1, 2))
        // Set the notification color
        .setColor(getResources().getColor(R.color.colorAccent))
        // Set the large and small icons
        .setLargeIcon(notificationCover)
        .setSmallIcon(R.drawable.ic_notification_new)
        // Set notification content information
        .setContentText(mActiveAudio.getAlbumTitle())
        .setContentTitle(audioTitle)
        // Set the intent for the activity that is launched on click
        .setContentIntent(launchIntent)
        // Set intent that is launched on delete notificaiton
        .setDeleteIntent(deleteIntent)
        // Set the visibility for the lock screen
        .setVisibility(VISIBILITY_PUBLIC)
        // Make notification non-removable if the track is currently playing
        .setOngoing(title.equals(getString(R.string.button_pause)))
        // Add playback actions
        .addAction(R.drawable.ic_media_backward, getString(R.string.button_backward), playbackAction(3))
        .addAction(notificationAction, title, play_pauseAction)
        .addAction(R.drawable.ic_media_forward, getString(R.string.button_forward), playbackAction(2));
0 Answers
Related