Why I got exception Bad notification posted when i use builder.setStyle(mediaStyle)

0

I want to use NotificationCompat.Builder to create a notification with mediaStyle.The problem is once I use builder.setStyle(mediaStyle) and then it throws a exception like android.app.RemoteServiceException: Bad notification posted from package the whole information:

android.app.RemoteServiceException: Bad notification posted from package com.hanjiu.mplayer: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.hanjiu.mplayer user=UserHandle{0} id=219 tag=null key=0|com.hanjiu.mplayer|219|null|10079 keyguardnotification=false : Notification(pri=0 contentView=null headsUpContentView=null bigContentView=null vibrate=null sound=null defaults=0x0 flags=0x62 color=0x00000000 category=transport actions=1 vis=PUBLIC))
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1877)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:203)
        at android.app.ActivityThread.main(ActivityThread.java:6560)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:974)

the original codes are following here:

NotificationCompat.Builder getBuilder(MediaMetadataCompat metadata,PlaybackStateCompat state,
                                                  boolean isPlaying,MediaSessionCompat.Token token){
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            notificationManager.createNotificationChannel(new NotificationChannel(SERVICE_CHANNEL_ID,SERVICE_CHANNEL_NAME,NotificationManager.IMPORTANCE_LOW));
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,SERVICE_CHANNEL_ID);

        androidx.media.app.NotificationCompat.MediaStyle mediaStyle = new androidx.media.app.NotificationCompat.MediaStyle();
        mediaStyle.setMediaSession(token)
                .setShowActionsInCompactView(0,1,2)
                .setShowCancelButton(true)
                .setCancelButtonIntent(
                        MediaButtonReceiver.buildMediaButtonPendingIntent(
                                context,
                                PlaybackStateCompat.ACTION_STOP)
                );


        if ((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0) {
            builder.addAction(mPrevAction);
        }

        builder.addAction(isPlaying ? mPauseAction : mPlayAction);


        if ((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0) {
            builder.addAction(mNextAction);
        }

        builder.setStyle(mediaStyle)
                .setOngoing(true)
                .setContentTitle(metadata.getString(MediaMetadataCompat.METADATA_KEY_TITLE))
                .setContentText(metadata.getString(MediaMetadataCompat.METADATA_KEY_ARTIST))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(metadata.getBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART))
                .setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(
                        context, PlaybackStateCompat.ACTION_STOP))
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        return builder;
    }

when I ran it on virtual devices(android 9 aosp,api 28) it won't throw exception and there was no notification

when I removed .setMediaSession(token) and ran it on a my phone(system:MEIZU flyme 8 based on android 7) it worked well but it surely didn't have media style.

I did really want to have figure it out

android
android-notifications
android-notification.mediastyle
asked on Stack Overflow May 13, 2020 by HHao

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0