With push notifications, your customers can be alerted about messaging activity even when they aren’t viewing the conversation. When a rep sends a message, your customer receives a notification on their device.
This article applies to the following implementations:
When the app successfully registers with the Apple Push Notification service (APNs), pass device token information to the Enhanced In-App Chat server using CoreFactory.provide(deviceToken). See the CoreFactory reference documentation.
Pass notification token to SDK
1func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){2 let tokenParts = deviceToken.map{ data in String(format: "%02.2hhx", data)}3 let token = tokenParts.joined()45 // Call the Enhanced In-App Chat SDK with token info6 CoreFactory.provide(deviceToken: token ?? "")7}
Handle a Tap (Optional)
By default, when a user taps on the notification, your app launches to the last view that was showing. If you want to take them to the updated conversation, extract the conversationId property, and then use that value to open the correct messaging conversation.
Handle a tap
1func userNotificationCenter(_ center: UNUserNotificationCenter,2 didReceive response: UNNotificationResponse,3 withCompletionHandler completionHandler: @escaping() ->Void){45 // Handle the notification6 let userInfo = response.notification.request.content.userInfo7 if let aps = userInfo["aps"] as? [String: AnyObject]{89 // Extract the conversation ID (if one exists)10 if let conversationId = aps["conversationId"] as? String{11 guard let conversationID = UUID(uuidString: conversationId)else{12 completionHandler()13 return14}1516 // After you have the conversation ID, you can open the messaging interface17 // for that particular conversation...18 // (that is, `Interface` for SwiftUI or19 // `InterfaceViewController` for Swift and Objective-C)20 // Be sure to use the interface with correct config values for this conversation ID21 // or a new conversation is created.22}23}24 completionHandler()25}
Hide Push Notifications When the App is Open (Optional)
You can get push notifications when the app is open and in the foreground by default. To suppress notifications when the app is open, add the userNotificationCenter function in the AppDelegate.m file and include this code.
Suppress notifications when app is open
1func userNotificationCenter(_ center: UNUserNotificationCenter,2 willPresent notification: UNNotification,3 withCompletionHandler completionHandler: @escaping(UNNotificationPresentationOptions) ->Void){45 // Check whether the app is in the background or open.6 let backgroundState = UIApplication.shared.applicationState7 if backgroundState != .background {8 // Don't show the push notification9 completionHandler([])10}1112 // Return the comptionHandler with the UNNotificationPresentationOptions of your choice.13 completionHandler([.badge, .sound, .list, .banner])14}
To deregister push notifications, call one of these functions on CoreClient: deregisterDeviceWithCompletion, revokeTokenAndDeregisterDevice, or revokeTokenWithCompletion.
Deregister push notifications
1coreClient.deregister(completion: { error in23})
To register a device after you have degregistered a device, pass the device token to the SDK. See the Pass the Token to the SDK section for information.