Xamarin表单:使用FirebasePushNotificationPlugin轻按推送通知时,打开内容页面

时间:2019-04-18 10:44:00

标签: xamarin.forms push-notification

我正在使用以下通知有效载荷,以使用邮递员向我的android设备发送推送通知。

{
    "to" : "device_key",
 "collapse_key" : "type_a",
 "notification" : {
     "body" : "Body of Your Notification",
     "title": "Title of Your Notification",
     "sound": "default"
 },
 "data" : {
     "body" : "Body of Your Notification in Data",
     "title": "Title of Your Notification in Title",
     "key_1" : "Value for key_1",
     "key_2" : "Value for key_2"
 }
}

仅当应用程序处于前台时,通知才会接收到我的设备并触发OnMessageReceived()。当应用处于后台状态时,OnMessageReceived不会触发,也不会在点击通知时触发。

我安装了FirebasePushNotificationPlugin,并在App构造函数中添加了如下所示的通知事件。

CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
{
      System.Diagnostics.Debug.WriteLine("Received");
};

CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
{
    System.Diagnostics.Debug.WriteLine("Opened");
    foreach (var data in p.Data)
     {
         System.Diagnostics.Debug.WriteLine($"{data.Key} : {data.Value}");
     }
     if (!string.IsNullOrEmpty(p.Identifier))
     {
         System.Diagnostics.Debug.WriteLine($"ActionId: {p.Identifier}");
     }
 };

但是当接收或点击通知时,这些代码不会触发。

在一个线程中,我发现上述事件仅在您发送数据消息时触发,如果要使用此事件处理程序,则不应发送通知消息。

但是,如果我发送数据消息,则仅通知不会接收到我的设备。以下是数据消息的通知有效负载。

{
    "to" : "device_key",
 "collapse_key" : "type_a",
 "data" : {
     "body" : "Body of Your Notification in Data",
     "title": "Title of Your Notification in Title",
     "key_1" : "Value for key_1",
     "key_2" : "Value for key_2"
 }
}

点击通知时,我需要在PCL中打开内容页面。但是OnNotificationReceivedOnNotificationOpened不会触发。那我在实现过程中缺少什么呢?

1 个答案:

答案 0 :(得分:0)

我通过以下方式处理通知窃听。页面加载在App.xaml.cs中处理。

在OnCreate()上:

//Background or killed mode
if (Intent.Extras != null)
{
    foreach (var key in Intent.Extras.KeySet())
    {
        var value = Intent.Extras.GetString(key);
        if (key == "webContentList") 
        {
            if (value?.Length > 0)
            {
                isNotification = true;
                LoadApplication(new App(domainname, value));
            }
        }
    }
}
//Foreground mode
if (FirebaseNotificationService.webContentList.ToString() != "")
{
    isNotification = true;
    LoadApplication(new App(domainname, FirebaseNotificationService.webContentList.ToString()));
    FirebaseNotificationService.webContentList = "";
}

//Normal loading
if (!isNotification)
{
    LoadApplication(new App(domainname, string.Empty));
}

在FirebaseNotificationService上:

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class FirebaseNotificationService : FirebaseMessagingService
{
    public static string webContentList = "";
    public override void OnMessageReceived(RemoteMessage message)
    {
        base.OnMessageReceived(message);
        webContentList = message.Data["webContentList"];

        try
        {
            SendNotificatios(message.GetNotification().Body, message.GetNotification().Title);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error:>>" + ex);
        }
    }

    public void SendNotificatios(string body, string Header)
    {
        if (Build.VERSION.SdkInt < BuildVersionCodes.O)
        {
            var intent = new Intent(this, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

            var notificationBuilder = new Android.App.Notification.Builder(this, Utils.CHANNEL_ID)
                        .SetContentTitle(Header)
                        .SetSmallIcon(Resource.Drawable.icon)
                        .SetContentText(body)
                        .SetAutoCancel(true)
                        .SetContentIntent(pendingIntent);

            var notificationManager = NotificationManager.FromContext(this);

            notificationManager.Notify(0, notificationBuilder.Build());
        }
        else
        {
            var intent = new Intent(this, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

            var notificationBuilder = new Android.App.Notification.Builder(this, Utils.CHANNEL_ID)
                        .SetContentTitle(Header)
                        .SetSmallIcon(Resource.Drawable.icon)
                        .SetContentText(body)
                        .SetAutoCancel(true)
                        .SetContentIntent(pendingIntent)
                        .SetChannelId(Utils.CHANNEL_ID);

            if (Build.VERSION.SdkInt < BuildVersionCodes.O)
            {
                return;
            }

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

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

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