In my hybrid Cordova Android app targeting API 23+ I want to use a custom sound for notifications. To that end I have done the following
- In
plugin.xml
file for the single custom plugin I use in the app I declare<resource-file src="src/android/res/unysound.mp3" target="res/raw/mysound.mp3" />'
.
Opening the APK as a zip archive I see that the mp3 file has in fact ended up in `res/raw/mysound.mp3'. - When building the notification I do the following
Notification notification = new Notification.Builder(context)
.setDefaults(0) //turns off ALL defaults
.setVibrate(vibrate) /sets to vibrate
....
.setSound(uri).build();
where
Uri uri = Uri.parse("android.resource://" + ctxt.getPackageName() + "/raw/mysound.mp3");
This appears to be the recipe indicated in a number of articles I find on a spot of googling and even in other threads on SO. And yet, when I issue a notification I do not hear the expected sound. What might I be doing wrong?
The answer below does not help since in the context of my hybrid Cordova app with a custom plugin attempting to build the APK throws up an error along the lines of class R not known/found...
mp3
that was causing the problem. Without the extension my original code delivers the desired result. Note that in a Cordova appR.java
is not recognized so what you have shown above is not truly applicable. – Comras