MediaStyle : RemoteServiceException: Bad notification posted from package

后端 未结 3 1341
轻奢々
轻奢々 2021-01-25 12:21

I\'m trying to create a notification media controller in my app using the code below which is working fine on all devices expect Huawei P8 Lite with An

3条回答
  •  礼貌的吻别
    2021-01-25 12:45

    I had the same issue and the root cause was not able to setStyle, it was only giving this exception in Huawei P8 Lite, not on other devices.

    So, what i had to do was check if current device is android version 5.0 and its manufacturer is Huawei and remove the setStyle property. please check below code

    boolean isLollipopHuawei = (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1 ||
                android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) && Build.MANUFACTURER.equalsIgnoreCase("HUAWEI");
    
    
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1 || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
    
                if (isLollipopHuawei) {
    
                    return builder
                            .addAction(generateAction(android.R.drawable.ic_media_previous, "Previous", Constants.ACTION_PREVIOUS))
                            .addAction(action)
                            .addAction(generateAction(android.R.drawable.ic_media_next, "Next", Constants.ACTION_NEXT))
                            .setSmallIcon(R.mipmap.vpicon_grayscale)
                            .setContentTitle(getSongDataHelper().getTitle())
                            .setContentIntent(pendingIntent)
                            .setContentText(getSongDataHelper().getAlbum())
                            .setLargeIcon(getSongDataHelper().getAlbumArt())
                            //.setColor(color)
                          /*  .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                                    .setShowActionsInCompactView(0, 1, 2)
                                    .setMediaSession(mMediaSession.getSessionToken()))*/
                            .build();
                } else {
                    return builder
                            .addAction(generateAction(android.R.drawable.ic_media_previous, "Previous", Constants.ACTION_PREVIOUS))
                            .addAction(action)
                            .addAction(generateAction(android.R.drawable.ic_media_next, "Next", Constants.ACTION_NEXT))
                            .setSmallIcon(R.mipmap.vpicon_grayscale)
                            .setContentTitle(getSongDataHelper().getTitle())
                            .setContentIntent(pendingIntent)
                            .setContentText(getSongDataHelper().getAlbum())
                            .setLargeIcon(getSongDataHelper().getAlbumArt())
                            //.setColor(color)
                            .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                                    .setShowActionsInCompactView(0, 1, 2)
                                    .setMediaSession(mMediaSession.getSessionToken()))
                            .build();
                }
            }
    

提交回复
热议问题