首页 > 解决方案 > 当我使用 .setStyle 时,通知未显示在 android Oreo 中

问题描述

在 android Oreo 中创建通知时,一切正常,直到我使用 .setStyle 然后它停止工作

代码:

   } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        int importance = NotificationManager.IMPORTANCE_HIGH;

        {
            NotificationChannel mChannel = new NotificationChannel(
                    CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);

            assert notificationManager != null;
            notificationManager.createNotificationChannel(mChannel);
        }

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setContentTitle(track.getTitle()) 
                .setContentText(track.getArtist()) 
                .setSmallIcon(R.drawable.icon) 
                .setOnlyAlertOnce(true)//show notification for only first time
                .setLights(Color.CYAN, 500, 1200) //Setting the color of the led blink on the phone for notifications
                .addAction(drw_previous, "Previous", pendingIntentPrevious) //Setting action for the previous button
                .addAction(playbutton, "Play", pendingIntentPlay) //Setting the action for the play/pause button
                .addAction(drw_next, "Next", pendingIntentNext) //Setting the action for he next button
                .setDeleteIntent(pendingIntentDelete) //Setting delete intent for when the notification is destroyed/deleted
                .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                        .setShowActionsInCompactView(0, 1, 2)
                        .setMediaSession(mediaSessionCompat.getSessionToken())) //Setting how and what order the buttons will be shown
                .setShowWhen(false) //Do not show time for when this notification was created
                .setDefaults(Notification.DEFAULT_ALL); //Defaults

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addNextIntent(intent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
        );
        mBuilder.setContentIntent(resultPendingIntent);

        notificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());

    }

我是不是错过了什么。我拥有一切应该让它发挥作用的东西。它曾经可以工作,但我正在重新审视代码并对其进行更改并使其变得更好,并且不确定这次出了什么问题。感谢您的帮助!

标签: javaandroid

解决方案


推荐阅读