SetSound和SetVibrationPattern不起作用

时间:2019-04-09 20:19:02

标签: android xamarin.forms firebase-cloud-messaging

我想在自定义通知中播放自定义mp3,放在res / raw文件夹中,但它始终播放手机的默认通知声音。我使用了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);
                }
            }

1 个答案:

答案 0 :(得分:1)

您的手机是什么?我在Google Pixel(Android 8.1)上测试了您的代码,效果很好。然后我在小米MI 5s Plus(Android 8.1)上对其进行了测试,直到我输入手机的 Setting ,然后在 Sound 的通知通道中将其设置为打开,它才能起作用应用程序! 因此,可能是某些手机制造商有自己的自定义设置,您可以尝试输入手机的设置以进行查看!

更新

官方链接:https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/notifications/local-notifications-walkthrough