首页 > 解决方案 > 推送通知在 Xamarin.Android 中不起作用

问题描述

我已经使用Azure Notification HubFirebase为Xamarin.Android应用程序配置了推送通知。我已经按照这个教程

Azure 通知中心发送测试推送通知时,我可以看到我的通知代码被调用,所以看起来一切都已配置。没有发生错误,但也没有收到推送通知。

    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {
        const string TAG = "MyFirebaseMsgService";
        public override void OnMessageReceived(RemoteMessage message)
        {
            Log.Debug(TAG, "From: " + message.From);
            if (message.GetNotification() != null)
            {
                //These is how most messages will be received
                Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
                SendNotification(message.GetNotification().Body);
            }
            else
            {
                //Only used for debugging payloads sent from the Azure portal
                SendNotification(message.Data.Values.First());
            }
        }

        void SendNotification(string messageBody)
        {
            var intent = new Intent(this, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

            var notificationBuilder = new NotificationCompat.Builder(this)
                .SetContentTitle("FCM Message")
                .SetSmallIcon(Resource.Drawable.drivingalert)
                .SetContentText(messageBody)
                .SetAutoCancel(true)
                .SetContentIntent(pendingIntent);

            var notificationManager = NotificationManager.FromContext(this);
            notificationManager.Notify(0, notificationBuilder.Build()); // this should send the notification!!
        }
    }

我可以单步执行代码,一切似乎都没有问题,但没有收到推送通知。

标签: xamarinxamarin.formsxamarin.androidazure-notificationhub

解决方案


在 MainActivity 中调用以下方法设置通知通道:

void CreateNotificationChannel()
    {
        if (Build.VERSION.SdkInt < BuildVersionCodes.O)
        {
            // Notification channels are new in API 26 (and not a part of the
            // support library). There is no need to create a notification 
            // channel on older versions of Android.
            return;
        }

        var channel = new NotificationChannel(CHANNEL_ID, "FCM Notifications", NotificationImportance.Default)
                      {
                          Description = "Firebase Cloud Messages appear in this channel"
                      };

        var notificationManager = (NotificationManager) GetSystemService(NotificationService);
        notificationManager.CreateNotificationChannel(channel);
    }

如果您检查 Firebase Github,他们已经更新了此代码,但在他们的文档中似乎不可用,而且其他推送通知服务器文档还没有更新它,这导致人们以巨大的速度面临这个问题,谢谢指出这一点,我将向 Mircosoft 提出这个问题!

更新

此外,请检查您是否使用 Notification Compat 类来实现向后兼容性,如下所示:

 var intent = new Intent(this, typeof(MainActivity));
 intent.AddFlags(ActivityFlags.ClearTop);               
 var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

 NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
             .SetSmallIcon(Resource.Drawable.Icon)
             .SetContentTitle(messageTitle)
             .SetContentText(messageBody)
             .SetSound(Settings.System.DefaultNotificationUri)
             .SetVibrate(new long[] { 1000, 1000 })
             .SetLights(Color.AliceBlue, 3000, 3000)
             .SetAutoCancel(true)
             .SetOngoing(true)
             .SetContentIntent(pendingIntent);
            NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
            notificationManager.Notify(0, notificationBuilder.Build());

推荐阅读