我希望在所有状态(已杀死,前景和背景)都收到通知时在我的应用中实现自定义通知声音。目前,我的代码在某些设备上可以运行,在体内1812,通知声音在前台模式下工作,而在其他情况下,默认通知声音在工作。在Redmi 6 Pro中,自定义通知声音不起作用。如何使其在所有设备上都能正常工作。
从MainActivity调用CreateNotificationChannel()
。
void CreateNotificationChannel()
{
try
{
Android.Net.Uri sound = Android.Net.Uri.Parse(ContentResolver.SchemeAndroidResource + "://" + this.PackageName + "/" + Resource.Raw.bloom);
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
return;
}
AudioAttributes attributes = new AudioAttributes.Builder()
.SetUsage(AudioUsageKind.NotificationRingtone)
.Build();
var channel = new NotificationChannel(CHANNEL_ID, "PushNotifications", NotificationImportance.High)
{
LockscreenVisibility = NotificationVisibility.Public,
Importance = NotificationImportance.Max,
LightColor = Android.Graphics.Color.Red,
};
channel.CanShowBadge();
channel.SetShowBadge(true);
channel.SetSound(sound, attributes);
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
catch (Exception ex)
{
}
}
这是我的json负载
{
"data": {
"targetPage": "Message",
"userType": null,
"targetId": 4034,
"id": "210bfd9fss-b469-46ad-bdec-e576eaffa293",
"content_available": true,
"android_channel_id": "test_fcm_channel",
"sound": "bloom.wav",
"priority": "high"
},
"notification": {
"body": "Hello world",
"title": TestApp
"sound": "bloom.wav",
"click_action": ""
},
"priority": "high",
"condition": null,
"to": "cpVcLtt4Y0M:APA91bEHeweyfcfgvhjbnkmlXiVFb1WBnOe7HaPsVGIyp0RhaPcVEHpq_sJFY5EWVZyhYotdN9ZmToavMpl-FbJfG7Cm4C6SHy9o69obnTW6W2d3wogeji-g6aRolLxfMJtNu16jFR92E44RUQyZc_ZV"
}