diff --git a/WordPress/Classes/NotificationsManager.m b/WordPress/Classes/NotificationsManager.m index 3951ed921fba..a74da6c3f2bb 100644 --- a/WordPress/Classes/NotificationsManager.m +++ b/WordPress/Classes/NotificationsManager.m @@ -58,6 +58,8 @@ + (void)registrationDidFail:(NSError *)error { } + (void)unregisterDeviceToken { + [[UIApplication sharedApplication] unregisterForRemoteNotifications]; + NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:NotificationsDeviceToken]; if (nil == token) { return; @@ -93,7 +95,7 @@ + (void)unregisterDeviceToken { #pragma mark - Notification handling -+ (void)handleNotification:(NSDictionary*)userInfo forState:(UIApplicationState)state completionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { ++ (void)handleNotification:(NSDictionary *)userInfo forState:(UIApplicationState)state completionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { DDLogInfo(@"Received push notification:\nPayload: %@\nCurrent Application state: %d", userInfo, state); switch (state) { @@ -108,9 +110,6 @@ + (void)handleNotification:(NSDictionary*)userInfo forState:(UIApplicationState) break; case UIApplicationStateBackground: - [WPMobileStats recordAppOpenedForEvent:StatsEventAppOpenedDueToPushNotification]; - [[WordPressAppDelegate sharedWordPressApplicationDelegate] showNotificationsTab]; - if (completionHandler) { [Note getNewNotificationswithContext:[[ContextManager sharedInstance] mainContext] success:^(BOOL hasNewNotes) { DDLogInfo(@"notification fetch completion handler completed with new notes: %@", hasNewNotes ? @"YES" : @"NO"); diff --git a/WordPress/Classes/WordPressAppDelegate.m b/WordPress/Classes/WordPressAppDelegate.m index 0020a150233b..3eb43fb0e513 100644 --- a/WordPress/Classes/WordPressAppDelegate.m +++ b/WordPress/Classes/WordPressAppDelegate.m @@ -83,11 +83,13 @@ - (void)dealloc { #pragma mark - UIApplicationDelegate - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + [WordPressAppDelegate fixKeychainAccess]; + // Crash reporting, logging, debugging [self configureLogging]; [self configureHockeySDK]; [self configureCrashlytics]; - [self printDebugLaunchInfo]; + [self printDebugLaunchInfoWithLaunchOptions:launchOptions]; [self toggleExtraDebuggingIfNeeded]; [self removeCredentialsForDebug]; @@ -106,24 +108,12 @@ - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions: [self customizeAppearance]; - CGRect bounds = [[UIScreen mainScreen] bounds]; - [self.window setFrame:bounds]; - [self.window setBounds:bounds]; // for good measure. - - self.window.backgroundColor = [UIColor blackColor]; - self.window.rootViewController = self.tabBarController; - [self.window makeKeyAndVisible]; - - [self showWelcomeScreenIfNeededAnimated:NO]; - // Push notifications [NotificationsManager registerForPushNotifications]; - [NotificationsManager handleNotificationForApplicationLaunch:launchOptions]; // Deferred tasks to speed up app launch dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ [self changeCurrentDirectory]; - [WordPressAppDelegate fixKeychainAccess]; [[PocketAPI sharedAPI] setConsumerKey:[WordPressComApiCredentials pocketConsumerKey]]; [self cleanUnusedMediaFileFromTmpDir]; }); @@ -131,8 +121,28 @@ - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions: return YES; } -- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation -{ +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + DDLogVerbose(@"didFinishLaunchingWithOptions state: %d", application.applicationState); + + // Launched by tapping a notification + if (application.applicationState == UIApplicationStateActive) { + [NotificationsManager handleNotificationForApplicationLaunch:launchOptions]; + } + + CGRect bounds = [[UIScreen mainScreen] bounds]; + [self.window setFrame:bounds]; + [self.window setBounds:bounds]; // for good measure. + + self.window.backgroundColor = [UIColor blackColor]; + self.window.rootViewController = self.tabBarController; + [self.window makeKeyAndVisible]; + + [self showWelcomeScreenIfNeededAnimated:NO]; + + return YES; +} + +- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { BOOL returnValue = NO; if ([[BITHockeyManager sharedHockeyManager].authenticator handleOpenURL:url @@ -238,8 +248,8 @@ - (void)applicationDidEnterBackground:(UIApplication *)application { }]; } -- (void)applicationWillEnterForeground:(UIApplication *)application -{ +- (void)applicationWillEnterForeground:(UIApplication *)application { + DDLogInfo(@"%@ %@", self, NSStringFromSelector(_cmd)); [WPMobileStats resumeSession]; } @@ -257,13 +267,11 @@ - (void)applicationDidBecomeActive:(UIApplication *)application { [[WordPressComApi sharedApi] syncPushNotificationInfo]; } -- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder -{ +- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder { return YES; } -- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder -{ +- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder { return YES; } @@ -924,7 +932,7 @@ + (void)fixKeychainAccess #pragma mark - Debugging and logging -- (void)printDebugLaunchInfo { +- (void)printDebugLaunchInfoWithLaunchOptions:(NSDictionary *)launchOptions { UIDevice *device = [UIDevice currentDevice]; NSInteger crashCount = [[NSUserDefaults standardUserDefaults] integerForKey:@"crashCount"]; NSArray *languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"]; @@ -945,6 +953,7 @@ - (void)printDebugLaunchInfo { DDLogInfo(@"Language: %@", currentLanguage); DDLogInfo(@"UDID: %@", [device wordpressIdentifier]); DDLogInfo(@"APN token: %@", [[NSUserDefaults standardUserDefaults] objectForKey:NotificationsDeviceToken]); + DDLogInfo(@"Launch options: %@", launchOptions); DDLogInfo(@"==========================================================================="); }