Skip to content

Instantly share code, notes, and snippets.

@kezzico
Last active May 15, 2023 17:28
Show Gist options
  • Save kezzico/355a3ebf880c1c5e58302acadd2ef916 to your computer and use it in GitHub Desktop.
Save kezzico/355a3ebf880c1c5e58302acadd2ef916 to your computer and use it in GitHub Desktop.
Firebase Dynamic Links + AppBoy
#import "Appboy-iOS-SDK/AppboyKit.h"
#import "ReactNativeConfig.h"
#import "SEGAppboyIntegrationFactory.h"
#import <RNFBDynamicLinks/RNFBDynamicLinksAppDelegateInterceptor.h>
static DeeplinkPusher *_shared = nil;
@implementation DeeplinkPusher
+ (DeeplinkPusher *) shared {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_shared = [[DeeplinkPusher alloc] init];
});
return _shared;
}
- (BOOL)handleAppboyURL:(NSURL *) url fromChannel:(ABKChannel)channel withExtras:(NSDictionary *) extras {
NSLog(@"[deeplinks] Handle URL in push notification: %@", url);
// Trigger FB Dynamic Links ⛅️
NSUserActivity *activity = [[NSUserActivity alloc] initWithActivityType:@"com.goodeggs.deeplinks"];
activity.webpageURL = url;
[RNFBDynamicLinksAppDelegateInterceptor.sharedInstance
application:UIApplication.sharedApplication
continueUserActivity:activity
restorationHandler:^(id a){ }];
return YES;
}
- (void) prepare {
NSString *brazeKey = [ReactNativeConfig envFor:@"IOS_BRAZE_API_KEY"];
if (brazeKey == nil) {
NSLog(@"[deeplinks] Braze key missing from '.env'. Unable to recieve Push notifications");
return;
}
[Appboy startWithApiKey:brazeKey
inApplication:UIApplication.sharedApplication
withLaunchOptions:NSDictionary.dictionary];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
UNAuthorizationOptions options =
UNAuthorizationOptionAlert | UNAuthorizationOptionSound |
UNAuthorizationOptionBadge | UNAuthorizationOptionProvisional;
[center requestAuthorizationWithOptions:options
completionHandler:^(BOOL granted, NSError * _Nullable error) {
[[Appboy sharedInstance] pushAuthorizationFromUserNotificationCenter:granted];
}];
[[UIApplication sharedApplication] registerForRemoteNotifications];
Appboy.sharedInstance.appboyUrlDelegate = self;
NSString *segmentKey = [ReactNativeConfig envFor:@"SEGMENT_WRITE_KEY"];
if (segmentKey != nil) {
SEGAnalyticsConfiguration *config = [SEGAnalyticsConfiguration configurationWithWriteKey:segmentKey];
[config use:[SEGAppboyIntegrationFactory instance]];
[SEGAnalytics setupWithConfiguration:config];
[[SEGAppboyIntegrationFactory instance] saveLaunchOptions:NSDictionary.dictionary];
}
}
- (void) registerForRemoteNotifications:(NSData *) deviceToken {
[[Appboy sharedInstance] registerDeviceToken:deviceToken];
}
- (void) didReceiveRemoteNotification: userInfo completion:(void (^)(UIBackgroundFetchResult)) completionHandler {
[Appboy.sharedInstance registerApplication:UIApplication.sharedApplication
didReceiveRemoteNotification:userInfo
fetchCompletionHandler:nil];
[[SEGAnalytics sharedAnalytics] receivedRemoteNotification:userInfo];
if (Appboy.sharedInstance == nil) {
[SEGAppboyIntegrationFactory.instance saveRemoteNotification:userInfo];
}
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
if (Appboy.sharedInstance == nil) {
[SEGAppboyIntegrationFactory.instance.appboyHelper
saveUserNotificationCenter:center
notificationResponse:response];
}
[SEGAppboyIntegrationFactory.instance.appboyHelper
userNotificationCenter:center
receivedNotificationResponse:response];
completionHandler();
}
//handle push notification display while app is in foreground
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
if (@available(iOS 14.0, *)) {
completionHandler(UNNotificationPresentationOptionList | UNNotificationPresentationOptionBanner);
} else {
completionHandler(UNNotificationPresentationOptionAlert);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment