【问题标题】:Notification does not appear in Android 11 (sdk 30)通知不会出现在 Android 11 (sdk 30)
【发布时间】:2021-11-06 09:19:04
【问题描述】:

通知工作正常,出现(sdk 28,android 9)。我决定在 android 11 的模拟器上试一试,结果没有出现通知。我无法理解我的错误。适用于任何地方,在 sdk 28 下的小米 Redmi 7 和 Pixel 4 上进行了测试。在 sdk 30 下的 Nexus 5 上,该应用程序可以正常工作,但只是没有通知。

private void createNotificationChannel(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel1 = new
                    NotificationChannel(CHANNEL_ID_1, "Channel(1)", NotificationManager.IMPORTANCE_HIGH);
            channel1.setDescription("Channel 1 Dec..");

            NotificationChannel channel2 = new
                    NotificationChannel(CHANNEL_ID_2, "Channel(2)", NotificationManager.IMPORTANCE_HIGH);
            channel2.setDescription("Channel 2 Dec..");

            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel1);
            notificationManager.createNotificationChannel(channel2);
        }
    }

void showNotification(int playPauseBtn){
        
        ...

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID_2)
                .setSmallIcon(playPauseBtn)
                .setLargeIcon(thumb)
                .setContentTitle(musicFiles.get(position).getTitle())
                .setContentText(musicFiles.get(position).getArtist())
                .addAction(R.drawable.ic_skip_previous, "Previous", prevPending)
                .addAction(playPauseBtn, "Pause", pausePending)
                .addAction(R.drawable.ic_skip_next, "Next", nextPending)
                .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                        .setMediaSession(new MediaSessionCompat(getBaseContext(), TAG).getSessionToken()))
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(contentPending)
                .setDeleteIntent(deletePending)
                .setOnlyAlertOnce(true)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .build();
        startForeground(2, notification);
    }

【问题讨论】:

  • 您应该发布我们可以复制和粘贴的代码进行试用。这里有很多是未定义的。

标签: java android kotlin notifications


【解决方案1】:

感谢您的回答,但错误原来是在另一个。这是风格 - .setStyle(new androidx.media.app.NotificationCompat.MediaStyle().setMediaSession(new MediaSessionCompat(getBaseContext(), TAG).getSessionToken()))

你不能这样做(它最多只能带你 11 个 android)!你需要这样做: .setStyle(new androidx.media.app.NotificationCompat.MediaStyle())

【讨论】:

    【解决方案2】:

    通知在 android 11 中显示,您可以进行相应的自定义。我已经测试了这段代码,并且在 android 11 模拟器中运行良好。

    NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_ID_2)
                    .setSmallIcon(playPauseBtn)
                    .setLargeIcon(thumb)
                    .setContentTitle("Title")
                    .setContentText("Text")
                    .setStyle(new NotificationCompat.BigPictureStyle()
                            .bigPicture(imgUrl)
                            .bigLargeIcon(null))
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setContentIntent(pendingIntent)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                notification.setSmallIcon(playPauseBtn);
                notification.setColor(getResources().getColor(R.color.colorPrimary));
            } else {
                notification.setSmallIcon(playPauseBtn);
            }
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
    
            int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
            notificationManager.notify(m,notification.build());
    

    【讨论】:

    • 感谢您的回答,但不幸的是它对我没有帮助。我看不出你的代码和我的代码有什么区别。可能是什么错误?我已经在两个 android 11 模拟器上测试过了。
    猜你喜欢
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 2019-02-10
    • 2021-06-30
    • 2015-04-09
    • 1970-01-01
    相关资源
    最近更新 更多