从Firebase通知恢复应用程序不起作用(Xamarin表单)

问题描述 投票:1回答:2

我正在将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
2个回答
3
投票

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

您可以在OnCreate方法中创建一个断点,在点击通知后打开应用程序时,它不会介入其中。


0
投票

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

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

尝试一下,这将解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.