Не появляется Notification в Android 11 (sdk 30)
Notification работал отлично, появлялся (sdk 28, android 9). Решила попробовать на эмуляторе под 11 андроид, как следствие уведомления не появляются.
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);
}
Ответы (2 шт):
Автор решения: Arty Morris
→ Ссылка
build.grandle SDK не забыли поправить?
compileSdkVersion 31
minSdkVersion 25
targetSdkVersion 31
На Андроид 12 работает
try {
notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel_main = new NotificationChannel("ID", "VestaXR_main", NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(notificationChannel_main);
builder_main = new NotificationCompat.Builder(this, notificationChannel_main.getId())
.setContentTitle("")
.setSound(null)
.setSmallIcon(R.drawable.ic_stat_onesignal_default)
.setContentText("");
} else {
builder_main = new NotificationCompat.Builder(this);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(1, builder_main.build());
}
} catch (RuntimeException e) {
}
Автор решения: Alex Goodman
→ Ссылка
Спасибо за ответ, но ошибка оказалась в другом.
Это находится в стиле:
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setMediaSession(new MediaSessionCompat(
getBaseContext(), TAG).getSessionToken()))
Так делать нельзя, прокатит только до 11 андроид. Нужно сделать так:
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle())