小编Ste*_*tes的帖子

使用引用资源 ID 的声音 URI 时,Android 通知通道声音停止工作

我们为在 Oreo 及更高版本上运行的设备创建了通知渠道,这些渠道使用位于我们/res/raw文件夹中的自定义通知声音。最近,当用户升级我们的应用程序时,通知声音停止工作,通知只会振动设备。

我们已确认卸载/重新安装或清除应用程序数据可解决此问题。但是,为了在无需重新安装的情况下再次为所有人提供通知声音,我们需要从本质上删除并重新创建这些频道。

我们创建通知通道如下:

fun initNotificationChannel(channel: PSSNotificationChannel) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val id = channel.id
            val name = context.getString(channel.nameResId)
            val importance = channel.importance
            val channel = NotificationChannel(id, name, importance)

            ...

            // Default sound
            val soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
                    context.applicationContext.packageName + "/" + R.raw.notification)
            val audioAttributes = AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .build()
            channel.setSound(soundUri, audioAttributes)

            val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
            notificationManager?.createNotificationChannel(channel)
        }
    }
Run Code Online (Sandbox Code Playgroud)

我已经验证该文件仍然存在于/res/raw. 似乎导致此问题的提交只是文件/res夹中添加/修改的一些文件。

android android-notifications android-8.0-oreo android-9.0-pie

3
推荐指数
1
解决办法
866
查看次数