【问题标题】:Oreo Notification Channel Sounds Not Working奥利奥通知频道听起来不工作
【发布时间】:2019-05-10 02:53:40
【问题描述】:

使用Android Developer docs 中的以下代码,我无法在 API 27 (Android O) 模拟器中获得声音。它适用于 API 24 设备。我还仔细检查了通知设置,通知通道设置为播放默认声音。

这是一个包含以下示例代码的项目,您可以在模拟器上试用:Github

NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

String channelId = "test-channel";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel newIncidentChannel = new NotificationChannel(channelId,
            "Test Channel",
            NotificationManager.IMPORTANCE_HIGH);
    notificationManager.createNotificationChannel(newIncidentChannel);
}

NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle("Test")
                .setContentText("Text")
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true);

int NOTIFICATION_ID = (int) (System.currentTimeMillis()%10000);
notificationManager.notify("test", NOTIFICATION_ID, notificationBuilder.build());

2018 年 5 月 16 日更新:

我在这里使用解决方案:https://stackoverflow.com/a/46862503/817886 在收到通知时使用媒体播放来播放声音。不理想,但在找到合适的解决方案之前使用它。

2018 年 5 月 29 日更新:

最新版本的 Android 8.1.0 已修复此问题。

【问题讨论】:

  • 我遇到了同样的问题,并且阅读了几乎所有关于这个声音问题的答案。终于听到提示音了。问题是,如果您订阅了任何频道并且修改/编辑此频道,则通知声音不起作用。只需删除已安装的应用程序并重新安装

标签: android android-8.0-oreo


【解决方案1】:

你应该在 NotificationChannel 中设置声音而不是在 NotificaitonBuilder 中

举例

Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.notification_mp3);

String channelId = "test-channel";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        NotificationChannel mChannel = new NotificationChannel(channelId ,
            "Test Channel",
            NotificationManager.IMPORTANCE_DEFAULT)

        AudioAttributes attributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();

        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, 
                context.getString(R.string.app_name),
                NotificationManager.IMPORTANCE_HIGH);

        // Configure the notification channel.
        mChannel.setDescription(msg);
        mChannel.enableLights(true);
        mChannel.enableVibration(true);
        mChannel.setSound(sound, attributes); // This is IMPORTANT


        if (mNotificationManager != null)
            mNotificationManager.createNotificationChannel(mChannel);
    }

【讨论】:

    【解决方案2】:

    改变你的

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.notification_icon)
                    .setContentTitle("Test")
                    .setContentText("Text")
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setAutoCancel(true);
    

    像这样:

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.notification_icon)
                    .setContentTitle("Test")
                    .setContentText("Text")
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setSound(RingtoneManager
                    .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).
                    .setAutoCancel(true);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    相关资源
    最近更新 更多