在Xamarin iOS中按远程通知的“默认用户操作”推送View Controller

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

我已经设置了远程通知,并为有效负载添加了一些自定义键,如果用户点击通知并希望阅读有关该主题的更多信息,则可以选择在主页上推送特定的详细信息ViewController。

例如:当应用程序到达前台时,linkType: "services"linkPost: "main"会告诉我的应用程序推送主服务页面。

我可以成功访问我的变量topicValuelinkTypelinkPost中的键和值,但是我在调​​试代码在用户点击通知时所采用的路径时遇到问题,因此我知道在何时何地推送正确的ViewController。我正在使用Xamarin.Essentials包,所以我可以GetSet这些关键值对。我可以在应用程序的任何地方访问它们。

public class MyNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
    public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
    {
        if (response.IsDefaultAction)
        {
            var userInfo = response.Notification.Request.Content.UserInfo;

            string topicValue = userInfo.ObjectForKey(new NSString("topic")).ToString();
            string linkType = userInfo.ObjectForKey(new NSString("linktype")).ToString();
            string linkPost = userInfo.ObjectForKey(new NSString("linkpage")).ToString();
            Preferences.Set("linkType", linkType);
            Preferences.Set("linkPost", linkPost);

我最初尝试在AppDelegate中的launchOptions方法中使用FinishedLaunching参数,但是当我设置断点时,当我点击远程通知时它们似乎没有被击中。任何在Xamarin.iOS中完成此任务的帮助将不胜感激。

ios xamarin.ios
2个回答
2
投票

两件事(背景状态或InActive状态)

  • 如果您的应用程序在后台不可用并且您使用通知,那么您应该使用DidFinishLaunching对于这种情况,您应该设置密钥来自通知。您将从launchOptions获取该密钥。比较viewWillAppearHomeViewController中的那个键,如果关键匹配则放入RedirectScreen代码。 例如: - linkType: "services"
  • 如果您的应用程序在后台可用并且通知发出,那么DidReceiveNotificationResponse会调用,您可以重定向应用中的任何屏幕。首先,您应该获取topMostViewController然后使用此控制器重定向到任何屏幕。

例如: - 获得topMostViewController

    public static UIViewController GetTopViewController()
{
    var window = UIApplication.SharedApplication.KeyWindow;
    var vc = window.RootViewController;
    while (vc.PresentedViewController != null)
        vc = vc.PresentedViewController;

    if (vc is UINavigationController navController)
        vc = navController.ViewControllers.Last();

    return vc;
}

0
投票

这是我特定用例的解决方案。再次感谢Nilesh帮助我到达这里:)

public class MyNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
    UIViewController detailViewController;
    public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
    {
        UIApplication application = UIApplication.SharedApplication;
        if (response.IsDefaultAction)
        {
            var userInfo = response.Notification.Request.Content.UserInfo;

            string linkType = userInfo.ObjectForKey(new NSString("linktype")).ToString();
            string linkPost = userInfo.ObjectForKey(new NSString("linkpage")).ToString();

//I'm using Xamarin.Essentials package to manage my User Defaults Key Value pairs with the
// Preferences method. Then I can see if those keys are set or not in ViewDidLoad in 
// my HomeViewController. This covers the case if the app had been closed 
//when the notification is tapped.

            Preferences.Set("linkType", linkType);
            Preferences.Set("linkPost", linkPost);

            var window = UIApplication.SharedApplication.KeyWindow;
            var tc = window.RootViewController as UITabBarController;

            if(tc != null) 
            {
//this is essential. if app was in background then `tc != null` and this logic can run here.
//If the app was closed and `tc == null` then this logic will be carried out at the end of my 
// ViewDidLoad method in my HomeViewController instead of this method.

// carry out your logic for your app to either push a view controller or
// select a TabBarController index ...

//if you are pushing a detail view controller on your Home Navigation Controller
              var homeNavController = tc.ChildViewControllers[0] as UINavigationController;
              homeNavController.PushViewController(detailViewController, true);

              tc.SelectedIndex = 0; //select my HomeViewController via TabBarController index
//HomeViewController is where I'm pushing the detail controller so I want to make to navigate in case the user backgrounded the app on another TabBarController index.
            }
      }

   completionHandler();
}

所以除此之外,我需要在我的ViewDidLoad中的HomeViewController方法的末尾执行相同的导航逻辑(选择要推送的视图控制器或选择哪个TabBarController索引)。您可以访问设置的“首选项”键以选择导航位置。这包括当用户点击通知时关闭应用程序的情况。

希望这有助于其他人尝试在Xamarin.iOS中使用他们的远程通知来完成这种“深度链接”!

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