【问题标题】:Xamarin.ios firebase push notification click issueXamarin.ios firebase 推送通知点击问题
【发布时间】:2020-04-20 17:35:25
【问题描述】:

我有一个 xamarin.forms 应用程序,它实现了 firebase 推送通知。目前我正在玩 ios 部分。当应用程序处于前台、后台和被杀死时,我可以收到通知。问题是处理通知点击。我正在尝试从DidReceiveNotificationResponse 拨打MessagingCenter 方法以打开内容页面(表单中的我的通知详细信息页面)。我有两个疑问

  1. 我只处理来自DidReceiveNotificationResponse 的通知点击 方法。我可以处理这种方法的通知。目前我正在使用ios 12.4.6 在 Iphone 上进行测试。处理较低操作系统版本的通知是否还有其他事情要做?还是对所有操作系统版本都足够了?

  2. 当APP处于关闭或被杀状态时如何处理通知点击?目前应用在所有场景下都会导航到我的特定页面,但在被杀状态下,应用本身会在加载后关闭消息详情页面并自动导航到主屏幕。如何解决这个问题?

我的 DidReceiveNotificationResponse

[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
    public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action
    completionHandler)
    {
        completionHandler();
        NSDictionary userInfo = response.Notification.Request.Content.UserInfo;
        var a = userInfo[new NSString("user_notification_id")] as NSString;
        try
        {            
            var myData = JsonConvert.DeserializeObject(userInfo[new NSString("user_notification_id")] as NSString);

            if (myData != null)
            {
                Settings.NotificationID = myData.ToString();
                Settings.NCNotificationStatus = null;
            }              
        }

        catch (Exception ex)
        {

        }

        // Managing notification click here
        MessagingCenter.Send<Object>(new Object(), "iosNcnotificationTapped");
    }

我的 App.xaml.cs

public partial class App : Application
    {
        public string Isnotification;
        public App(bool hasNotification = false)
        {       
            InitializeComponent();
            if (hasNotification)
            {
                var navPage = new NavigationPage(new LandingPage());
                Application.Current.MainPage = navPage;
                navPage.Navigation.PushModalAsync(new ChatPage("26"));        
            }
            else
            {                         
                var splashPage = new CustomRender.TransitionNavigationPage(new SplashPage());
                MainPage = splashPage;            
            }   
                MessagingCenter.Subscribe<Object>(this, "iosNcnotificationTapped", async (sender) => {                  
                        var navPage = new NavigationPage(new LandingPage());
                        Application.Current.MainPage = navPage;
                        await navPage.Navigation.PushModalAsync(new ChatPage("26"));

                });
        }

        protected override void OnStart()
        { 

        }
        protected override void OnSleep()
        {  

        }
        protected override void OnResume()
        {   

        }

    }

hasNotification 部分用于安卓部分。对于ios来说总是假的。如何解决这些问题?

【问题讨论】:

  • 1.如果它适用于 iOS 12,我认为它也适用于较低版本。 2 试试this solution。这是一个 swift 版本,如果你需要,我可以将它翻译成 C#。
  • @JackHua-MSFT 感谢您的回复。我认为我的问题与 APP.xaml.cs 部分有关。当我们点击通知时,我的想法是加载应用程序部分也在调用。问题仅在应用被杀死状态
  • @JackHua-MSFT 您能帮我将您建议的解决方案转换为表单方式吗?
  • 我添加了一个答案作为例子。

标签: xamarin xamarin.forms


【解决方案1】:

this thread中解决方案的C#版本

在您的 iOS 项目中:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{

    //if (launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification]) != nil {
    //    // check the userInfo and take the needed action
    //}

    if (options != null)
    {
        NSObject result;
        if (options.TryGetValue(UIApplication.LaunchOptionsRemoteNotificationKey, out result))
        {
            //Get remoteNotification
        }
    }

    global::Xamarin.Forms.Forms.Init();
    LoadApplication(new App());

    return base.FinishedLaunching(app, options);
}

【讨论】:

  • 谢谢杰克。让我检查一下
  • 您可以关注this doc了解更多信息。
  • 我仍然坚持这一点,因为我在 DidReceiveNotificationResponse 中的消息中心即使处于终止状态也会命中。那么我应该如何在这个场景中使用这个答案呢?
  • 既然 DidReceiveNotificationResponse 在被杀状态下命中,你能在那个方法中实现你想要的吗?如果没有,请在答案中使用该解决方案。
  • 兄弟。抱歉回复晚了。即使应用程序处于终止状态,它也会很热。并且消息中心部分也会命中。我想要的页面也将打开。但是在加载所需的页面后,它突然导航到处于终止状态的起始页面。它是问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多