Forum Xamarin Xamarin.Android

How to play notification dependant custom sound file when app is in the background

I understand that for Oreo onward at least, a custom sound file can be made to play when the app is in the background by creating a Notification Channel and setting the sound for that channel, along the lines of :

                    Android.Net.Uri uri = Android.Net.Uri.Parse("android.resource://" + Application.Context.PackageName + "/raw/somecustomfile");

                    NotificationChannel channel = new NotificationChannel(CHANNEL_ID,"FCM Notifications", NotificationImportance.High)
                    {
                        Description = "Firebase Cloud Messages appear in this channel"
                    };

                    var alarmAttributes = new AudioAttributes.Builder()
                       .SetContentType(AudioContentType.Music)
                       .SetUsage(AudioUsageKind.Notification).Build();

                    channel.SetSound(uri, alarmAttributes);

                    var notificationManager = (NotificationManager)GetSystemService(Android.Content.Context.NotificationService);
                    notificationManager.CreateNotificationChannel(channel);

However, my server can send different (application specific) types of notification which need to be identified by their notification sound when the app is in background.

So, how can I play a different sound file based upon data passed in the notification when the app is in the background?

Thanks

Best Answer

  • JarvanJarvan Member, Xamarin Team Xamurai
    Accepted Answer

    how can I play a different sound file based upon data passed in the notification when the app is in the background

    NotificationChannel.SetSound will set the sound that should be played for notifications posted to this channel and its audio attributes. To different sound files, you can set specified sound file to different channels. Create other notification channels for informational messages, NotificationManager.Notify(channel,notification) can help you identify this notification unique within your application.

    Tutorial about Android Notification Channels: https://devblogs.microsoft.com/xamarin/android-oreo-notification-channels/

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    Accepted Answer

    how can I play a different sound file based upon data passed in the notification when the app is in the background

    NotificationChannel.SetSound will set the sound that should be played for notifications posted to this channel and its audio attributes. To different sound files, you can set specified sound file to different channels. Create other notification channels for informational messages, NotificationManager.Notify(channel,notification) can help you identify this notification unique within your application.

    Tutorial about Android Notification Channels: https://devblogs.microsoft.com/xamarin/android-oreo-notification-channels/

  • MarthrMarthr Member ✭✭

    Thank you for your concise answer. What is the situation if I need to do this "pre"-Oreo ?

  • JarvanJarvan Member, Xamarin Team Xamurai

    What is the situation if I need to do this "pre"-Oreo

    Could you post more details about "pre"-Oreo?

  • MarthrMarthr Member ✭✭

    To the best of my knowledge Notification Channels were only introduced in Oreo (Android 8.0/8.1)
    Our App needs to support systems back to Android 4.4.4

    What options do I have for playing different sound files based on the notification settings, in background mode for Android versions earlier than 8.0.

    Thanks.

  • MarthrMarthr Member ✭✭

    OK I may have worked this out. When the server sends the notification, it sets the channel_id to the channel corresponding to the sound file initialised against it on the client, and it sets the sound to the name of the sound file.

    On the client
    -> Oreo+ will use the channel id value and play the sound initialised on the client against that particular channel.
    -> Pre Oreo will use the sound value passed in the notification.

    Part of the firebase messaging json structure for the notification follows :

                            chanID    = GetChannelName(3);
                            theSound = GetSoundFileName(3)";
    
    
      message = new Message()
                        {
    
                            #region "notification parts"
                            Notification = new Notification()
                            {
                                Title = ase.Line1,
                                Body = ase.Line2,
                            },
                            Android = new AndroidConfig()
                            {
                                Priority = Priority.High,
                                Notification = new AndroidNotification()
                                {
                                    Sound     = theSound,
                                    ChannelId = chanID,
                                },
                            },
    
Sign In or Register to comment.