Xamarin 如何深度链接Firebase消息传递后台通知?

Xamarin 如何深度链接Firebase消息传递后台通知?,xamarin,xamarin.forms,xamarin.android,firebase-cloud-messaging,android-notifications,Xamarin,Xamarin.forms,Xamarin.android,Firebase Cloud Messaging,Android Notifications,对于Xamarin.Android,当关闭的应用程序收到通知时,它会触发HandleIntent()。我想在打开后台通知后指定目标(意图)页面,就像在应用程序已经打开时接收前台通知一样 如何在HandleIntent()中指定要深入链接到应用程序不同部分的意图页面,而不是打开应用程序主页的后台通知?或者有没有一种方法可以启动应用程序并捕获消息。数据值然后链接到另一个页面 using System; using Android.App; using Android.Content; using A

对于Xamarin.Android,当关闭的应用程序收到通知时,它会触发HandleIntent()。我想在打开后台通知后指定目标(意图)页面,就像在应用程序已经打开时接收前台通知一样

如何在HandleIntent()中指定要深入链接到应用程序不同部分的意图页面,而不是打开应用程序主页的后台通知?或者有没有一种方法可以启动应用程序并捕获消息。数据值然后链接到另一个页面

using System;
using Android.App;
using Android.Content;
using Android.Media;

using Android.Support.V4.App;
using Firebase.Messaging;

namespace Appname
{
    [Service(Name = "com.mydomainname.appname.MyFirebaseMessagingService")]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {
        const string TAG = "MyFirebaseMsgService";
        internal static readonly string CHANNEL_ID = "alerts_channel";

        public override void OnNewToken(string token)
        {
            base.OnNewToken(token);
        }

        public override void OnMessageReceived(RemoteMessage message)
        {
            if (message.Data.Count > 0)
            {
                // NOTIFICTION AS DATA WITH ID
                Android.Util.Log.Debug(TAG, "From: " + message.From);
                Android.Util.Log.Debug(TAG, "Title: " + message.GetNotification().Title);
                Android.Util.Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
                Android.Util.Log.Debug(TAG, "Data: " + message.Data["EId"]);
                SendNotification(message.GetNotification().Title, message.GetNotification().Body, message.Data["EId"]);
            }
            else
            {
                // NOTIFICATION WITHOUT DATA
                Android.Util.Log.Debug(TAG, "From: " + message.From);
                Android.Util.Log.Debug(TAG, "Title: " + message.GetNotification().Title);
                Android.Util.Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
                SendNotification(message.GetNotification().Title, message.GetNotification().Body, null);
            }
        }

        public override void HandleIntent(Intent intent)// this method will fire when the app in background/closed state, and the foreground before calling OnMessageReceived()
        {
            base.HandleIntent(intent);
            string strTitle = "";
            string strBody = "";
            string strEId = "";

            if (intent.Extras != null)
            {
                foreach (var key in intent.Extras.KeySet())
                {
                    var value = intent.Extras.GetString(key);
                    switch (key)
                    {
                        case "gcm.notification.title":
                            strTitle = value;
                            break;
                        case "gcm.notification.body":
                            strBody = value;
                            break;
                        case "EId":
                            strEId = value;
                            break;
                    }
                }
            }
        }

        void SendNotification(string messageTitle, string messageBody, string strEId)
        {
            Intent intent;
            if(strEId == null)
            {
                intent = new Intent(this, typeof(MainActivity));
            }
            else
            {
                intent = new Intent(this, typeof(pageEvent));
                intent.PutExtra("EID", strEId);
            }

            intent.AddFlags(ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(this, 0 /* Request code */, intent, PendingIntentFlags.OneShot);

            var defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
            var notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .SetSmallIcon(Resource.Mipmap.ic_launcher_round)
                .SetContentTitle(messageTitle)
                .SetContentText(messageBody)
                .SetAutoCancel(true)
                .SetSound(defaultSoundUri)
                .SetContentIntent(pendingIntent);

            var notificationManager = NotificationManager.FromContext(this);

            notificationManager.Notify(0 /* ID of notification */, notificationBuilder.Build());
        }
    }
}

在sendNotification函数中,如果您按照代码查看下面的意图创建,您将看到指定了要打开的活动的名称

    if(strEId == null)
    {
        intent = new Intent(this, typeof(MainActivity));
    }
    else
    {
        intent = new Intent(this, typeof(pageEvent));
        intent.PutExtra("EID", strEId);
    }
当strEId==null时,主屏幕打开,即MainActivity 否则,将打开指定要打开的页面。i、 e pageEvent(它可以是您想要的任何活动)

因此,您需要做的是指定在特定通知到达时需要打开的活动

在这里,您可以选择多个选项,选择如何深入链接到所需屏幕

  • 您可以在现有代码中指定活动来代替pageEvent,或者根据任何其他条件创建新的意图等
  • intent=新的intent(这个,类型为(pageEvent)),其中pageEvent可以是任何活动名称

  • 您可以将通知类型与通知数据一起发送。 然后根据通知类型,可以指定需要打开的活动类型以及需要传递给该活动的数据(如果有)

      void SendNotification(string messageTitle, string messageBody, string strEId,string notifType)
                 {
    
       Intent intent;
    
       switch (notifType) 
       {
         case "home":
           //create intent for home page
           break;
         case "customPage":
           //create intent for customPage
           break;    
     }
    
     now pass the intent to pendingIntent and show the notification.
    
    }

  • 您可以从通知转到主屏幕,同时将通知类型传递到主屏幕。然后在主屏幕中创建自定义功能来处理通知。它将检查是否有来自意图的任何通知类型,如果有,我们将从主屏幕重定向到目标屏幕


  • 我不太熟悉这方面的细节,但我想到了两个(模糊的)想法:1)在HandleIntent中,是否有一些“内部”通知或MessageCenter消息可以触发,然后应用程序会收到?2) 如果你在某处设置了一些“静态”值,应用程序在恢复时会看到它吗?一句话:找到一些沟通信息的方法,应用程序可以在OnResume中使用,或者在OnResume之后不久使用。(棘手的部分是找到一种机制,该机制在onResume发生之前不会被清除/重新初始化。)下面的响应/示例将引导您找到您想要做的事情