Example SDK Flow

A complete Swift walkthrough integrating Pulsate into an app with a login flow, showing in-app notifications only to signed-in users.

This page ties the SDK together in a realistic app. The app has a login flow and enables in-app notifications only for signed-in users, while keeping Pulsate active in the background so pushes, geofences, and sessions keep working.

1. Initialize in the AppDelegate

Create the manager in application(_:didFinishLaunchingWithOptions:). The getInstance initializer throws, so wrap it in a do/catch.

import UIKit
import PULPulsate

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        do {
            let authData = try PULAuthorizationData(withAppId: "APP_ID", andAppKey: "APP_KEY")
            _ = try PULPulsateFactory.getInstance(
                withAuthorizationData: authData,
                withLaunchOptions: launchOptions
            )
        } catch {
            print("Pulsate init failed: \(error)")
        }
        return true
    }

    // When the app goes to the background, stop showing in-app notifications so a
    // signed-out user won't see them next time the app comes forward.
    func applicationDidEnterBackground(_ application: UIApplication) {
        PULPulsateFactory.getDefaultInstance()?.enable(inAppNotification: false)
    }
}

📘

Note

getDefaultInstance() returns an optional PULPulsateManager? and does not throw. Only the getInstance(...) initializers on PULPulsateFactory throw.

2. Gate in-app notifications on the login screen

On your login screen, enable in-app notifications for users who are already signed in and navigate them onward. Do not start the Pulsate session here (see the note in step 3).

import UIKit
import PULPulsate

class LoginViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let pulsateManager = PULPulsateFactory.getDefaultInstance()

        showSpinner()
        if loginViewModel.isUserLoggedIn() {
            pulsateManager?.enable(inAppNotification: true)
            navigateToMainScreen()
        } else {
            hideSpinner()
            setupLoginScreen()
        }
    }
}

3. Start the session on the main screen

Start the Pulsate session from an active view controller — the main screen — not the login screen or the AppDelegate. The session start is asynchronous and, on success, tries to attach an in-app message to the current screen. If you start it on a screen that is about to be destroyed (like a login screen after a successful sign-in), the in-app message may fail to render.

import UIKit
import PULPulsate

class MainViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let pulsateManager = PULPulsateFactory.getDefaultInstance()

        pulsateManager?.startPulsateSession(forAlias: alias) { success, error in
            pulsateManager?.showLastInAppNotification(true)
        }
    }
}

This starts a session for the identified user and shows any in-app notification that was waiting to render.

🚧

Important

Avoid calling enable(inAppNotification:) repeatedly with different values in quick succession. These are asynchronous database writes, and racing calls can leave the wrong value set or interrupt an in-app message mid-render. Enable it once when you know the user's state.

4. Summary

With this setup:

  • Pulsate stays active in the background — geofences, pushes, and session management keep working even when your app is not in the foreground.
  • In-app notifications are shown only to signed-in users, because you disable them on background and re-enable them once the user reaches the main screen.
  • To also stop push notifications for signed-out users, toggle setPushNotificationEnabled(false) on logout and setPushNotificationEnabled(true) on login.

Related pages