簡體   English   中英

在遠程推送通知重定向到正確的ViewController

[英]On remote push notification redirect to correct ViewController

通過遠程推送通知(使用Swift)打開時,我很難讓我的iOS應用程序按預期運行。 我想要的是,當通過點擊推送通知打開應用程序時,它應該直接跳轉到某個ViewController,但仍然維護導航堆棧。 並且為了使其進一步復雜化,目標視圖控制器依賴於推送消息。

示例:

通常,以下是響應通知的方法。 通過這些方法,您需要實現代碼,該代碼將根據通知向堆棧顯示相應的視圖。

要在應用程序在前台或后台運行時響應通知,請執行以下application:didReceiveRemoteNotification:fetchCompletionHandler: method。 如果您啟用了遠程通知后台模式,系統將啟動您的應用程序(或將其從掛起狀態喚醒),並在遠程通知到達時將其置於后台狀態。 但是,如果用戶強行退出,系統不會自動啟動您的應用。

要在應用程序在前台運行時響應通知,請在后台實現application:didReceiveRemoteNotification: method

要在應用程序未運行時響應通知,請執行以下application:willFinishLaunchingWithOptions: OR application:didFinishLaunchingWithOptions: method

所以我最終做的是:

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導航回來。

我已經在基於@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];
}

做編碼並享受編碼。 歡呼:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM