Deep Linking Guide

Handle push notification deep links in the StreamLayer iOS SDK. Upload APNs device tokens, process incoming notifications, and route users to the correct StreamLayer Element view.

Deep Linking Guide

The StreamLayer SDK supports push notifications via the Apple Push Notification Service (APNs). StreamLayer uses push notifications to notify users about incoming chat messages and Watch Party invitations. When a user taps a notification, your app opens by default. For the best experience, you should deep link the user directly to the StreamLayer Element for easier navigation.

Deep Linking is also how Watch Party invitations work. For additional information about invitations, see the Invites Guide.

Deep Link Integration

The StreamLayer service needs your users' APNs device token to send push notifications. If a user does not consent to push notifications, StreamLayer cannot send notifications to their device.

Sample Integration

To upload the device token, use StreamLayer.uploadDeviceAPNsToken(deviceAPNsToken: token):

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){
  	 // 1. Convert device token to a string
	let tokenParts = deviceToken.map (data -> String in
		return String(format: "%02.2hhx", data)
  }
	let token = tokenParts.joined()
	// 2. Send device token to StreamLayer API
	StreamLayer.uploadDeviceAPNsToken(deviceAPNsToken: token)
}

Import the UserNotifications foundation:

import UIKit
import UserNotifications

Set the AppDelegate as the delegate for handling incoming notifications and request authorization:

func registerForPushNotifications(){
  UNUserNotificationCenter.current().delegate = self
  UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, _) in
                                                                                              // 1. Check if permission granted
                                                                                              guard granted else {return}
                                                                                              // 2. Attempt registration for invite notifications on the main thread
                                                                                              DispatchQueue.main.sync{
                                                                                                UIApplication.shared.registerForRemoteNotifications()
                                                                                              }
                                                                                             }
}

Handle the notification when it arrives (foreground):

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> void {
  StreamLayer.handlePushNotification (nil, userInfo: userInfo, background: false)
  completionHandler(.noData)
}

Handle the notification from background:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> void) {
  StreamLayer.handlePushNotification(center, userInfo: response.notification.request.content.userInfo, background: true)
  completionHandler()
}

Related