gpt4 book ai didi

Android Oreo 不播放自定义通知声音

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:30:03 26 4
gpt4 key购买 nike

我正在尝试为 API > 26 添加自定义声音通知。下面是代码

NotificationChannel notificationChannel = new NotificationChannel("channel id","channel name",NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(notificationChannel);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
notificationChannel.setSound(Uri.parse("android.resource://" + BuildConfig.APPLICATION_ID + "/raw/beep"),audioAttributes);

这里的问题是它播放设备的默认钢琴声,而不是播放 Assets 中的哔哔声。我不允许使用铃声管理器,但常识统计通知声音应该是指定的而不是默认的。

它适用于 API <= 26

最佳答案

最后我设法自己找到了解决方案。下面是代码

NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

if(notificationSoundUri != null){
// Changing Default mode of notification
notificationCompatBuilder.setDefaults(Notification.DEFAULT_VIBRATE);

// Creating an Audio Attribute
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();

// Creating Channel
NotificationChannel notificationChannel = new NotificationChannel(context.getString(R.string.channel_id_prayers),context.getString(R.string.channel_name_prayers),NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(notificationSoundUri,audioAttributes);
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
mNotificationManager.notify(0, notificationCompatBuilder.build());

关于Android Oreo 不播放自定义通知声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50062576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com