奥利奥通知频道听起来不起作用

时间:2018-05-15 07:12:58

标签: android android-8.0-oreo

使用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());

更新5/16/18:

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

更新5/29/18:

最新版本的Android 8.1.0解决了这个问题。

2 个答案:

答案 0 :(得分:0)

更改您的

do something after

像这样:

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

答案 1 :(得分:-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);
    }