从 Firebase 通知恢复应用程序不起作用(Xamarin 表单) [英] Resume App from a Firebase Notification is not working (Xamarin Forms)

查看:25
本文介绍了从 Firebase 通知恢复应用程序不起作用(Xamarin 表单)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on integrating firebase push notification to my application.

请找到我的 firebase FirebaseMessagingService 类.

Please find my firebase FirebaseMessagingService class.

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

If the app is open and running everything is working fine. But if the app is not open / if I switch to some other app (my app is not closed). I am getting notification but when I tap on Notification it relaunches the app without resuming.

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

I am using launch mode LaunchMode = LaunchMode.SingleTop in my main activity.

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

And if the app is open I am getting the response in OnNewIntent override method of main activity.

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

Can anyone please help me to figure of the real cause. Please help.

    [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
    }

推荐答案

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

Data messages - Handled by the client app. These messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed. When using this type of message you are the one providing the UI and handling when push notification is received on an Android device.

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

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

Try this, this will solve your issue.

这篇关于从 Firebase 通知恢复应用程序不起作用(Xamarin 表单)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆