Flying light
Flying light

Reputation: 173

Xamarin cross notification custom sound

I am using

Xamarin + firebase notification for cross platform app

Need

To change the default notification sound

Expect

The sound changed, even if the app is colse. Which means I will assign a different tone for the notifications which come to this app

Coding
No need to write any code to play the sound or to trigger the on notofication recieved event at all


Current result:
I success to do this with IOS only, but faild with Android.
Now I can recieve notification with custom sound even if the app is closed, without writting any code for this


My code
For IOS
1- add this line to the notification payload
"sound": "test.wav"
only sound name with the extention
2- add test.wav to IOS project under Resources as "BundleResource", or "Embedded Resource"
3- send the notification, and do not care about "NotificationRecieved" event at all
4- kill the app, and send the notification
5- notification recieved with custom sound "test.wav"
6- IOS accect "wav" or "caf" files only

For Android
Repeat the same with "wav" file, but with some changes like:
1- the sound file add at Resources/raw as AndroidResource For Project.Droid
2- try to change the payload to be:
"sound": "test.wav"
"sound": "test"
"sound": "raw/test.wav"
"sound": "raw/test"
But never success to play the sound

Question
I am sure there is something wrong in the ("sound": "test.wav") format.
I tested it with and without extention,
I do not need to handle the sound by coding, I want to assign the tone exactly as done with the IOS project

I expected IOS to be harder than Android! but at end it was easy

Any help, please?

Upvotes: 3

Views: 3486

Answers (2)

Flying light
Flying light

Reputation: 173

Finally, I solved it.

For those who face the same problem I will post the solution.

For IOS, you can only pass the sound file name as I said before.

For Android, it is comples. I noticed that the app uses the default system sound for all notification which recieved in the background mode, closed, or killed app.

I went to the app settings, then I found a channel name "Miscellaneous" which used for such notifications.

The idea was how to change this channel sound?

If you create a channel with the same name, it will not work because you have to use channel name + channel ID.

I searched and finaly find the default channel id which is "fcm_fallback_notification_channel".

The second step was to create a channel with this name and id, in hope to override the system one.

I created this channel in the app start with the following code:

var importance = NotificationImportance.High;
var soundFileName = "sound";

NotificationChannel chan = new NotificationChannel("fcm_fallback_notification_channel", "Miscellaneous", importance);
chan.EnableVibration(true);
chan.LockscreenVisibility = NotificationVisibility.Public;
chan.SetSound(Android.Net.Uri.Parse("android.resource://com.myapp.test/raw/" + soundFileName), null);

NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(chan);

And it works as expected :)

Now I can recieve nottifications with the custom sound even if the app is closed, killed, or background mode.

I did not think that I can do this, finally.

Thanks for everyone.

Hope this can help someone.

Upvotes: 4

Leon Lu
Leon Lu

Reputation: 9234

For the Android, you should set the sound in NotificationChannel

var channel = new NotificationChannel(CHANNEL_ID, "FCM Notifications", NotificationImportance.Default)
                          {
                              Description = "Firebase Cloud Messages appear in this channel"
                          };
var audioattributes = new AudioAttributes.Builder();
audioattributes.SetContentType(AudioContentType.Music);
audioattributes.SetUsage(AudioUsageKind.Notification);
channel.SetSound(global::Android.Net.Uri.Parse("android.resource://" + this.ApplicationContext.PackageName + "/raw/app_sound"), audioattributes.Build());

Note: If you set it, please uninstall the app to alter sound settings, Check out these link for more details.

Upvotes: 0

Related Questions