Звук канала оповещения Oreo не работает - PullRequest
0 голосов
/ 15 мая 2018

Используя следующий код из Документов для разработчиков Android Я не могу получить звук, работающий в симуляторе 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 голосов
/ 30 мая 2018

Вы должны установить звук в 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);
    }
0 голосов
/ 15 мая 2018

Измените

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