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

自从添加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似乎不执行任何操作.当显示媒体控件通知时,我仍会在应用程序图标上收到徽章.所以看起来我设置的通知通道属性没有被应用.

解决方法

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

Android已经在托盘中重新排序了更高的MediaStyle通知,因此不需要使用更高的重要性,就像在以前的Android版本中一样.

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

相关文章

Android 通过adb shell命令查看内存,CPU,启动时间,电量等...
Monkey Android app稳定性测试工具之Monkey使用教程 by:授客...
Android 常见adb命令 by:授客 QQ:1033553122 1、 查看所有已...
这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...