I'm trying to use a MediaStyle notification in my Android app, with some action buttons added.

NotificationCompat.Builder myNotifBuilder;
// ...
androidx.media.app.NotificationCompat.MediaStyle ms = 
         new androidx.media.app.NotificationCompat.MediaStyle();                   
ms.setMediaSession(mSession.getSessionToken());
                myNotifBuilder.setStyle(ms)
                    .addAction(new NotificationCompat.Action(R.drawable.clipboard_speak,
                            "Paste", pIntent5))
                    .addAction(new NotificationCompat.Action(R.drawable.fb_on,
                            "Floating button", pIntent7));
// ...
myNotifBuilder.build();

The resulting notification contains the action buttons, but only as long as mSession.setPlaybackState(state) is NOT called. As soon as this code is called:

        PlaybackStateCompat state = new PlaybackStateCompat.Builder()
                .setState(isTalking() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED,
                        progress * 1000, 1f)
                .setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE
                                | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
                )
        try {
            mSession.setPlaybackState(state);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

the action buttons added with NotificationCompat.Builder() disappear and are replaced with ACTION_SKIP_TO_NEXT button, full width progress bar, then ACTION_SKIP_TO_NEXT button.

NOW, I KNOW that I can add buttons on the PlaybackStateCompat state.addCustomAction(). However on these buttons one CANNOT set a pending intent, like with .addAction() call of the notification builder. Both these buttons need to hide the notification shade panel and open my main activity. If I start the activity normally from my app code in the custom action button callback, the activity may open, but the notifications panel does not disappear, and it is critical that it did and let the user interact with the activity at once.

An alternative seems to be to never all the mSession.setPlaybackState(state); and do everything from the buttons added with .addAction() in the notification builder. But then there is no way to show the progress bar - it does not appear at all on Android 13, some progress bar appears on Android 14 to the left of the buttons, but there is no way to update the progress. It's a coding dead end...

UPDATE: Also submitted this as a bug report to Android issue tracker: https://issuetracker.google.com/issues/309068092

0

There are 0 answers