在远程推送通知上重定向以更正 ViewController
On remote push notification redirect to correct ViewController
我正在努力让我的 iOS 应用程序在通过远程推送通知(使用 Swift)打开时按预期运行。我想要的是,当通过点击推送通知打开应用程序时,它应该直接跳转到某个 ViewController,但仍保持导航堆栈。更复杂的是,目标视图控制器取决于推送消息。
示例: 我的应用已关闭,我收到推送通知:"You got a new message"。我单击通知,应用程序打开并显示新消息,而不是常规的初始视图控制器。如果我的应用程序打开并且我收到推送通知,则什么也不会发生。
一般来说,这里是响应通知的方法。通过这些方法,您需要实现代码以根据通知向您的堆栈显示适当的视图。
要在应用 运行 在前台或后台运行时响应通知,请实施 application:didReceiveRemoteNotification:fetchCompletionHandler:
方法。如果您启用了远程通知后台模式,系统会启动您的应用程序(或将其从挂起状态唤醒)并在远程通知到达时将其置于后台状态。但是,如果用户强行退出,系统不会自动启动您的应用程序。
要在您的应用 运行 在前台而非后台时响应通知,请实施 application:didReceiveRemoteNotification:
方法
要在您的应用不是 运行 时响应通知,请实施 application:willFinishLaunchingWithOptions:
或 application:didFinishLaunchingWithOptions:
方法
所以我最后做的是:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
if application.applicationState == .Inactive || application.applicationState == .Background {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = self.window?.rootViewController as? UINavigationController
let destinationController = storyboard.instantiateViewControllerWithIdentifier("dashboard") as? DashboardViewController
navigationController?.pushViewController(destinationController!, animated: false)
let destinationController2 = storyboard.instantiateViewControllerWithIdentifier("notificationsSettings") as? AppSettingsTableViewController
navigationController?.pushViewController(destinationController2!, animated: false)
}
}
所以在 didReceiveRemoteNotification
中,我检查应用程序来自哪个状态,然后导航到我想呈现给用户的 viewController。我不直接转到 ViewController 的原因是因为我希望导航堆栈是 "intact" 以便用户可以通过 navigationController 导航回来。
我在 Objective C 中做了同样的事情,基于 @NikMos 回答
// Handle notification messages after display notification is tapped by the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
#if defined(__IPHONE_11_0)
withCompletionHandler:(void(^)(void))completionHandler {
#else
withCompletionHandler:(void(^)())completionHandler {
#endif
if (([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) ||
([UIApplication sharedApplication].applicationState == UIApplicationStateInactive)) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *navigationController=[[UINavigationController alloc] init];
self.window.rootViewController =nil;
self.window.rootViewController = navigationController;
ScannerViewController *scannerVc = [storyboard instantiateViewControllerWithIdentifier:@"ScannerID"];
[navigationController pushViewController:scannerVc animated:YES];
NotificationVC * notificationVC = [storyboard instantiateViewControllerWithIdentifier:@"NotificationVCID"];
[navigationController presentViewController:notificationVC animated:YES completion:nil];
[self.window makeKeyAndVisible];
}
编码并享受编码。干杯 :)
我正在努力让我的 iOS 应用程序在通过远程推送通知(使用 Swift)打开时按预期运行。我想要的是,当通过点击推送通知打开应用程序时,它应该直接跳转到某个 ViewController,但仍保持导航堆栈。更复杂的是,目标视图控制器取决于推送消息。
示例: 我的应用已关闭,我收到推送通知:"You got a new message"。我单击通知,应用程序打开并显示新消息,而不是常规的初始视图控制器。如果我的应用程序打开并且我收到推送通知,则什么也不会发生。
一般来说,这里是响应通知的方法。通过这些方法,您需要实现代码以根据通知向您的堆栈显示适当的视图。
要在应用 运行 在前台或后台运行时响应通知,请实施 application:didReceiveRemoteNotification:fetchCompletionHandler:
方法。如果您启用了远程通知后台模式,系统会启动您的应用程序(或将其从挂起状态唤醒)并在远程通知到达时将其置于后台状态。但是,如果用户强行退出,系统不会自动启动您的应用程序。
要在您的应用 运行 在前台而非后台时响应通知,请实施 application:didReceiveRemoteNotification:
方法
要在您的应用不是 运行 时响应通知,请实施 application:willFinishLaunchingWithOptions:
或 application:didFinishLaunchingWithOptions:
方法
所以我最后做的是:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
if application.applicationState == .Inactive || application.applicationState == .Background {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = self.window?.rootViewController as? UINavigationController
let destinationController = storyboard.instantiateViewControllerWithIdentifier("dashboard") as? DashboardViewController
navigationController?.pushViewController(destinationController!, animated: false)
let destinationController2 = storyboard.instantiateViewControllerWithIdentifier("notificationsSettings") as? AppSettingsTableViewController
navigationController?.pushViewController(destinationController2!, animated: false)
}
}
所以在 didReceiveRemoteNotification
中,我检查应用程序来自哪个状态,然后导航到我想呈现给用户的 viewController。我不直接转到 ViewController 的原因是因为我希望导航堆栈是 "intact" 以便用户可以通过 navigationController 导航回来。
我在 Objective C 中做了同样的事情,基于 @NikMos 回答
// Handle notification messages after display notification is tapped by the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
#if defined(__IPHONE_11_0)
withCompletionHandler:(void(^)(void))completionHandler {
#else
withCompletionHandler:(void(^)())completionHandler {
#endif
if (([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) ||
([UIApplication sharedApplication].applicationState == UIApplicationStateInactive)) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *navigationController=[[UINavigationController alloc] init];
self.window.rootViewController =nil;
self.window.rootViewController = navigationController;
ScannerViewController *scannerVc = [storyboard instantiateViewControllerWithIdentifier:@"ScannerID"];
[navigationController pushViewController:scannerVc animated:YES];
NotificationVC * notificationVC = [storyboard instantiateViewControllerWithIdentifier:@"NotificationVCID"];
[navigationController presentViewController:notificationVC animated:YES completion:nil];
[self.window makeKeyAndVisible];
}
编码并享受编码。干杯 :)