机器人13
使用自定义声音创建本地通知
我的res/raw/event_sound.mp3
中有Event_sound.mp3
我收到以下通知
val notification = NotificationCompat.Builder(context, channelId)
.setContentTitle(title)
.setContentText(description)
.setSmallIcon(R.drawable.bell)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setSound(getUriSoundFile(context))
.build()
我有以下方法来提取我的数据模块中的声音文件.
private fun getUriSoundFile(context: Context): Uri {
val uri = Uri.parse("android.resource://" + "me.androidbox.data" + "/raw/" + "event_sound.mp3")
return uri
}
当我判断完整路径时,我得到以下结果:
android.resource://me.androidbox.data/raw/event_sound.mp3
我在我的应用程序类中设置了如下频道:
private fun createNotificationChannel(listOfChannel: Map<String, String>) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
listOfChannel.map { mapOfChannel ->
val notificationManager = getNotificationManager()
val notificationChannel = NotificationChannel(
mapOfChannel.key,
mapOfChannel.value,
NotificationManager.IMPORTANCE_HIGH
)
notificationManager.createNotificationChannel(notificationChannel)
}
}
}
然而,通知只会发出默认的声音,而不会播放我的自定义声音.
==引用=外部链接==
在我的应用程序课上,我有以下内容.EVENT_SOUND直接出现在以下内容中
raw/event_sound.mp3
个
这是调试输出
android.resource://me.androidbox.presentation/2131558400
个
这是我的更新代码:
private fun createNotificationChannel(listOfChannel: Map<String, String>) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
listOfChannel.map { mapOfChannel ->
val notificationManager = getNotificationManager()
val notificationChannel = NotificationChannel(
mapOfChannel.key,
mapOfChannel.value,
NotificationManager.IMPORTANCE_HIGH
)
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
.build()
notificationChannel.setSound(getUriSoundFile(), audioAttributes)
notificationManager.createNotificationChannel(notificationChannel)
}
}
}