diff --git a/Sources/mParticle-Appboy/MPKitAppboy.m b/Sources/mParticle-Appboy/MPKitAppboy.m index 4376a94..e32c75d 100644 --- a/Sources/mParticle-Appboy/MPKitAppboy.m +++ b/Sources/mParticle-Appboy/MPKitAppboy.m @@ -49,8 +49,10 @@ #if TARGET_OS_IOS __weak static id inAppMessageControllerDelegate = nil; +static BOOL shouldDisableNotificationHandling = NO; #endif __weak static id urlDelegate = nil; +static Braze *brazeInstance = nil; @interface MPKitAppboy() { Braze *appboyInstance; @@ -83,6 +85,15 @@ + (void)setInAppMessageControllerDelegate:(id)delegate { + (id)inAppMessageControllerDelegate { return inAppMessageControllerDelegate; } + ++ (void)setShouldDisableNotificationHandling:(BOOL)isDisabled { + shouldDisableNotificationHandling = isDisabled; +} + ++ (BOOL)shouldDisableNotificationHandling { + return shouldDisableNotificationHandling; +} + #endif + (void)setURLDelegate:(id)delegate { @@ -93,6 +104,16 @@ + (void)setURLDelegate:(id)delegate { return urlDelegate; } ++ (void)setBrazeInstance:(id)instance { + if ([instance isKindOfClass:[Braze class]]) { + brazeInstance = instance; + } +} + ++ (Braze *)brazeInstance { + return brazeInstance; +} + #pragma mark Private methods - (NSString *)stringRepresentation:(id)value { NSString *stringRepresentation = nil; @@ -239,6 +260,10 @@ - (NSString *)advertisingIdentifierString { #pragma mark MPKitInstanceProtocol methods - (MPKitExecStatus *)didFinishLaunchingWithConfiguration:(NSDictionary *)configuration { + + // Use the static braze instance if set + [self setAppboyInstance:brazeInstance]; + MPKitExecStatus *execStatus = nil; if (!configuration[eabAPIKey]) { @@ -266,6 +291,8 @@ - (MPKitExecStatus *)didFinishLaunchingWithConfiguration:(NSDictionary *)configu _started = NO; } + + execStatus = [[MPKitExecStatus alloc] initWithSDKCode:[[self class] kitCode] returnCode:MPKitReturnCodeSuccess]; return execStatus; } @@ -557,6 +584,10 @@ - (MPKitExecStatus *)logScreen:(MPEvent *)event { - (MPKitExecStatus *)receivedUserNotification:(NSDictionary *)userInfo { MPKitExecStatus *execStatus = [[MPKitExecStatus alloc] initWithSDKCode:@(MPKitInstanceAppboy) returnCode:MPKitReturnCodeSuccess]; + + if (!shouldDisableNotificationHandling) { + return execStatus; + } #if TARGET_OS_IOS if (![appboyInstance.notifications handleBackgroundNotificationWithUserInfo:userInfo fetchCompletionHandler:^(UIBackgroundFetchResult fetchResult) {}]) { @@ -575,11 +606,16 @@ - (MPKitExecStatus *)removeUserAttribute:(NSString *)key { } - (MPKitExecStatus *)setDeviceToken:(NSData *)deviceToken { + MPKitExecStatus *execStatus = [[MPKitExecStatus alloc] initWithSDKCode:@(MPKitInstanceAppboy) returnCode:MPKitReturnCodeSuccess]; + + if (!shouldDisableNotificationHandling) { + return execStatus; + } + #if TARGET_OS_IOS [appboyInstance.notifications registerDeviceToken:deviceToken]; #endif - MPKitExecStatus *execStatus = [[MPKitExecStatus alloc] initWithSDKCode:@(MPKitInstanceAppboy) returnCode:MPKitReturnCodeSuccess]; return execStatus; } @@ -931,6 +967,10 @@ - (MPKitExecStatus *)setUserIdentity:(NSString *)identityString identityType:(MP #if TARGET_OS_IOS - (nonnull MPKitExecStatus *)userNotificationCenter:(nonnull UNUserNotificationCenter *)center willPresentNotification:(nonnull UNNotification *)notification { MPKitExecStatus *execStatus = [[MPKitExecStatus alloc] initWithSDKCode:@(MPKitInstanceAppboy) returnCode:MPKitReturnCodeSuccess]; + + if (!shouldDisableNotificationHandling) { + return execStatus; + } if (![appboyInstance.notifications handleBackgroundNotificationWithUserInfo:notification.request.content.userInfo fetchCompletionHandler:^(UIBackgroundFetchResult fetchResult) {}]) { NSLog(@"mParticle -> Invalid Braze remote notification: %@", notification.request.content.userInfo); @@ -941,6 +981,10 @@ - (nonnull MPKitExecStatus *)userNotificationCenter:(nonnull UNUserNotificationC - (nonnull MPKitExecStatus *)userNotificationCenter:(nonnull UNUserNotificationCenter *)center didReceiveNotificationResponse:(nonnull UNNotificationResponse *)response API_AVAILABLE(ios(10.0)) { MPKitExecStatus *execStatus = [[MPKitExecStatus alloc] initWithSDKCode:@(MPKitInstanceAppboy) returnCode:MPKitReturnCodeSuccess]; + + if (!shouldDisableNotificationHandling) { + return execStatus; + } if (![appboyInstance.notifications handleUserNotificationWithResponse:response withCompletionHandler:^{}]) { NSLog(@"mParticle -> Notification Response rejected by Braze: %@", response); diff --git a/Sources/mParticle-Appboy/include/MPKitAppboy.h b/Sources/mParticle-Appboy/include/MPKitAppboy.h index 73e1243..f2ec884 100644 --- a/Sources/mParticle-Appboy/include/MPKitAppboy.h +++ b/Sources/mParticle-Appboy/include/MPKitAppboy.h @@ -18,7 +18,9 @@ #if TARGET_OS_IOS + (void)setInAppMessageControllerDelegate:(nonnull id)delegate; ++ (void)setShouldDisableNotificationHandling:(BOOL)isDisabled; #endif + (void)setURLDelegate:(nonnull id)delegate; ++ (void)setBrazeInstance:(nonnull id)instance; @end diff --git a/mParticle_AppboyTests/mParticle_AppboyTests.m b/mParticle_AppboyTests/mParticle_AppboyTests.m index aa37f2f..2a1a65b 100644 --- a/mParticle_AppboyTests/mParticle_AppboyTests.m +++ b/mParticle_AppboyTests/mParticle_AppboyTests.m @@ -16,6 +16,8 @@ - (void)setAppboyInstance:(Braze *)instance; - (NSMutableDictionary *)optionsDictionary; + (id)inAppMessageControllerDelegate; - (void)setEnableTypeDetection:(BOOL)enableTypeDetection; ++ (BOOL)shouldDisableNotificationHandling; ++ (Braze *)brazeInstance; @end @@ -28,6 +30,7 @@ @implementation mParticle_AppboyTests - (void)setUp { [super setUp]; // Put setup code here. This method is called before the invocation of each test method in the class. + [MPKitAppboy setBrazeInstance:nil]; } - (void)tearDown { @@ -275,6 +278,49 @@ - (void)testWeakMessageDelegate { [self waitForExpectationsWithTimeout:1 handler:nil]; } +- (void)testSetDisableNotificationHandling { + XCTAssertEqual([MPKitAppboy shouldDisableNotificationHandling], NO); + + [MPKitAppboy setShouldDisableNotificationHandling:YES]; + + XCTAssertEqual([MPKitAppboy shouldDisableNotificationHandling], YES); + + [MPKitAppboy setShouldDisableNotificationHandling:NO]; + + XCTAssertEqual([MPKitAppboy shouldDisableNotificationHandling], NO); +} + +- (void)testSetBrazeInstance { + BRZConfiguration *configuration = [[BRZConfiguration alloc] init]; + Braze *testClient = [[Braze alloc] initWithConfiguration:configuration]; + + XCTAssertEqualObjects([MPKitAppboy brazeInstance], nil); + + [MPKitAppboy setBrazeInstance:testClient]; + + MPKitAppboy *appBoy = [[MPKitAppboy alloc] init]; + + XCTAssertEqualObjects(appBoy.appboyInstance, nil); + XCTAssertEqualObjects(appBoy.providerKitInstance, nil); + XCTAssertEqualObjects([MPKitAppboy brazeInstance], testClient); + + NSDictionary *kitConfiguration = @{@"apiKey":@"BrazeID", + @"id":@42, + @"ABKCollectIDFA":@"true", + @"ABKRequestProcessingPolicyOptionKey": @"1", + @"ABKFlushIntervalOptionKey":@"2", + @"ABKSessionTimeoutKey":@"3", + @"ABKMinimumTriggerTimeIntervalKey":@"4", + @"userIdentificationType":@"CustomerId" + }; + + [appBoy didFinishLaunchingWithConfiguration:kitConfiguration]; + + XCTAssertEqualObjects(appBoy.appboyInstance, testClient); + XCTAssertEqualObjects(appBoy.providerKitInstance, testClient); + XCTAssertEqualObjects([MPKitAppboy brazeInstance], testClient); +} + - (void)testUserIdCustomerId { MPKitAppboy *appBoy = [[MPKitAppboy alloc] init];