From c64cd809add4f2eadf3f640ef410ffa930482b1b Mon Sep 17 00:00:00 2001 From: Aaron Douglas Date: Fri, 3 Jan 2014 09:02:26 -0600 Subject: [PATCH 1/4] =?UTF-8?q?Don=E2=80=99t=20show=20the=20notifications?= =?UTF-8?q?=20tab=20when=20a=20background=20push=20is=20received?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WordPress/Classes/NotificationsManager.m | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) 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"); From f2df406eaf764bdb670c4bc98a0bbd599ba449a8 Mon Sep 17 00:00:00 2001 From: Aaron Douglas Date: Fri, 3 Jan 2014 09:02:59 -0600 Subject: [PATCH 2/4] Call unregister for push in the WP.com API - needs a better place --- WordPress/WordPressApi/WordPressComApi.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/WordPress/WordPressApi/WordPressComApi.m b/WordPress/WordPressApi/WordPressComApi.m index ca7cf3214cd2..e28e2872673b 100644 --- a/WordPress/WordPressApi/WordPressComApi.m +++ b/WordPress/WordPressApi/WordPressComApi.m @@ -213,7 +213,8 @@ - (void)signOut { DDLogMethod(); NSError *error = nil; -// [NotificationsManager unregisterDeviceToken]; + // FIXME : This needs to be called outside of the WP.com API + [NotificationsManager unregisterDeviceToken]; [SFHFKeychainUtils deleteItemForUsername:self.username andServiceName:@"WordPress.com" error:&error]; [SFHFKeychainUtils deleteItemForUsername:self.username andServiceName:WPComXMLRPCUrl error:&error]; From 64051833b31cbae6cb065b67960c754fdacd7f15 Mon Sep 17 00:00:00 2001 From: Aaron Douglas Date: Fri, 3 Jan 2014 09:04:29 -0600 Subject: [PATCH 3/4] Moved items to didFinishLaunching, force keychain fix first, only handle notification if active When a background push launches the app (which it can do in iOS 7) the notification was being handled twice. Ensure that the app is in an active state when attempting to handle a notification sent through to the didFinishLaunching. --- WordPress/Classes/WordPressAppDelegate.m | 53 ++++++++++++++---------- 1 file changed, 31 insertions(+), 22 deletions(-) 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(@"==========================================================================="); } From 67864b120dc8cb300a047d77e241db346ddc475c Mon Sep 17 00:00:00 2001 From: Aaron Douglas Date: Fri, 3 Jan 2014 09:51:28 -0600 Subject: [PATCH 4/4] Reversing change made to WordPress.com API in favor of mods from #22 PR #999 --- WordPress/WordPressApi/WordPressComApi.m | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/WordPress/WordPressApi/WordPressComApi.m b/WordPress/WordPressApi/WordPressComApi.m index e28e2872673b..ca7cf3214cd2 100644 --- a/WordPress/WordPressApi/WordPressComApi.m +++ b/WordPress/WordPressApi/WordPressComApi.m @@ -213,8 +213,7 @@ - (void)signOut { DDLogMethod(); NSError *error = nil; - // FIXME : This needs to be called outside of the WP.com API - [NotificationsManager unregisterDeviceToken]; +// [NotificationsManager unregisterDeviceToken]; [SFHFKeychainUtils deleteItemForUsername:self.username andServiceName:@"WordPress.com" error:&error]; [SFHFKeychainUtils deleteItemForUsername:self.username andServiceName:WPComXMLRPCUrl error:&error];