XAMARIN.FORMS:如果应用程序在 Android 后台运行,FCM 推送通知将不会振动 [英] XAMARIN.FORMS: FCM Push Notifications won't vibrate if app is in background in Android

查看:35
本文介绍了XAMARIN.FORMS:如果应用程序在 Android 后台运行,FCM 推送通知将不会振动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xamarin.Forms,但在处理推送通知时遇到了一些问题.它们只有在应用处于前台时才能正常工作.

I'm using Xamarin.Forms but I'm having a few problems handling push notifications. They only work fine when app is in foreground.

当应用程序在后台时,所有通知都会被接收并正确显示,但我无法让它们振动甚至打开 LED.我找到了很多文档,但它已经很旧了,而且大多数情况下,不推荐使用所引用的方法.

When the app is in background, all notifications are received and displayed correctly but I can't manage to get them to vibrate or even turn led on. I have found a lot of documentation but it's pretty old and most of the time, methods referred are deprecated.

无论如何,我有一个 FirebaseMessagingService 来处理 FCM 事件,这是生成推送通知的操作:

Anyway, I have a FirebaseMessagingService to handle FCM events and this is the action that generates the push notification:

    void SendNotification(RemoteMessage message)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);

        intent.PutExtra("type", message.Data["type"]);

        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

        NotificationManager notificationManager = (NotificationManager)ApplicationContext.GetSystemService(NotificationService);
        NotificationCompat.Builder builder = null;

        if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
        {
            var importance = NotificationImportance.Default;
            NotificationChannel notificationChannel = new NotificationChannel("ID", "Name", importance);
            notificationManager.CreateNotificationChannel(notificationChannel);
            builder = new NotificationCompat.Builder(ApplicationContext, notificationChannel.Id);
        }
        else
            builder = new NotificationCompat.Builder(ApplicationContext);

        builder = builder
            .SetSmallIcon(Resource.Drawable.icon)
            .SetDefaults((int)((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate))
            .SetAutoCancel(true)
            .SetContentTitle(message.GetNotification().Title)
            .SetContentText(message.GetNotification().Body)
            .SetContentIntent(pendingIntent)
            .SetLights(65536, 1000, 500)
            .SetVibrate(new long[] { 1000, 1000, 1000 })
            .SetPriority((int)NotificationPriority.High);

        PowerManager pm = (PowerManager)ApplicationContext.GetSystemService(PowerService);

        WakeLock wl = pm.NewWakeLock(WakeLockFlags.Partial, "TAG");

        wl.Acquire(15000);

        notificationManager.Notify(0, builder.Build());

        wl.Release();

    }

我还在 Android.Manifest 文件中添加了这些行:

I also added these lines in the Android.Manifest file:

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE"/>

之前要简单得多,但我一直在添加新的东西以使其工作,例如 WAKE_LOCK 部分或创建 Builder 对象时 Android 版本的区别.仅当应用程序处于前台时才会振动.

It was pretty much simpler before but I've been adding new things trying to make it work, like the WAKE_LOCK section or the distinction of Android Version when creating the Builder object. It vibrates only when app is in foreground.

我什至尝试使用 James Montemagno 的 Vibrate 插件或使用 Android.Vibrator 类,但当应用程序处于后台时,它们也不起作用.

I even tried using the James Montemagno's Vibrate Plugin or using Android.Vibrator class and they didn't work either when app is in background.

我愿意在这里接受建议.谢谢.

I'm open to suggestions here. Thanks.

推荐答案

在 Oreo(+) 上,您必须通过 EnableVibration(true)NotificationChannel 上启用振动>.

On Oreo(+), you have to enable the vibration on the NotificationChannel via EnableVibration(true).

NotificationChannel channel;
channel = notificationManager.GetNotificationChannel(myUrgentChannel);
if (channel == null)
{
    channel = new NotificationChannel(myUrgentChannel, channelName, NotificationImportance.High);
    channel.EnableVibration(true);
    channel.EnableLights(true);
    channel.SetSound(
        RingtoneManager.GetDefaultUri(RingtoneType.Notification),
        new AudioAttributes.Builder().SetUsage(AudioUsageKind.Notification).Build()
    );
    channel.LockscreenVisibility = NotificationVisibility.Public;
    notificationManager.CreateNotificationChannel(channel);
}
channel.Dispose();

这篇关于XAMARIN.FORMS:如果应用程序在 Android 后台运行,FCM 推送通知将不会振动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆