Android O中的媒体控制通知会发出警报

6
自从添加了对Android O的支持,运行O版本的Android设备在我的应用程序媒体控制通知更新(元数据或播放状态更改)时会收到警报(音调或振动)。我正在寻找一种方法来禁用此警报,因为它不适用于媒体样式通知。
以下是我用来创建媒体控制通知的代码:
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似乎没有任何作用。当媒体控制通知显示时,应用程序图标仍然会显示徽章。因此,看起来我设置的通知频道属性没有被应用。

1个回答

19
根据将Migrating MediaStyle通知迁移到Android O, 您应该使用IMPORTANCE_LOW,它不包含任何声音-IMPORTANCE_HIGH通道与声音相关联。
Android已经在托盘中重新排列了MediaStyle通知,因此在之前的Android版本上使用更高的重要性是不必要的。
注意:更改重要性后,您需要清除应用程序数据或重新安装应用程序,以便在通知通道中生效。

1
嗨,@ianhanniballake。感谢您的回复。昨晚我尝试了不同的重要级别,包括“IMPORTANCE_LOW”,但仍然不能生效。在运行 Oreo 的 Pixel 上,我仍然会收到警报声。 - Ryan
5
请记住,通知渠道在创建后无法编辑(用户除外)。尝试清除应用程序中的数据以完全删除该渠道。 - ianhanniballake
啊,明白了。清除应用程序数据解决了问题。非常感谢! - Ryan
如果这个回答解决了您的问题,请确保接受答案,以从未回答问题的队列中移除该问题。 - ianhanniballake
感谢@ianhaniballake,这解决了我的问题,我的媒体样式通知在播放器状态改变时一直播放声音。 - Andrew Lam
@ianhanniballake,请问您能否在您的答案中加入有关清除应用数据的评论?这非常重要:我刚刚因为忘记清除它而浪费了两个小时。 - David

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接