capacitor-community / fcm

Enable Firebase Cloud Messaging for Capacitor apps
https://capacitor.ionicframework.com/docs/
MIT License
237 stars 83 forks source link

Custom sounds not working on Android in background mode #84

Closed wwwguy closed 1 year ago

wwwguy commented 2 years ago

I've managed to get custom notification sounds working for Android when the app is in foreground. But when it's in background mode (or closed) it just plays the default notification sound. Same payload from the server. Sounds are all located in res/raw (again, it all works fine when the app's in the foreground).

In looking around it seems clear this is caused by the Android 26+ change whereby notification channels must be used in order to play a custom sound. I have attempted to do this by following many of the other examples out there. However, as they are all a bit different from the Capacitor configuration using the FCM plugin here I'm hoping someone can better define this here. (i.e. it should really be part of the instructions for this specific plugin).

I'm assuming I can use the FCM subscribeTo() function, after checking for permission and registering the token:

PushNotifications.requestPermissions().then((response) => PushNotifications.register().then(() => { console.log("FCM - Registered for push"); FCM.subscribeTo({ topic: "notification05" }) .then((r) => console.log("FCM - Subscribed to channel: " + JSON.stringify(r))) .catch((err) => console.log(err)); }) );

But... how does this attach the specific sound file to the topic/channel? Or does that need to be done with PushNotifications.createChannel() ? (and if so, how?)

Then there's the issue of the MainActivity.java file. Does it require further modification than what was needed to get @capacitor-community/fcm working in general? (I'm assuming not, as that is supposedly the whole point of this plugin.)

Finally the question of the payload from the server. What tag (and where) is added to trigger the channel and make the custom sound play? There seems to be conflicting guidance out there about this (and the Google docs themselves are terrible and silent on the issue). My current payload (which is working for foreground) is:

{ "to": "",   "notification": {   "title": "Some title",   "body": "Some body",   "sound": "notification05.mp3",   "android_channel_id": "notification05" }, "content_available": true, "priority": "high" }

If anyone is using custom sounds in conjunction with this plugin, help is appreicated!

luismrosero commented 2 years ago

x2

apri23 commented 1 year ago

hey is there any solution for this problem

jcesarmobile commented 1 year ago

Topics and channels are different things, topics are a FCM thing, while channels are an Android thing for notifications (both push and local). In your code you are subscribing to a topic, not to a channel.

To use custom notification sounds you have to create a notification channel in your app, you can use @capacitor/push-notifications or @capacitor/local-notifications to do so. https://capacitorjs.com/docs/apis/local-notifications#createchannel Then use the same id you used to create the channel in the createChannel call as the "android_channel_id" value in the push notification payload.

Note that users can change the channel sound from the app settings, so on Android 8+ it's not possible to guarantee that the sound is the one the developer wants.