Media Notification issues a warning on Android O

From the moment you add support for Android O, Android devices that work with O receive a warning (tone or vibration) when they change the notification of events related to the notification of the application (changes in metadata or changes in playback status). I am looking for a way to disable this warning, as it does not apply to multimedia-style notifications.

Here is the code I use to create a media notification:

MediaDescriptionCompat description = metadata.getDescription(); String artistName = metadata.getString(MediaMetadataCompat.METADATA_KEY_ARTIST); String albumName = metadata.getString(MediaMetadataCompat.METADATA_KEY_ALBUM); Bitmap largeIcon = BitmapFactory.decodeResource(playbackService.getResources(), R.drawable.vector_global_defaultsong); NotificationCompat.Builder notificationBuilder = new NotificationCompat .Builder(playbackService, NotificationHelper.CHANNEL_MEDIA_CONTROLS) .setColor(ContextCompat.getColor(playbackService, R.color.colorAccent)) .setSmallIcon(R.drawable.logo_light_filled) .setContentTitle(description.getTitle()) .setContentText(playbackService.getString(R.string.song_list_subtitle_format, artistName, albumName)) .setContentIntent(createContentIntent()) .setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(playbackService, PlaybackStateCompat.ACTION_STOP)) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setOngoing(playbackState.getState() == PlaybackStateCompat.STATE_PLAYING) .setLargeIcon(largeIcon); notificationBuilder.setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle() // show previous, play/pause, and next in compact view .setShowActionsInCompactView(addActions(notificationBuilder)) .setMediaSession(sessionToken)); showNotification(notificationBuilder.build()); 

.,.

 private void showNotification(final Notification notification) { if (!started) { mediaController.registerCallback(mediaControllerCallback); playbackService.startForeground(NOTIFICATION_ID, notification); started = true; } else { notificationManager.notify(NOTIFICATION_ID, notification); } if (playbackState.getState() == PlaybackStateCompat.STATE_PAUSED) { playbackService.stopForeground(false); } } 

Here is the code I use to create the notification channel:

 public static final String CHANNEL_MEDIA_CONTROLS = "media_controls"; public static void createNotificationChannels(Context context) { if (android.os.Build.VERSION.SDK_INT >= 26) { NotificationChannel mediaControlsChannel = new NotificationChannel(CHANNEL_MEDIA_CONTROLS, context.getString(R.string.notification_channel_media_controls), NotificationManager.IMPORTANCE_HIGH); mediaControlsChannel.setShowBadge(false); getNotificationManager(context).createNotificationChannel(mediaControlsChannel); } } 

Update : setting showBadge to false in the notification channel does not work. I still get the icon on the application icon when a media notification is displayed. Thus, it looks like the notification channel attributes that I set are not applied.

+5
source share
1 answer

In Migrating MediaStyle notifications to Android O , you should use IMPORTANCE_LOW , which does not contain sound - IMPORTANCE_HIGH channels have the sound associated with them.

Android is already reordering MediaStyle notifications higher in the tray, so the use of higher importance is not required, as it was in previous versions of Android.

+9
source

Source: https://habr.com/ru/post/1271589/


All Articles