From cc90c0005f6c3c2eaeaedd6b6ea703730dcabe64 Mon Sep 17 00:00:00 2001 From: emawby Date: Thu, 2 Sep 2021 15:26:35 -0700 Subject: [PATCH 1/2] Always immediately register once we get an APNs token Previously we would wait for the notification permission prompt to be completed before we registered the user. This is not a correct optimization so we are removing that behavior. --- iOS_SDK/OneSignalSDK/Source/OneSignal.m | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/iOS_SDK/OneSignalSDK/Source/OneSignal.m b/iOS_SDK/OneSignalSDK/Source/OneSignal.m index 7835a7d0c..53bbc2db2 100755 --- a/iOS_SDK/OneSignalSDK/Source/OneSignal.m +++ b/iOS_SDK/OneSignalSDK/Source/OneSignal.m @@ -1631,18 +1631,10 @@ + (void)updateDeviceToken:(NSString*)deviceToken { let isPushTokenDifferent = ![deviceToken isEqualToString:self.currentSubscriptionState.pushToken]; self.currentSubscriptionState.pushToken = deviceToken; - // iOS 9+ - We get a token right away but give the user 30 sec to respond notification permission prompt. - // The goal is to only have 1 server call. - [self.osNotificationSettings getNotificationPermissionState:^(OSPermissionState *status) { - if (status.answeredPrompt || status.provisional) { - if ([self shouldRegisterNow]) - [self registerUser]; - else if (isPushTokenDifferent) - [self playerPutForPushTokenAndNotificationTypes]; - } else { - [self registerUserAfterDelay]; - } - }]; + if ([self shouldRegisterNow]) + [self registerUser]; + else if (isPushTokenDifferent) + [self playerPutForPushTokenAndNotificationTypes]; } + (void)playerPutForPushTokenAndNotificationTypes { From 66b3d4f6a70a76f2212c1900424759c839a5422e Mon Sep 17 00:00:00 2001 From: emawby Date: Thu, 2 Sep 2021 15:27:06 -0700 Subject: [PATCH 2/2] Fixing unit tests to account for immediate register user change This includes other unit test fixes for broken in app messaging tests that was caused by the IAM lifecycle project --- iOS_SDK/OneSignalSDK/UnitTests/MigrationTests.m | 2 +- .../OSInAppMessageViewControllerOverrider.m | 5 +++++ .../Shadows/OSMessagingControllerOverrider.h | 2 +- .../Shadows/OSMessagingControllerOverrider.m | 4 ++-- iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m | 17 ++++++----------- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/iOS_SDK/OneSignalSDK/UnitTests/MigrationTests.m b/iOS_SDK/OneSignalSDK/UnitTests/MigrationTests.m index 7c1675e54..b2ecbe4d8 100644 --- a/iOS_SDK/OneSignalSDK/UnitTests/MigrationTests.m +++ b/iOS_SDK/OneSignalSDK/UnitTests/MigrationTests.m @@ -257,7 +257,7 @@ - (void)testIAMCachedDictionaryToCachedCodeableMigration { NSDictionary*retrievedDict = [OneSignalUserDefaults.initStandard getSavedCodeableDataForKey:OS_IAM_REDISPLAY_DICTIONARY defaultValue:nil]; - XCTAssertEqualObjects(emptyDict, retrievedDict); + XCTAssertEqualObjects(nil, retrievedDict); } - (void)testIAMCachedCodeableMigration { diff --git a/iOS_SDK/OneSignalSDK/UnitTests/Shadows/OSInAppMessageViewControllerOverrider.m b/iOS_SDK/OneSignalSDK/UnitTests/Shadows/OSInAppMessageViewControllerOverrider.m index 3f0f18ba1..a77a41ded 100644 --- a/iOS_SDK/OneSignalSDK/UnitTests/Shadows/OSInAppMessageViewControllerOverrider.m +++ b/iOS_SDK/OneSignalSDK/UnitTests/Shadows/OSInAppMessageViewControllerOverrider.m @@ -36,6 +36,7 @@ + (void)load { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wundeclared-selector" injectToProperClass(@selector(overrideAnimateAppearance), @selector(animateAppearance), @[], [OSInAppMessageViewControllerOverrider class], [OSInAppMessageViewController class]); + injectToProperClass(@selector(overrideAnimateAppearance:), @selector(animateAppearance:), @[], [OSInAppMessageViewControllerOverrider class], [OSInAppMessageViewController class]); #pragma clang diagnostic pop } @@ -43,4 +44,8 @@ - (void)overrideAnimateAppearance { } +- (void)overrideAnimateAppearance:(BOOL)firstDisplay { + +} + @end diff --git a/iOS_SDK/OneSignalSDK/UnitTests/Shadows/OSMessagingControllerOverrider.h b/iOS_SDK/OneSignalSDK/UnitTests/Shadows/OSMessagingControllerOverrider.h index 25f6595bf..c0b748ed9 100644 --- a/iOS_SDK/OneSignalSDK/UnitTests/Shadows/OSMessagingControllerOverrider.h +++ b/iOS_SDK/OneSignalSDK/UnitTests/Shadows/OSMessagingControllerOverrider.h @@ -48,7 +48,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)resetState; - (void)messageViewDidSelectAction:(OSInAppMessageInternal *)message withAction:(OSInAppMessageAction *)action; - (void)persistInAppMessageForRedisplay:(OSInAppMessageInternal *)message; -- (void)messageViewControllerWasDismissed; +- (void)messageViewControllerWasDismissed:(OSInAppMessageInternal *)message displayed:(BOOL)displayed; - (void)setLastTimeGenerator:(NSTimeInterval(^)(void))dateGenerator; - (NSArray *)getInAppMessages; - (NSMutableDictionary *)getRedisplayedInAppMessages; diff --git a/iOS_SDK/OneSignalSDK/UnitTests/Shadows/OSMessagingControllerOverrider.m b/iOS_SDK/OneSignalSDK/UnitTests/Shadows/OSMessagingControllerOverrider.m index 68b6f0905..d2aa25b67 100644 --- a/iOS_SDK/OneSignalSDK/UnitTests/Shadows/OSMessagingControllerOverrider.m +++ b/iOS_SDK/OneSignalSDK/UnitTests/Shadows/OSMessagingControllerOverrider.m @@ -94,7 +94,7 @@ + (void)load { - (void)overrideShowMessage:(OSInAppMessageInternal *)message { dispatch_async(dispatch_get_main_queue(), ^{ - let viewController = [[OSInAppMessageViewController alloc] initWithMessage:message delegate:OSMessagingController.self]; + let viewController = [[OSInAppMessageViewController alloc] initWithMessage:message delegate:OSMessagingController.sharedInstance]; [viewController viewDidLoad]; [OSMessagingController.sharedInstance webViewContentFinishedLoading:message]; }); @@ -107,7 +107,7 @@ - (void)overrideWebViewContentFinishedLoading:(OSInAppMessageInternal *)message } + (void)dismissCurrentMessage { - [OSMessagingController.sharedInstance messageViewControllerWasDismissed]; + [OSMessagingController.sharedInstance messageViewControllerWasDismissed: self.messageDisplayQueue.firstObject displayed:YES]; } + (BOOL)isInAppMessageShowing { diff --git a/iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m b/iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m index 412222759..398786cf0 100644 --- a/iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m +++ b/iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m @@ -698,16 +698,12 @@ - (void)testPromptForPushNotificationsWithUserResponseOnIOS9 { XCTAssertTrue(didAccept); } +// We should always register right away regardless of notification prompt status - (void)testPromptedButNeveranswerNotificationPrompt { [UnitTestCommonMethods setCurrentNotificationPermissionAsUnanswered]; [OneSignal promptForPushNotificationsWithUserResponse:nil]; [UnitTestCommonMethods initOneSignal_andThreadWait]; - // Don't make a network call right away - XCTAssertNil(OneSignalClientOverrider.lastHTTPRequest); - - // Triggers the 30 fallback to register device right away. - [OneSignal performSelector:NSSelectorFromString(@"registerUser")]; [UnitTestCommonMethods runBackgroundThreads]; XCTAssertEqualObjects(OneSignalClientOverrider.lastHTTPRequest[@"app_id"], @"b2f7f966-d8cc-11e4-bed1-df8f05be55ba"); @@ -1498,26 +1494,25 @@ - (void)testGetTagsWithNestedDelete { } - (void)testSendTagsBeforeRegisterComplete { - [UnitTestCommonMethods setCurrentNotificationPermissionAsUnanswered]; - [UnitTestCommonMethods initOneSignal]; + [OneSignal sendTag:@"key" value:@"value"]; [UnitTestCommonMethods foregroundApp]; [UnitTestCommonMethods runBackgroundThreads]; NSObjectOverrider.selectorNamesForInstantOnlyForFirstRun = [@[@"sendTagsToServer"] mutableCopy]; - [OneSignal sendTag:@"key" value:@"value"]; + [UnitTestCommonMethods runBackgroundThreads]; // Do not try to send tag update yet as there isn't a player_id yet. - XCTAssertEqual(OneSignalClientOverrider.networkRequestCount, 1); + XCTAssertEqual(OneSignalClientOverrider.networkRequestCount, 0); - [UnitTestCommonMethods answerNotificationPrompt:false]; + [UnitTestCommonMethods initOneSignal]; [UnitTestCommonMethods runBackgroundThreads]; // A single POST player create call should be made with tags included. XCTAssertEqual(OneSignalClientOverrider.networkRequestCount, 2); XCTAssertEqualObjects(OneSignalClientOverrider.lastHTTPRequest[@"tags"][@"key"], @"value"); - XCTAssertEqualObjects(OneSignalClientOverrider.lastHTTPRequest[@"notification_types"], @0); + XCTAssertEqualObjects(OneSignalClientOverrider.lastHTTPRequest[@"notification_types"], @15); XCTAssertEqualObjects(OneSignalClientOverrider.lastHTTPRequest[@"identifier"], @"0000000000000000000000000000000000000000000000000000000000000000"); }