【发布时间】:2019-04-09 20:19:02
【问题描述】:
我想在推送通知中播放放在 res/raw 文件夹中的自定义 mp3,但它总是播放我手机的默认通知声音。我使用了 xamarin,我的测试手机是 Android 8.1。即使在早期版本中,也不总是播放自定义声音。以下代码放在 OnMessageReceived 方法中。 FCM 的通知只有数据标签,没有通知标签。此外,振动模式似乎始终是默认模式。图标和文本工作正常。
NotificationManager notificationManager = (NotificationManager)this.GetSystemService(Context.NotificationService);
//Setting up Notification channels for android O and above
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
SetupChannels(notificationManager, messageTitle);
}
var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentTitle(messageTitle)
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetDefaults((int)NotificationDefaults.All)
.SetSound(global::Android.Net.Uri.Parse("android.resource://" + this.ApplicationContext.PackageName + "/raw/de900"))
.SetVibrate(new long[] { 1000, 500, 1000, 500 })
.SetContentIntent(pendingIntent)
.SetStyle(new NotificationCompat.BigTextStyle().BigText(messageBody));
notificationManager.Notify(MainActivity.NOTIFICATION_ID, notificationBuilder.Build());
}
private void SetupChannels(NotificationManager notificationManager, string description)
{
// AudioAttributes.Builder.
NotificationChannel channel = new NotificationChannel(MainActivity.CHANNEL_ID, "EmersyChannel", NotificationImportance.Max)
{
LockscreenVisibility = NotificationVisibility.Public,
Description = description
};
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/de900"),
audioattributes.Build());
channel.EnableVibration(true);
channel.SetVibrationPattern(new long[] { 100, 30, 100, 30, 100, 200, 200, 30, 200, 30, 200, 200, 100, 30, 100, 30, 100, 100, 30, 100, 30, 100, 200, 200, 30, 200, 30, 200, 200, 100, 30, 100, 30, 100 });
channel.EnableLights(true);
channel.LightColor = Color.Red;
channel.SetShowBadge(true);
if (notificationManager != null)
{
notificationManager.CreateNotificationChannel(channel);
}
}
【问题讨论】:
标签: android xamarin.forms firebase-cloud-messaging