【问题标题】:Xamarin Forms Push Notification received but not showing/appearing收到 Xamarin 表单推送通知但未显示/出现
【发布时间】:2019-07-01 04:55:39
【问题描述】:

我之前已经启动了推送通知并且可以正常工作,但现在由于某种原因虽然收到了推送通知,(我知道它已收到,因为它在 Debug 中显示已收到 FireBaseMessage),它不再显示或通知没有不出现。

这里可以看到收到了消息:

以下是我的 MyFireBaseMessagingService.cs 中的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;
using System.Diagnostics;
using Firebase.Messaging;

namespace Chatfer.Droid
{
    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    class MyFirebaseMessagingService : FirebaseMessagingService
    {
        const string TAG = "MyFirebaseMsgService";
        public override void OnMessageReceived(RemoteMessage message)
        {
            System.Diagnostics.Debug.WriteLine(TAG, "From: " + message.From);
            if (message.GetNotification() != null)
            {
                //These is how most messages will be received
                System.Diagnostics.Debug.WriteLine(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 Notification.Builder(this)
                        .SetContentTitle("Chatfer Message")
                        .SetSmallIcon(Resource.Drawable.ic_launcher)
                        .SetContentText(messageBody)
                        .SetAutoCancel(false)
                        .SetContentIntent(pendingIntent);

            var notificationManager = NotificationManager.FromContext(this);

            notificationManager.Notify(0, notificationBuilder.Build());
        }
    }
}

发生了什么事?为什么通知突然停止出现?任何见解或建议将不胜感激!

编辑:将 MyFirebaseMessagingService.cs 更改为以下内容后,通知再次开始显示!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;
using System.Diagnostics;
using Firebase.Messaging;

namespace Chatfer.Droid
{
    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    class MyFirebaseMessagingService : FirebaseMessagingService
    {
        const string TAG = "MyFirebaseMsgService";
        public override void OnMessageReceived(RemoteMessage message)
        {
            System.Diagnostics.Debug.WriteLine(TAG, "From: " + message.From);
            if (message.GetNotification() != null)
            {
                //These is how most messages will be received
                System.Diagnostics.Debug.WriteLine(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 Notification.Builder(this)
                        .SetContentTitle("Chatfer Message")
                        .SetSmallIcon(Resource.Drawable.ic_launcher)
                        .SetContentText(messageBody)
                        .SetAutoCancel(true)
                        .SetContentIntent(pendingIntent)
                        .SetChannelId("YourUniqueChannelID");
            
            //var notificationManager = NotificationManager.FromContext(this);

            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("YourUniqueChannelID", "FCM Notifications", NotificationImportance.Default)
            {
                Description = "Firebase Cloud Messages appear in this channel"
            };

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

            notificationManager.Notify(0, notificationBuilder.Build());
        }
    }
}

【问题讨论】:

  • 问题是我已将我的 TargetAPIVersion 更新为 26。请参考forums.xamarin.com/discussion/106314/…?了解更多信息
  • Bhai,我希望我能再投票十几次。您的修改帮助我解决了我几天来遇到的问题。
  • 哈哈哈没问题,一切顺利!

标签: firebase xamarin xamarin.forms push-notification firebase-cloud-messaging


【解决方案1】:

我认为您不明白的原因是,如果您有 Android V 8+ 设备,您没有通知渠道,我相信您会看到您正在 MainActivity 中注册通知渠道OnCreate 方法

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);
    }

【讨论】:

  • 你说得对,我没有这个,最近我的应用程序的最低 API 版本更新到 26。谢谢 bhai
  • 什么时候,在OnCreate中,需要调用CreateNotificationChannel方法吗?
  • @Bedir 我认为没有任何此类限制,您只需确保在发送通知之前有一个频道
猜你喜欢
  • 2020-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-06
  • 1970-01-01
  • 2011-08-23
相关资源
最近更新 更多