【问题标题】:Push notifications not working in Xamarin.Android推送通知在 Xamarin.Android 中不起作用
【发布时间】:2019-06-18 00:35:24
【问题描述】:

我使用 Azure Notification HubFirebaseXamarin.Android 应用配置了推送通知。我关注了这个tutorial

当从 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!!
        }
    }

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

【问题讨论】:

  • 您在哪个版本的 Android 上进行测试?如果您在使用奥利奥,您还需要创建一个通知渠道。
  • 所以调用了发送通知方法?
  • @G.hakim 是的,通知方法被调用,没有遇到错误。但是没有收到通知。
  • @Cheesebaron 如何设置通知渠道?
  • @DomBurf 我已经在下面添加了通知通道代码

标签: xamarin xamarin.forms xamarin.android azure-notificationhub


【解决方案1】:

在您的 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());

【讨论】:

  • 没问题一定要检查我给出的更新
  • 我已经在使用 NotificationCompat(),因为我在另一个示例中遇到了这个问题。很好的例子,非常有用。再次感谢:)
  • 只是指出你没有使用NotificationManagerCompat,这是最重要的
  • 我已经更新了我的代码以使用 NotificationManagerCompat
猜你喜欢
  • 2023-03-19
  • 1970-01-01
  • 2017-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多