Android 设置声音和设置振动模式不工作
我想在推送通知时播放放置在res/raw文件夹中的自定义mp3,但它始终播放手机的默认通知声音。我用的是xamarin,我的测试手机是安卓8.1。即使在早期版本中,也不总是播放自定义声音。以下代码放在OnMessageReceived方法中。来自FCM的通知只有数据标记,而没有通知标记。此外,振动模式似乎始终是默认模式。图标和文本工作正常Android 设置声音和设置振动模式不工作,android,xamarin.forms,firebase-cloud-messaging,Android,Xamarin.forms,Firebase Cloud Messaging,我想在推送通知时播放放置在res/raw文件夹中的自定义mp3,但它始终播放手机的默认通知声音。我用的是xamarin,我的测试手机是安卓8.1。即使在早期版本中,也不总是播放自定义声音。以下代码放在OnMessageReceived方法中。来自FCM的通知只有数据标记,而没有通知标记。此外,振动模式似乎始终是默认模式。图标和文本工作正常 NotificationManager notificationManager = (NotificationManager)this.GetSystemSe
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);
}
}
你的电话是什么?我在谷歌Pixel(安卓8.1)上测试了你的代码,效果很好。然后我在小米米5s Plus(安卓8.1)上测试它,直到我进入手机的设置,然后在应用程序的通知通道中设置声音,它才起作用! 因此,可能是一些手机制造商有自己的定制,你可以尝试进入手机的设置来看看 更新:
官方链接:我使用了三星Galaxy J7和安卓8.1,在应用程序的声音设置中,振动显示为false,声音为默认声音。为什么?这应该是三星定制的问题。如果您的Galaxy J7已更新为Android Oreo,这可能就是您的消息应用程序无法使用自定义或非默认通知声音的原因。这绝对不是安卓奥利奥的错。运行此版本Android的其他Android设备仍然可以使用非默认通知声音。真的吗!?!?!?使用不同的android Verison比使用不同的浏览器更糟糕……现在你可以打开振动选项,看看你是否可以用默认提示音收到振动通知?是的,正如我所说,它不能使用自定义或非默认通知音。我认为这是三星定制的一个不合理的地方