6

Ever since adding support for Android O, Android devices running O receive an alert (tone or vibration) whenever my app's media controls notification gets updated (metadata or playback state changes). I'm looking for a way to disable this alert since it's not applicable to media style notifications.

Here is the code I use to create a media controls 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 the showBadge to false on the notification channel doesn't appear to do anything. I still receive a badge on the app icon when the media controls notification is shown. So it looks like the notification channel attributes that I set are not being applied.

Ryan
  • 3,414
  • 2
  • 27
  • 34

1 Answers1

19

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

Android already reorders MediaStyle notifications higher in the tray, so using a higher importance is not necessary as it was on previous versions of Android.

NOTE: After changing the importance, you need to clear app data or reinstall the app in order to have this change take effect in notification channel.

rpattabi
  • 9,984
  • 5
  • 45
  • 53
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 1
    Hey @ianhanniballake. Thanks for the response. I tested different importance levels last night, including `IMPORTANCE_LOW`, to no avail. I still receive an alert tone on my Pixel running Oreo. – Ryan Sep 07 '17 at 13:26
  • 5
    Keep in mind that Notification channels are not editable after creation (except by the user). Try clearing data in your app to completely delete the channel. – ianhanniballake Sep 07 '17 at 14:10
  • Ah, makes sense. Clearing app data did the trick. Many thanks! – Ryan Sep 07 '17 at 15:04
  • If this helped answer your question, make sure to accept the answer to remove the question from the queue of unanswered questions. – ianhanniballake Sep 07 '17 at 15:05
  • Thanks @ianhaniballake, this solved my problem with my media style notification playing sounds all the time when the player state changes. – Andrew Lam Sep 17 '17 at 21:58
  • @ianhanniballake, please, pretty please, put your comment regarding clearing app data to you answer. It is really important: I've just lost two hours because I forgot to clear it. – David Jan 24 '18 at 21:13