【问题标题】:SetSound and SetVibrationPattern not workingSetSound 和 SetVibrationPattern 不起作用
【发布时间】: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


    【解决方案1】:

    你的手机是什么?我在 Google Pixel (Android 8.1) 上测试了你的代码,它运行良好。然后在小米5s Plus(Android 8.1)上测试,进入手机的设置,然后在通知通道中打开声音应用程序! 所以可能有些手机厂商有自己的定制,你可以试试进入手机的设置看看!

    更新

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

    【讨论】:

    • 我使用的是带有 Android 8.1 的三星 Galaxy J7,并且在应用程序的声音设置中出现了假振动和默认声音。为什么会这样?
    • 这应该是三星定制的问题。如果你的Galaxy J7已经更新到Android Oreo,这可能是你的消息应用程序无法使用自定义或非默认通知声音的原因。这绝对不是安卓奥利奥的错。运行此版本 Android 的其他 Android 设备仍可以使用非默认通知声音。
    • 真的!?!?!?使用不同的安卓版本比使用不同的浏览器更糟糕......
    • 现在可以打开振动选项,看看能不能得到默认提示音的振动通知?
    • 是的,正如我上面所说,它不能使用自定义或非默认通知声音。我认为这是三星定制的不合理地方
    猜你喜欢
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多