I have I have been wasting at least a day trying to make this work. I am trying to play an mp3 file that i placed in Resources/raw once a notification is received. I do not know exactly how to get the Uri. My questions please are:
1.To play a custom file do you have to place it in Resources/raw or can it be also in Assets/Sounds under the Xamarin Android project.
2.How do i get the Uri correctly based on where the mp3 file resides.
This is my code:
private void createNotificationChannel()
{
var channelName = GetString(Resource.String.noti_chan_urgent);
var channelDescription = GetString(Resource.String.noti_chan_urgent_description);
// set the vibration patterm for the channel
long[] vibrationPattern = { 100, 200, 300, 400, 500, 400, 300, 200, 400 };
// Creating an Audio Attribute
var alarmAttributes = new AudioAttributes.Builder().SetUsage(AudioUsageKind.Alarm).Build();
// Create the uri for the alarm file
var alarmUri = Android.Net.Uri.Parse("MyApp.Android/Resources/raw/alarm.mp3"); // this must be wrong because its not working
// create chan1 which is the urgent notifications channel
var chan1 = new NotificationChannel(PRIMARY_CHANNEL_ID, channelName, NotificationImportance.High)
{
Description = channelDescription
};
// set the channel properties
chan1.EnableLights(true);
chan1.LightColor = Color.Red;
chan1.EnableVibration(true);
chan1.SetVibrationPattern(vibrationPattern);
chan1.SetSound(alarmUri, alarmAttributes);
chan1.SetBypassDnd(true);
chan1.LockscreenVisibility = NotificationVisibility.Public;
var manager = (NotificationManager)GetSystemService(NotificationService);
manager.CreateNotificationChannel(chan1);
}
}