Notification in Android 11 (sdk 30)
-
Notification worked great, showed up (sdk 28, android 9). I decided to try the emul emulsion under 11 androids, as a consequence of the notification, the notification would not appear.
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); }
-
build.grandle SDK didn't you forget to fix it?
compileSdkVersion 31 minSdkVersion 25 targetSdkVersion 31
Android 12 works.
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) { }