可以檢測是否從通知開這個代碼在應用程序代理應用程序。在應用程序變爲活動狀態之前,當應用程序狀態爲UIApplicationStateInactive
時,您需要設置初始視圖控制器。您可以在那裏執行任何邏輯來決定應該打開哪個視圖控制器以及該視圖控制器應該顯示哪些內容。
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
if(application.applicationState == UIApplicationStateActive) {
//app is currently active, can update badges count here
} else if(application.applicationState == UIApplicationStateBackground){
//app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
} else if(application.applicationState == UIApplicationStateInactive){
//app is transitioning from background to foreground (user taps notification), do what you need when user taps here
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
}
}
謝謝你這麼多。第三個id語句是你從哪裏*實際*從背景到前景......前兩個只是在沒有任何UI交互的情況下更新模型。 **對?**。所以,基本上你通過viewController的標識符**正確嗎?** – Honey
對不起,我花了幾天的時間來評論,這是正確的。 –