我已经设置了“远程通知”,并向有效负载添加了一些自定义键,如果用户点击通知并想阅读有关该主题的更多信息,它将提供在主页上推送特定详细信息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中完成此操作的任何帮助将不胜感激。
答案 0 :(得分:2)
两件事(背景状态或非活动状态)
如果您的应用程序在后台不可用,并且您使用通知功能,则应使用DidFinishLaunching
对于这种情况,您应该设置其密钥来自通知。然后您将从launchOptions获取该密钥。
比较viewWillAppear
中HomeViewController
中的密钥,如果密钥匹配,则放入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;
}
答案 1 :(得分: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
的{{1}}方法的末尾执行相同的导航逻辑(选择要推入的视图控制器或选择要选择的TabBarController索引)。您可以访问设置的首选项键,以选择要导航的位置。这涵盖了当用户点击通知时应用程序关闭的情况。
希望这可以帮助其他尝试通过Xamarin.iOS中的远程通知完成这种“深度链接”的人!