Skip to content

rudderlabs/rudder-integration-braze-swift

Repository files navigation

The Customer Data Platform for Developers

Website · Documentation · Community Slack


Integrating RudderStack iOS SDK with Braze

This repository contains the resources and assets required to integrate the RudderStack iOS SDK with Braze.

For more information on configuring Braze as a destination in RudderStack and the supported events and their mappings, refer to the Braze documentation.

Important: This device mode integration is supported for Braze v4.7.0 and above.

Step 1: Integrate the SDK with Braze

  1. Add Braze as a destination in the RudderStack dashboard.
  2. RudderBraze is available through CocoaPods and Swift Package Manager (SPM).

CocoaPods

Add the following line to your Podfile and followed by pod install:

pod 'RudderBraze', '~> 1.1.0'

Swift Package Manager (SPM)

You can also add the RudderStack iOS SDK via Swift Package Mangaer.

  • Go to File -> Add Package, as shown:

add_package

  • Enter the package repository (https://github.com/rudderlabs/rudder-integration-braze-swift) in the search bar.

  • In Dependency Rule, select Exact Version and enter latest as the value, as shown:

add_package

  • Select the project to which you want to add the package.

  • Finally, click on Add Package.

Step 2: Import the SDK

Swift

import RudderBraze

Objective C

@import RudderBraze;

Step 3: Initialize the RudderStack client (RSClient)

Place the following code in your AppDelegate file under the didFinishLaunchingWithOptions method:

Objective C

RSConfig *config = [[RSConfig alloc] initWithWriteKey:WRITE_KEY];
[config dataPlaneURL:DATA_PLANE_URL];
[[RSClient sharedInstance] configureWith:config];
[[RSClient sharedInstance] addDestination:[[RudderBrazeDestination alloc] init]];

Swift

let config: RSConfig = RSConfig(writeKey: WRITE_KEY)
            .dataPlaneURL(DATA_PLANE_URL)
RSClient.sharedInstance().configure(with: config)
RSClient.sharedInstance().addDestination(RudderBrazeDestination())

Step 4: Send events

Follow the steps listed in the RudderStack iOS SDK repo to start sending events to Braze.

Sending push notification events

To send your push notification events to Braze, follow these steps:

  1. Place the following code in your AppDelegate file under the didFinishLaunchingWithOptions method:

Objective C

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_x_Max) {
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  center.delegate = self;
  UNAuthorizationOptions options = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
  if (@available(iOS 12.0, *)) {
  options = options | UNAuthorizationOptionProvisional;
  }
  [center requestAuthorizationWithOptions:options
                        completionHandler:^(BOOL granted, NSError * _Nullable error) {
      [[RSClient sharedInstance] pushAuthorizationFromUserNotificationCenter:granted];
  }];
  [[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
         UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound)
                              completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (granted) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[UIApplication sharedApplication] registerForRemoteNotifications];
                });
            } else {
                // Handle error or denial
            }
        }];
}

Swift

if #available(iOS 10, *) {
    let center = UNUserNotificationCenter.current()
    center.delegate = self
    var options: UNAuthorizationOptions = [.alert, .sound, .badge]
    if #available(iOS 12.0, *) {
        options = UNAuthorizationOptions(rawValue: options.rawValue | UNAuthorizationOptions.provisional.rawValue)
        }
        center.requestAuthorization(options: options) { (granted, error) in
            RSClient.sharedInstance().pushAuthorizationFromUserNotificationCenter(granted)
    }
    UIApplication.shared.registerForRemoteNotifications()
} else {
    let types: UIUserNotificationType = [.alert, .badge, .sound]
    let setting: UIUserNotificationSettings = UIUserNotificationSettings(types: types, categories: nil)
    UIApplication.shared.registerUserNotificationSettings(setting)
    UIApplication.shared.registerForRemoteNotifications()
}
  1. Provide either a .p8 file (recommended) or a .p12 certificate push notification file or certificate from Apple. Refer to the Braze documentation for more details. Implement the following push notification methods:

Objective C

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [[RSClient sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    [[RSClient sharedInstance] application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {
    [[RSClient sharedInstance] userNotificationCenter:center didReceive:response withCompletionHandler:completionHandler];
}

Swift

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    RSClient.sharedInstance().application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    RSClient.sharedInstance().application(application, didReceiveRemoteNotification: userInfo, fetchCompletionHandler: completionHandler)
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    RSClient.sharedInstance().userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
}

About RudderStack

RudderStack is the customer data platform for developers. With RudderStack, you can build and deploy efficient pipelines that collect customer data from every app, website, and SaaS platform, then activate your data in your warehouse, business, and marketing tools.

Start building a better, warehouse-first CDP that delivers complete, unified data to every part of your customer data stack. Sign up for RudderStack Cloud today.

Contact us

For queries on configuring or using this integration, start a conversation in our Slack community.