java - 媒体样式 : RemoteServiceException: Bad notification posted from package

标签 java android notifications android-notifications huawei-mobile-services

我尝试使用下面的代码在我的应用中创建一个通知媒体 Controller ,该代码在除 华为 P8 LiteAndroid 5.0 之外的所有设备上都能正常工作,我从 Firebase 测试实验室收到此错误日志:

android.app.RemoteServiceException: Bad notification posted from package maa.app_app: Couldn't expand RemoteViews for: StatusBarNotification(pkg=maa.app_app user=UserHandle{0} id=555 tag=null score=10 key=0|maa.app_app|555|null|10108: Notification(pri=1 contentView=maa.app_app/0x109007f vibrate=null sound=null defaults=0x0 flags=0x62 color=0xffbfbfbf category=transport actions=2 vis=PUBLIC)) FATAL EXCEPTION: main Process: maa.app_app, PID: 18793 android.app.RemoteServiceException: Bad notification posted from package maa.app_app: Couldn't expand RemoteViews for: StatusBarNotification(pkg=maa.app_app user=UserHandle{0} id=555 tag=null score=10 key=0|maa.app_app|555|null|10108: Notification(pri=1 contentView=maa.app_app/0x109007f vibrate=null sound=null defaults=0x0 flags=0x62 color=0xffbfbfbf category=transport actions=2 vis=PUBLIC)) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1534) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5538) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

这是我的代码:

void startNotify(Context context, String playbackStatus, String title) {
    String titlesonge;
    String artist;
    try {
        titlesonge = StringUtils.substringBefore(title, " - ");
        artist = StringUtils.substringAfter(title, " - ");
    } catch (Exception e) {
        titlesonge = title.substring(0, title.indexOf(" - "));
        artist = title.substring(title.lastIndexOf(" - ") - 1);
    }
    int icon = R.drawable.ic_pause_white;
    Intent playbackAction = new Intent(service, RadioService.class);
    playbackAction.setAction(RadioService.ACTION_PAUSE);
    PendingIntent action = PendingIntent.getService(service, 1, playbackAction, 0);
    if (playbackStatus.equals(PlaybackStatus.PAUSED)) {
        icon = R.drawable.ic_play_white;
        playbackAction.setAction(RadioService.ACTION_PLAY);
        action = PendingIntent.getService(service, 2, playbackAction, 0);

    }
    Intent stopIntent = new Intent(service, RadioService.class);
    stopIntent.setAction(RadioService.ACTION_STOP);
    PendingIntent stopAction = PendingIntent.getService(service, 3, stopIntent, 0);

    Intent intent = new Intent(service, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP |
            Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(service, 0, intent, 0);
    notificationManager.cancel(NOTIFICATION_ID);
    String PRIMARY_CHANNEL = "PRIMARY_CHANNEL_ID";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager manager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
        String PRIMARY_CHANNEL_NAME = "PRIMARY";
        NotificationChannel channel = new NotificationChannel(PRIMARY_CHANNEL, PRIMARY_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        if (manager != null) {
            manager.createNotificationChannel(channel);
        }
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(service, PRIMARY_CHANNEL)
            .setAutoCancel(false)
            .setContentTitle(titlesonge)
            .setContentText(artist)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.largeicon))
            .setContentIntent(pendingIntent)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setSmallIcon(R.drawable.smallwidth)
            .setColor(ContextCompat.getColor(context, R.color.colorneeded))
            .addAction(icon, "pause", action)
            .addAction(R.drawable.ic_stop_white, "stop", stopAction)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setWhen(System.currentTimeMillis())
            .setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
                    .setMediaSession(service.getMediaSession().getSessionToken())
                    .setShowActionsInCompactView(0, 1)
                    .setShowCancelButton(true)
                    .setCancelButtonIntent(stopAction));
    service.startForeground(NOTIFICATION_ID, builder.build());
}

谁能帮我解决这个问题

最佳答案

出于某种原因,Android 5.0 的华为设备在使用 .setStyle() 方法时会崩溃,因此您有两种可能性:

1 - 检测设备是否为华为制造,是否为 Android 5.0 或更低版本

public boolean isLolliPopHuawei() {
    return (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");
}

2 - 使用 PlayerNotificationManager 代替

void exoPlayerNotification(Context context, SimpleExoPlayer exoPlayer, String title) {
    String titlesonge;
    String artist;
    try {
        titlesonge = StringUtils.substringBefore(title, " - ");
        artist = StringUtils.substringAfter(title, " - ");
    } catch (Exception e) {
        titlesonge = title.substring(0, title.indexOf(" - "));
        artist = title.substring(title.lastIndexOf(" - ") - 1);
    }
    String finalArtist = artist;
    String finalTitlesonge = titlesonge;
    mPlayerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
            context,
            "PRIMARY_CHANNEL_ID",
            R.string.plaza,
            NOTIFICATION_ID,
            new PlayerNotificationManager.MediaDescriptionAdapter() {
                @Override
                public String getCurrentContentTitle(Player player) {
                    return finalArtist;
                }

                @Nullable
                @Override
                public PendingIntent createCurrentContentIntent(Player player) {
                    Intent intent = new Intent(service, MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    return PendingIntent.getActivity(service, 0, intent,
                            PendingIntent.FLAG_UPDATE_CURRENT);
                }

                @Override
                public String getCurrentContentText(Player player) {
                    return finalTitlesonge;
                }

                @Nullable
                @Override
                public Bitmap getCurrentLargeIcon(Player player, PlayerNotificationManager.BitmapCallback callback) {
                    return BitmapFactory.decodeResource(service.getResources(), R.drawable.largeicon);
                }

                @Nullable
                @Override
                public String getCurrentSubText(Player player) {
                    return null;
                }
            }
    );
    mPlayerNotificationManager.setUseNavigationActions(false);
    mPlayerNotificationManager.setFastForwardIncrementMs(0);
    mPlayerNotificationManager.setRewindIncrementMs(0);
    mPlayerNotificationManager.setColorized(true);
    mPlayerNotificationManager.setColor(0xFFEEEEEE);
    mPlayerNotificationManager.setUseChronometer(true);
    mPlayerNotificationManager.setOngoing(true);
    mPlayerNotificationManager.setPriority(NotificationCompat.PRIORITY_MAX);
    mPlayerNotificationManager.setUsePlayPauseActions(true);
    mPlayerNotificationManager.setSmallIcon(R.drawable.smallwidth);
    mPlayerNotificationManager.setNotificationListener(new PlayerNotificationManager.NotificationListener() {
        @Override
        public void onNotificationStarted(int notificationId, Notification notification) {
            service.startForeground(notificationId, notification);
        }

        @Override
        public void onNotificationCancelled(int notificationId) {
            service.stopSelf();
            cancelNotify();
        }
    });
    mPlayerNotificationManager.setPlayer(exoPlayer);
}

关于java - 媒体样式 : RemoteServiceException: Bad notification posted from package,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55421493/

相关文章:

java - 将 DOM 解析器更改为不对空节点使用短符号

java - 将模型属性从 jsp 传递到 Spring Controller

ios - 区分何时调用 UIApplicationDidBecomeActiveNotification

安卓最高自定义通知的大小

Java 接口(interface)的目标注解

java - 使用 IntelliJ 调试 Java 进程 - 连接到套接字但不连接到目标 VM

javascript - React Native : <string>. matchAll 不是函数

Android 设备作为 A2DP 配置文件的接收器

android - 启动进程 'command '\Android Studio\jre\bin\java.exe时出现问题''

ios - 屏幕锁定时的用户通知