当我使用.setstyle时,android oreo中没有显示通知

dced5bon  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(197)

在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());

    }

我错过了什么吗。我有一切可以让它工作的东西。它以前是有效的,但我正在重新检查代码,修改它并使之更好,不确定这次出了什么问题。谢谢你的帮助!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题