通知没有出现在 Android 11 (sdk 30)
Notification does not appear in Android 11 (sdk 30)
通知工作正常,出现了(sdk 28,android 9)。我决定在 android 11 的模拟器上尝试一下,结果没有出现通知。我无法理解我的错误。无处不在,在 sdk 28 下的 Xiomi 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);
}
通知显示在 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());
感谢解答,原来是另一个错误。这是风格——
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle().setMediaSession(new MediaSessionCompat(getBaseContext(), TAG).getSessionToken()))
你不能那样做(它只会让你达到 11 android)!你需要这样做:
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle())
通知工作正常,出现了(sdk 28,android 9)。我决定在 android 11 的模拟器上尝试一下,结果没有出现通知。我无法理解我的错误。无处不在,在 sdk 28 下的 Xiomi 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);
}
通知显示在 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());
感谢解答,原来是另一个错误。这是风格——
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle().setMediaSession(new MediaSessionCompat(getBaseContext(), TAG).getSessionToken()))
你不能那样做(它只会让你达到 11 android)!你需要这样做:
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle())