Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow setting Braze instance and override notification handling #87

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion Sources/mParticle-Appboy/MPKitAppboy.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@

#if TARGET_OS_IOS
__weak static id<BrazeInAppMessageUIDelegate> inAppMessageControllerDelegate = nil;
static BOOL shouldDisableNotificationHandling = NO;
#endif
__weak static id<BrazeDelegate> urlDelegate = nil;
static Braze *brazeInstance = nil;

@interface MPKitAppboy() {
Braze *appboyInstance;
Expand Down Expand Up @@ -83,6 +85,15 @@ + (void)setInAppMessageControllerDelegate:(id)delegate {
+ (id<BrazeInAppMessageUIDelegate>)inAppMessageControllerDelegate {
return inAppMessageControllerDelegate;
}

+ (void)setShouldDisableNotificationHandling:(BOOL)isDisabled {
shouldDisableNotificationHandling = isDisabled;
}

+ (BOOL)shouldDisableNotificationHandling {
return shouldDisableNotificationHandling;
}

#endif

+ (void)setURLDelegate:(id)delegate {
Expand All @@ -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;
Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -266,6 +291,8 @@ - (MPKitExecStatus *)didFinishLaunchingWithConfiguration:(NSDictionary *)configu
_started = NO;
}



execStatus = [[MPKitExecStatus alloc] initWithSDKCode:[[self class] kitCode] returnCode:MPKitReturnCodeSuccess];
return execStatus;
}
Expand Down Expand Up @@ -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) {}]) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions Sources/mParticle-Appboy/include/MPKitAppboy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
46 changes: 46 additions & 0 deletions mParticle_AppboyTests/mParticle_AppboyTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ - (void)setAppboyInstance:(Braze *)instance;
- (NSMutableDictionary<NSString *, NSNumber *> *)optionsDictionary;
+ (id<BrazeInAppMessageUIDelegate>)inAppMessageControllerDelegate;
- (void)setEnableTypeDetection:(BOOL)enableTypeDetection;
+ (BOOL)shouldDisableNotificationHandling;
+ (Braze *)brazeInstance;

@end

Expand All @@ -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 {
Expand Down Expand Up @@ -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];

Expand Down