在本地通知中设置自定义声音

ant*_*009 4 notifications android

安卓13

创建带有自定义声音的本地通知

我的 event_sound.mp3 在我的res/raw/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()
Run Code Online (Sandbox Code Playgroud)

我有以下方法来提取数据模块中的声音文件。

 private fun getUriSoundFile(context: Context): Uri {
        val uri = Uri.parse("android.resource://" + "me.androidbox.data" + "/raw/" + "event_sound.mp3")

        return uri
    }
Run Code Online (Sandbox Code Playgroud)

当我检查完整路径时,我得到以下信息:

android.resource://me.androidbox.data/raw/event_sound.mp3
Run Code Online (Sandbox Code Playgroud)

我在我的应用程序类中设置了我的频道,如下所示:

  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)
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是,通知仅发出默认声音,而不会播放我的自定义声音。

更新====

我的应用程序类中有以下内容。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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

sno*_*lax 6

问题描述:Android 13 后,无论应用内选择如何,所有通知声音都设置为默认声音

确保您的通知声音在 res/raw 中为 mp3 格式。

您可以将其他部分保留原样。

新答案


private fun getUriSoundFile(context: Context): Uri {
        val uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.packageName + "/" + R.raw.event_sound.mp3)

        return uri
    }


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()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build()

            notificationChannel.setSound(getUriSoundFile(), audioAttributes)
            notificationManager.createNotificationChannel(notificationChannel)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

旧答案

private fun createNotificationChannel() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val audioAttributes = AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
                    .build()
    
                val uri = Uri.parse("android.resource://$packageName/${R.raw.sample_sound}")
    
                val channel = NotificationChannel(
                    CHANNEL_ID,
                    "Channel Name",
                    NotificationManager.IMPORTANCE_DEFAULT
                ).apply {
                    description = "Description"
                    setSound(uri, audioAttributes)
                }
                val notificationManager =
                    getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                notificationManager.createNotificationChannel(channel)
            }
        }
Run Code Online (Sandbox Code Playgroud)