我已经设置了远程通知,并为有效负载添加了一些自定义键,如果用户点击通知并希望阅读有关该主题的更多信息,则可以选择在主页上推送特定的详细信息ViewController。
例如:当应用程序到达前台时,linkType: "services"
和linkPost: "main"
会告诉我的应用程序推送主服务页面。
我可以成功访问我的变量topicValue
,linkType
和linkPost
中的键和值,但是我在调试代码在用户点击通知时所采用的路径时遇到问题,因此我知道在何时何地推送正确的ViewController。我正在使用Xamarin.Essentials包,所以我可以Get
和Set
这些关键值对。我可以在应用程序的任何地方访问它们。
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中完成此任务的帮助将不胜感激。
两件事(背景状态或InActive状态)
DidFinishLaunching
对于这种情况,您应该设置密钥来自通知。您将从launchOptions获取该密钥。比较viewWillAppear
的HomeViewController
中的那个键,如果关键匹配则放入RedirectScreen
代码。
例如: - linkType: "services"
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;
}
这是我特定用例的解决方案。再次感谢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中使用他们的远程通知来完成这种“深度链接”!