在远程推送通知重定向到正确的ViewController

问题描述 投票:4回答:3

通过远程推送通知(使用Swift)打开时,我很难让我的iOS应用程序按预期运行。我想要的是,当通过点击推送通知打开应用程序时,它应该直接跳转到某个ViewController,但仍然维护导航堆栈。并且为了使其进一步复杂化,目标视图控制器依赖于推送消息。 示例:我的应用程序已关闭,我收到推送通知:“您收到了新消息”。我单击通知,应用程序打开并显示新消息,而不是常规初始视图控制器。如果我的应用程序打开并且我收到推送通知,则没有任何反应。

ios swift push-notification
3个回答
9
投票

通常,以下是响应通知的方法。通过这些方法,您需要实现代码,该代码将根据通知向堆栈显示相应的视图。

要在应用程序在前台或后台运行时响应通知,请执行application:didReceiveRemoteNotification:fetchCompletionHandler:方法。如果您启用了远程通知后台模式,系统将启动您的应用程序(或将其从挂起状态唤醒),并在远程通知到达时将其置于后台状态。但是,如果用户强行退出,系统不会自动启动您的应用。

要在应用程序在前台运行时响应通知,请在后台实现application:didReceiveRemoteNotification:方法

要在应用未运行时响应通知,请执行application:willFinishLaunchingWithOptions:application:didFinishLaunchingWithOptions:方法


9
投票

所以我最终做的是:

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的原因是因为我希望导航堆栈“完整”,以便用户可以通过navigationController导航回来。


0
投票

我已经在基于@NikMos答案的Objective C中做了同样的事情

// 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];
}

做编码并享受编码。欢呼:)

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