From 7d6e97761f7a05c4cd3b406c0c52631d8f97e306 Mon Sep 17 00:00:00 2001 From: Ben Baron Date: Wed, 14 Feb 2024 17:07:31 -0500 Subject: [PATCH 1/3] feat: Add option to disable notification handling --- Sources/mParticle-Appboy/MPKitAppboy.m | 29 ++++++++++++++++++- .../mParticle-Appboy/include/MPKitAppboy.h | 1 + mParticle_AppboyTests/mParticle_AppboyTests.m | 13 +++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Sources/mParticle-Appboy/MPKitAppboy.m b/Sources/mParticle-Appboy/MPKitAppboy.m index 4376a94..0297d72 100644 --- a/Sources/mParticle-Appboy/MPKitAppboy.m +++ b/Sources/mParticle-Appboy/MPKitAppboy.m @@ -49,6 +49,7 @@ #if TARGET_OS_IOS __weak static id inAppMessageControllerDelegate = nil; +static BOOL shouldDisableNotificationHandling = NO; #endif __weak static id urlDelegate = nil; @@ -83,6 +84,15 @@ + (void)setInAppMessageControllerDelegate:(id)delegate { + (id)inAppMessageControllerDelegate { return inAppMessageControllerDelegate; } + ++ (void)setShouldDisableNotificationHandling:(BOOL)isDisabled { + shouldDisableNotificationHandling = isDisabled; +} + ++ (BOOL)shouldDisableNotificationHandling { + return shouldDisableNotificationHandling; +} + #endif + (void)setURLDelegate:(id)delegate { @@ -557,6 +567,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 +589,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 +950,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 +964,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..cfbd5dd 100644 --- a/Sources/mParticle-Appboy/include/MPKitAppboy.h +++ b/Sources/mParticle-Appboy/include/MPKitAppboy.h @@ -18,6 +18,7 @@ #if TARGET_OS_IOS + (void)setInAppMessageControllerDelegate:(nonnull id)delegate; ++ (void)setShouldDisableNotificationHandling:(BOOL)isDisabled; #endif + (void)setURLDelegate:(nonnull id)delegate; diff --git a/mParticle_AppboyTests/mParticle_AppboyTests.m b/mParticle_AppboyTests/mParticle_AppboyTests.m index aa37f2f..76ecf0f 100644 --- a/mParticle_AppboyTests/mParticle_AppboyTests.m +++ b/mParticle_AppboyTests/mParticle_AppboyTests.m @@ -16,6 +16,7 @@ - (void)setAppboyInstance:(Braze *)instance; - (NSMutableDictionary *)optionsDictionary; + (id)inAppMessageControllerDelegate; - (void)setEnableTypeDetection:(BOOL)enableTypeDetection; ++ (BOOL)shouldDisableNotificationHandling; @end @@ -275,6 +276,18 @@ - (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)testUserIdCustomerId { MPKitAppboy *appBoy = [[MPKitAppboy alloc] init]; From 1c3b8280d6de6a1a9dfc4064b4b765c7f6ccebc3 Mon Sep 17 00:00:00 2001 From: Ben Baron Date: Thu, 15 Feb 2024 11:59:39 -0500 Subject: [PATCH 2/3] feat: Allow setting braze instance --- Sources/mParticle-Appboy/MPKitAppboy.m | 15 +++++++++ .../mParticle-Appboy/include/MPKitAppboy.h | 2 ++ mParticle_AppboyTests/mParticle_AppboyTests.m | 33 +++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/Sources/mParticle-Appboy/MPKitAppboy.m b/Sources/mParticle-Appboy/MPKitAppboy.m index 0297d72..1fd8674 100644 --- a/Sources/mParticle-Appboy/MPKitAppboy.m +++ b/Sources/mParticle-Appboy/MPKitAppboy.m @@ -52,6 +52,7 @@ static BOOL shouldDisableNotificationHandling = NO; #endif __weak static id urlDelegate = nil; +static Braze *brazeInstance = nil; @interface MPKitAppboy() { Braze *appboyInstance; @@ -103,6 +104,14 @@ + (void)setURLDelegate:(id)delegate { return urlDelegate; } ++ (void)setBrazeInstance:(Braze *)instance { + brazeInstance = instance; +} + ++ (Braze *)brazeInstance { + return brazeInstance; +} + #pragma mark Private methods - (NSString *)stringRepresentation:(id)value { NSString *stringRepresentation = nil; @@ -249,6 +258,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]) { @@ -276,6 +289,8 @@ - (MPKitExecStatus *)didFinishLaunchingWithConfiguration:(NSDictionary *)configu _started = NO; } + + execStatus = [[MPKitExecStatus alloc] initWithSDKCode:[[self class] kitCode] returnCode:MPKitReturnCodeSuccess]; return execStatus; } diff --git a/Sources/mParticle-Appboy/include/MPKitAppboy.h b/Sources/mParticle-Appboy/include/MPKitAppboy.h index cfbd5dd..3ed23da 100644 --- a/Sources/mParticle-Appboy/include/MPKitAppboy.h +++ b/Sources/mParticle-Appboy/include/MPKitAppboy.h @@ -10,6 +10,7 @@ #import "mParticle_Apple_SDK-Swift.h" #endif +@class Braze; @interface MPKitAppboy : NSObject @property (nonatomic, strong, nonnull) NSDictionary *configuration; @@ -21,5 +22,6 @@ + (void)setShouldDisableNotificationHandling:(BOOL)isDisabled; #endif + (void)setURLDelegate:(nonnull id)delegate; ++ (void)setBrazeInstance:(nonnull Braze *)instance; @end diff --git a/mParticle_AppboyTests/mParticle_AppboyTests.m b/mParticle_AppboyTests/mParticle_AppboyTests.m index 76ecf0f..2a1a65b 100644 --- a/mParticle_AppboyTests/mParticle_AppboyTests.m +++ b/mParticle_AppboyTests/mParticle_AppboyTests.m @@ -17,6 +17,7 @@ - (void)setAppboyInstance:(Braze *)instance; + (id)inAppMessageControllerDelegate; - (void)setEnableTypeDetection:(BOOL)enableTypeDetection; + (BOOL)shouldDisableNotificationHandling; ++ (Braze *)brazeInstance; @end @@ -29,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 { @@ -288,6 +290,37 @@ - (void)testSetDisableNotificationHandling { 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]; From 285ba5663a19bdc624af3539ecbdaea1f7918b3a Mon Sep 17 00:00:00 2001 From: Ben Baron Date: Thu, 15 Feb 2024 12:13:57 -0500 Subject: [PATCH 3/3] Remove forward declaration for Swift interop --- Sources/mParticle-Appboy/MPKitAppboy.m | 6 ++++-- Sources/mParticle-Appboy/include/MPKitAppboy.h | 3 +-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/mParticle-Appboy/MPKitAppboy.m b/Sources/mParticle-Appboy/MPKitAppboy.m index 1fd8674..e32c75d 100644 --- a/Sources/mParticle-Appboy/MPKitAppboy.m +++ b/Sources/mParticle-Appboy/MPKitAppboy.m @@ -104,8 +104,10 @@ + (void)setURLDelegate:(id)delegate { return urlDelegate; } -+ (void)setBrazeInstance:(Braze *)instance { - brazeInstance = instance; ++ (void)setBrazeInstance:(id)instance { + if ([instance isKindOfClass:[Braze class]]) { + brazeInstance = instance; + } } + (Braze *)brazeInstance { diff --git a/Sources/mParticle-Appboy/include/MPKitAppboy.h b/Sources/mParticle-Appboy/include/MPKitAppboy.h index 3ed23da..f2ec884 100644 --- a/Sources/mParticle-Appboy/include/MPKitAppboy.h +++ b/Sources/mParticle-Appboy/include/MPKitAppboy.h @@ -10,7 +10,6 @@ #import "mParticle_Apple_SDK-Swift.h" #endif -@class Braze; @interface MPKitAppboy : NSObject @property (nonatomic, strong, nonnull) NSDictionary *configuration; @@ -22,6 +21,6 @@ + (void)setShouldDisableNotificationHandling:(BOOL)isDisabled; #endif + (void)setURLDelegate:(nonnull id)delegate; -+ (void)setBrazeInstance:(nonnull Braze *)instance; ++ (void)setBrazeInstance:(nonnull id)instance; @end