Fre*_*ech 5 customization push-notification xamarin.android
我已经浪费了至少一天的时间来尝试完成这项工作。我正在尝试播放收到通知后放置在 Resources/raw 中的 mp3 文件。我不知道如何获取 Uri。我的问题是:
1.要播放自定义文件,您必须将其放置在 Resources/raw 中,还是也可以放置在 Xamarin Android 项目下的 Assets/Sounds 中。
2.如何根据mp3文件所在位置正确获取Uri。
这是我的代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
I figured it out and I hope this will help someone better than getting a downvote for a question, this is how you do it: (Note: Make sure you put your mp3 file in your Xamarin Android project under Resources/raw/soundFile.mp3 and build the file as Android Resource).
Then create the Uri like this:
Android.Net.Uri alarmUri = Android.Net.Uri.Parse(${ContentResolver.SchemeAndroidResource}://{Context.PackageName}/{Resource.Raw.soundFile}");
Run Code Online (Sandbox Code Playgroud)
Create the Alarm Attributes like this:
var alarmAttributes = new AudioAttributes.Builder()
.SetContentType(AudioContentType.Sonification)
.SetUsage(AudioUsageKind.Notification).Build();
Run Code Online (Sandbox Code Playgroud)
And finally setSound on the channel itself ONLY from Android Oreo onwards (not on the notification, create the channel at application launch):
chan1.SetSound (alarmUri, alarmAttributes);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2476 次 |
| 最近记录: |