Deep Linking Guide

Deep Linking Guide

The StreamLayer SDK supports push notifications via the Apple Push Notification Service (APNs). StreamLayer uses push notifications to notify incoming chat messages and Watch Parties. When a user clicks on the notification it will open your app by default. For the best experience, however, you want to have your app bring up the StreamLayer overlay for easier navigation. This kind of user navigation is also known as Deep Linking. Please follow this guide to implement Deep Linking in your application. Deep Linking is also how we handle inviting new friends into your Watch Parties. For additional information about invitations, see the following guide.

Deep Link Integration

The StreamLayer service needs your users APNs device token in order to send Push Notifications to the device. If your user does not consent to Push Notifications, StreamLayer will not be able to send Push Notifications to their device.

Sample Integration.

To upload the device token to StreamLayer, use the following function StreamLayer.uploadDeviceAPNsToken(deviceAPNsToken: token) to register your host application. You make this call in your application call.

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)
}

For access to Notification Center you have to import the UserNotifications foundation.

import UIKit
import UserNotifications

Then you need to set the AppDelegate class as the delegate for handling the incoming notifications and make the authorization request for receiving it.

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()
                                                                                              }
                                                                                             }
}

Then handle the notification when it comes in.

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

And in case when the application is in the background mode

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