简体   繁体   English

将通知声音设置为默认警报铃声

[英]Set Notification sound as default alarm ringtone

What I've discovered is, that if I set notification sound as devices default alarm ringtone like this: 我发现的是,如果我将通知声音设置为设备默认警报铃声,例如:

val alarmTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
val builder = NotificationCompat.Builder(
        context,
        CHANNEL_ID
)
builder.setDefaults(Notification.DEFAULT_VIBRATE or Notification.DEFAULT_LIGHTS)
builder.priority = NotificationCompat.PRIORITY_DEFAULT
builder.setSound(alarmTone)

This works on almost all older device versions, but as soon as I test it on Android 8.0 device, it sets sound as default notification sound. 这几乎可以在所有较旧的设备版本上使用,但是一旦我在Android 8.0设备上对其进行测试,它将声音设置为默认通知声音。 How can I get default alarm ringtone for devices 8.0? 如何获取设备8.0的默认警报铃声?

After 8.0 Oreo You may have created the channel for play notification sound. 8.0 Oreo之后,您可能已经创建了播放通知声音的通道。

private static void initChannels(NotificationManager notificationManager) {
    if (Build.VERSION.SDK_INT < 26) {
        return;
    }

    NotificationChannel channel = new NotificationChannel("ID",
            "NAME",
            NotificationManager.IMPORTANCE_LOW);
    channel.setDescription("DESC");
    channel.enableVibration(false);
    AudioAttributes audioAttributes = new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
            .build();
    channel.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification), audioAttributes);

    notificationManager.createNotificationChannel(channel);
}

Make sure you are using targetSdkVersion 26 or above 确保您使用的是targetSdkVersion 26或更高版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM