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

问题描述 投票:0回答:1

我们为在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)
        }
    }

我已经验证该文件仍然存在于/res/raw中。而似乎导致这种情况的提交只是在/res文件夹中添加/修改了一些文件。

android android-notifications android-8.0-oreo android-9.0-pie
1个回答
2
投票

似乎这个问题设置soundUri如下:

val soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
                    context.applicationContext.packageName + "/" + R.raw.notification)

看起来R.raw.notification的值从2131689979(声音工作的版本)变为2131755515(声音不起作用的版本)。由于您无法使用通知频道更改通知声音,因此我几乎可以确定该频道正在尝试使用旧资源ID(soundUri)解析android.resource://our.package.name/2131689979

我认为更好的方法是直接按名称引用文件,如下所示:

val soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
                    context.applicationContext.packageName + "/raw/notification")

我还注意到像Facebook Messenger和Slack这样的应用程序使用公共Notifications文件夹,他们可能只是复制文件并引用该确切路径。这似乎也允许用户重新选择应用程序提供的声音,因为它在文件系统中可见。

© www.soinside.com 2019 - 2024. All rights reserved.