Android 当使用引用资源 ID 的声音 URI 时,通知通道声音停止工作
Android Notification Channel sounds stop working when using sound URIs that reference resource ids
我们已经为 Oreo 及更高版本的设备 运行 创建了通知渠道,这些渠道使用位于我们 /res/raw
文件夹中的自定义通知声音。最近,当用户升级我们的应用程序时,通知声音刚刚停止工作,通知只振动设备。
我们已确认uninstall/reinstall或清除应用数据可以解决问题。但是,为了让通知声音再次为每个人工作而无需重新安装,我们需要删除并重新创建这些频道。
我们创建通知渠道如下:
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
文件夹中的一些 added/modified 文件。
似乎这个问题设置 soundUri
如下:
val soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
context.applicationContext.packageName + "/" + R.raw.notification)
看起来 R.raw.notification
的值从 2131689979
(有声音的版本)更改为 2131755515
(没有声音的版本)。由于您无法使用通知通道更改通知声音,我几乎可以肯定该通道正在尝试使用旧资源 ID (android.resource://our.package.name/2131689979
) 解析 soundUri
。
我认为更好的方法是直接按名称引用文件,如下所示:
val soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
context.applicationContext.packageName + "/raw/notification")
我还注意到 Facebook Messenger 和 Slack 等应用程序使用 public 通知文件夹,它们可能只是将文件复制过来并引用该确切路径。这似乎也允许用户重新select 应用程序提供的声音,因为它在文件系统中可见。
我们已经为 Oreo 及更高版本的设备 运行 创建了通知渠道,这些渠道使用位于我们 /res/raw
文件夹中的自定义通知声音。最近,当用户升级我们的应用程序时,通知声音刚刚停止工作,通知只振动设备。
我们已确认uninstall/reinstall或清除应用数据可以解决问题。但是,为了让通知声音再次为每个人工作而无需重新安装,我们需要删除并重新创建这些频道。
我们创建通知渠道如下:
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
文件夹中的一些 added/modified 文件。
似乎这个问题设置 soundUri
如下:
val soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
context.applicationContext.packageName + "/" + R.raw.notification)
看起来 R.raw.notification
的值从 2131689979
(有声音的版本)更改为 2131755515
(没有声音的版本)。由于您无法使用通知通道更改通知声音,我几乎可以肯定该通道正在尝试使用旧资源 ID (android.resource://our.package.name/2131689979
) 解析 soundUri
。
我认为更好的方法是直接按名称引用文件,如下所示:
val soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
context.applicationContext.packageName + "/raw/notification")
我还注意到 Facebook Messenger 和 Slack 等应用程序使用 public 通知文件夹,它们可能只是将文件复制过来并引用该确切路径。这似乎也允许用户重新select 应用程序提供的声音,因为它在文件系统中可见。