【问题标题】:Resume App from a Firebase Notification is not working (Xamarin Forms)从 Firebase 通知恢复应用程序不起作用(Xamarin 表单)
【发布时间】:2020-05-22 17:09:51
【问题描述】:

我正在努力将 firebase 推送通知集成到我的应用程序中。

请找到我的 firebase FirebaseMessagingService 类。

如果应用程序已打开并运行,一切正常。但是,如果应用程序未打开/如果我切换到其他应用程序(我的应用程序未关闭)。我收到通知,但是当我点击通知时,它会重新启动应用程序而不恢复。

我在我的主要活动中使用启动模式 LaunchMode = LaunchMode.SingleTop

如果应用程序打开,我会在主活动的 OnNewIntent 覆盖方法中得到响应。

谁能帮我找出真正的原因。请帮忙。

    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class DriverAppMessagingService : FirebaseMessagingService
    {
        #region Overriden Methods

        public override void OnMessageReceived(RemoteMessage message)
        {
            base.OnMessageReceived(message);
            var parameters = new Dictionary<string, object>();
            var notification = message.GetNotification();
            if (null != notification)
            {
                if (!string.IsNullOrEmpty(notification.Body))
                {
                    parameters.Add("Body", notification.Body);
                }

                if (!string.IsNullOrEmpty(notification.BodyLocalizationKey))
                {
                    parameters.Add("BodyLocalizationKey", notification.BodyLocalizationKey);
                }

                // convert the incoming message to a local notification
                SendLocalNotification(parameters);

                // send the incoming message directly to the MainActivty
                SendNotificationToMainActivity(parameters);
            }
        }

        public override void OnNewToken(string p0)
        {
            base.OnNewToken(p0);
            //Persist the token to app settings for registration purpose.
            AppDefinition.Helpers.Settings.Current.PnsHandle = p0;
        }

        #endregion

        #region Private Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        private void SendNotificationToMainActivity(Dictionary<string, object> args)
        {
            if (CrossCurrentActivity.Current.Activity is MainActivity activity)
            {
                var message = args["Body"].ToString();
                activity.TriggerPushNotification(message);
            }
        }

        /// <summary>
        /// Method to trigger the local notification.
        /// </summary>
        /// <param name="args"></param>
        private void SendLocalNotification(Dictionary<string, object> args)
        {
            //TODO Only using one token from message response.
            var message = args["Body"].ToString();

            var intent = new Intent(CrossCurrentActivity.Current.Activity, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
            intent.PutExtra("message", message);

            var pendingIntent = PendingIntent.GetActivity(CrossCurrentActivity.Current.Activity, 0, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.OneShot);

            var notificationBuilder = new NotificationCompat.Builder(CrossCurrentActivity.Current.Activity, Constants.NotificationChannelName)
                .SetContentTitle(Constants.ContentTitle)
                .SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
                .SetContentText(message)
                .SetAutoCancel(true)
                .SetShowWhen(false)
                .SetLights(0xff0000, 100, 100)
                .SetContentIntent(pendingIntent);

            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                notificationBuilder.SetChannelId(Constants.NotificationChannelName);
            }
            var notificationManager = NotificationManager.FromContext(CrossCurrentActivity.Current.Activity);
            notificationManager.Notify(0, notificationBuilder.Build());
        }
        #endregion
    }

【问题讨论】:

    标签: xamarin.forms xamarin.android firebase-notifications


    【解决方案1】:

    当你设置MainActivityLaunchMode = LaunchMode.SingleTop和设置ActivityFlags.ClearTop时,当你点击通知打开你的应用程序时,它将清除MainActivity之上的所有活动,并将MainActivity放在堆栈的顶部。 它不是重新创建 MainActivity,而是进入 OnNewIntent 方法。

    你可以在OnCreate方法中下一个断点,当你点击通知后打开应用程序时,它不会进入它。

    【讨论】:

    • 那么请告诉我,哪个标志或配置可以在这里工作?
    • 但在 Xamarin 形式中。只有一个 Activity 可用吗?
    • 我试过了,在我的情况下它再次进入 OnCreate。如果应用程序未打开。如果应用程序已打开并出现在屏幕中,那么正如您所说,它没有进入 oncreate
    • @Sooraj 你能分享一个屏幕截图吗?我这边一切正常,当我将 LaunchMode = LaunchMode.SingleTop 设置为 MainActivity 时,它只是进入其 OnNewIntent 方法。或者您尝试将 LaunchMode 更改为 LaunchMode.SingleTask,然后 intent.AddFlags(ActivityFlags.NewTask);(即使它们是等效)
    • @NithaPaul 是的,一般来说,我们只在表单中使用一个 MainActivity。
    【解决方案2】:

    数据消息 - 由客户端应用处理。即使您的应用程序处于前台/后台/已终止,这些消息也会触发 onMessageReceived() 回调。使用此类消息时,您是提供 UI 并在 Android 设备上收到推送通知时进行处理的人。

    {
        "data": {
            "message" : "my_custom_value",
            "other_key" : true,
            "body":"test"
         },
         "priority": "high",
         "condition": "'general' in topics"
    }
    

    试试这个,这会解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 2020-11-21
      相关资源
      最近更新 更多