简体   繁体   中英

Android custom notification sound in system app settings

I used to use a custom sound for notifications saved in res/raw . It is a requirement to not allow another application to use this sound.

With Android 8 notification channels I was able to make a workaround with redirecting a user from system notification preferences to app preferences from where I could show custom sound among other android provided sounds. And when a user changed notification sound new notification channel was created.

Now I got to the next level and I need to provide this sound for Firebase cloud messaging while an app is in the background.

UPDATE: I need to rely on High Priority messages and notification tray handler so data message is (probably) not solution for me.

So long story short: Can I make custom sound available from system app settings only for my application (including Firebase push notifications)?

First of all you need to be sending payload notifications so they don't get automatically displayed by Android itself. Read this great post for more info on that.

After you set up your custom notification receiving, then you just need to apply a sound to it.

To do that you need to get your file from res/raw/ like this:

notification.sound = Uri.parse("android.resource://com.my.package/" + R.raw.notification_sound);

where com.my.package needs to be replaced with your package name. If that doesn't work for you try setting notification defaults:

notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM