媒体控制通知在 Android O 上发出警报

Media controls notification emits alert on Android O

自从添加了对 Android O、Android 设备的支持后 运行 O 每当我的应用程序的媒体控制通知更新(元数据或播放状态)时都会收到警报(音调或振动)变化)。我正在寻找禁用此警报的方法,因为它不适用于媒体样式通知。

这是我用来创建媒体控件通知的代码:

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);
    }
}

这是我用来创建通知渠道的代码:

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);
    }
}

更新:在通知渠道上将showBadge设置为false似乎没有任何作用。当显示媒体控件通知时,我仍然会在应用程序图标上收到徽章。所以看起来我设置的通知通道属性没有被应用。

根据 Migrating MediaStyle notifications to Android O,您应该使用 IMPORTANCE_LOW,它不包含任何声音 - IMPORTANCE_HIGH 频道有与之关联的声音。

Android 已经将 MediaStyle 通知重新排列在托盘中更高的位置,因此没有必要像以前版本的 Android.

那样使用更高的重要性

注意:更改重要性后,您需要清除应用数据或重新安装应用才能使此更改在通知渠道中生效。