Skip to content

Delayed initialization for the Braze Swift SDK

Learn how to initialize your Braze Swift SDK asynchronously while ensuring push notification handling is preserved. This can be useful when you need to set up other services before initializing the SDK, such as fetching configuration data from a server, or waiting for user consent.

Setting up delayed initialization

Step 1: Preparing the SDK for delayed initialization

By default, if an end-user opens your push notification while your app is in a terminated state, the push notification cannot be processed before the SDK is initialized.

Starting with Braze Swift SDK version 10.1.0 and later, you can handle this using the static helper method: Braze.prepareForDelayedInitialization(pushAutomation:). This method will prepare the SDK for delayed initialization by setting up the push automation system.

Before the SDK is initialized, all push notifications originating from Braze will be captured and queued. After the SDK is initialized, those push notifications will be processed by the SDK. This method must be called as early as possible in your application lifecycle, either in or before the application(_:didFinishLaunchingWithOptions:) method of your AppDelegate.

1
2
3
4
5
6
7
8
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  // Prepare the SDK for delayed initialization
  Braze.prepareForDelayedInitialization()

  // ... Additional non-Braze setup code

  return true
}

SwiftUI applications require implementing the @UIApplicationDelegateAdaptor property wrapper to call the prepareForDelayedInitialization() method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@main
struct MyApp: App {
  @UIApplicationDelegateAdaptor var appDelegate: AppDelegate

  var body: some Scene {
    WindowGroup {
      ContentView()
    }
  }
}

class AppDelegate: NSObject, UIApplicationDelegate {
  
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    // Prepare the SDK for delayed initialization
    Braze.prepareForDelayedInitialization()

    // ... Additional non-Braze setup code

    return true
  }
  
}
1
2
3
4
5
6
7
8
9
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Prepare the SDK for delayed initialization
  [Braze prepareForDelayedInitialization];
  
  // ... Additional non-Braze setup code

  return YES;
}

Step 2: Initializing the Braze SDK

After you’ve prepared the SDK for delayed initialization, you can initialize the SDK asynchronously at any time in the future. Then the SDK will process all queued push notifications events originating from Braze.

To initialize the Braze SDK, follow the standard Swift SDK initialization process.

Considerations

By using Braze.prepareForDelayedInitialization(pushAutomation:), you are configuring the SDK to automatically use push notification automation features. Any system delegate methods that handle push notifications will not be called for push notifications originating from Braze.

The SDK will only process a Braze push notification and the resulting action after the SDK is initialized. For example, if a user taps a push notification that opens a deep link, the deep link will only open after the Braze instance is initialized.

If you need to perform additional processing on Braze push notifications, see Subscribing to push notifications updates. Keep in mind, to receive updates for push notifications that were previously enqueued, you must implement the subscription handler directly after initializing the SDK.

HOW HELPFUL WAS THIS PAGE?
New Stuff!