Notification Interactions

A user can interact with a notification by:

  • Tapping the notification

  • Clicking one of the buttons on the notification

The result of any of those interactions depends on the action you select on the Notification Message Templatearrow-up-right. There are three options:

  • Open App: opens the application.

  • Open Deep Link: opens the application. Then you can choose whether the deep link will be automatically opened or you want to manually handle it via the automaticallyOpenDeeplinks parameter.

  • Open URL: opens the application. Then opens the url. The actual activity that is launched depends on the url and the installed applications on the device. In most cases, urls with http/https scheme will be opened in the browser.

Handling all notification interactions

All notification interactions go through the UNUserNotificationCenterDelegate which you can set up as follows:

class AppDelegate: NSObject, UIApplicationDelegate {
    static var bird: Bird!
    static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "AppDelegate")
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        do {
            Self.bird = try Bird()
        } catch {
            Self.logger.info("failed to create bird \(error)")
        }
        
        // Set UNUserNotificationCenterDelegate
        UNUserNotificationCenter.current().delegate = self
        return true
    }
    
    // Other methods of UIApplicationDelegate
}

extension AppDelegate: UNUserNotificationCenterDelegate {

    func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        didReceive response: UNNotificationResponse,
        withCompletionHandler completionHandler: @escaping () -> Void
    ) {
        // All notification interactions are captured here
        if let result = Self.bird.notifications.handleNotificationResponse(
            response: response,
            // Set automaticallyOpenDeeplinks to true if you want deep links
            // to be automatically opened
            automaticallyOpenDeeplinks: false
        ) {
            // The result contains the url and the custom payload
            Self.logger.log("type \(result.type.rawValue)")
            Self.logger.log("payload \(result.payload))")
            Self.logger.log("action.type \(result.action.type.rawValue)")
            Self.logger.log("action.url \(result.action.url?.absoluteString ?? "")")
            Self.logger.log("action.identifier \(result.action.identifier)")
        }
        completionHandler()
    }
    
    // Other methods of UNUserNotificationCenterDelegate
}

Notice that you can control whether deep links are automatically opened or not via the parameter automaticallyOpenDeeplinks.

There are two way to deal with Deep Links:

Custom Payload

You can set a custom payload on the Notification Message Templatearrow-up-right to be delivered to your app. You can capture custom payload when your application received a notification response using the bird.notifications.handleNotificationResponse() method. See sample code above.

Last updated

Was this helpful?