Android Notification Channel sounds stop working when using sound URIs that reference resource ids

前端 未结 1 1562
春和景丽
春和景丽 2020-12-11 23:23

We have created notification channels for devices running on Oreo and above, that use a custom notification sound that is located in our /res/raw folder. Recent

相关标签:
1条回答
  • 2020-12-12 00:06

    It seems like this issue setting the soundUri as follows:

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

    It looks like the value of R.raw.notification changed from 2131689979 (version where sound works) to 2131755515 (version where sound doesn't work). And since you can't change your notification sound with Notification Channels, I am almost certain that the channel is trying to resolve the soundUri with the old resource id (android.resource://our.package.name/2131689979).

    I think the better approach is to reference the file directly by name as follows:

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

    I also notice apps like Facebook Messenger and Slack use a public Notifications folder where they probably just copy the file over and reference that exact path. This also seems to allow the user to re-select the app-provided sound because it is visible in the filesystem.

    0 讨论(0)
提交回复
热议问题