Set Up Messaging Push Notifications for iOS

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:

UI SDKCore SDK

Before you add code in your app for notifications, set up Apple Push Notifications as described in the Apple documentation. Then follow Salesforce org setup instructions at Set Push Notifications for Enhanced In-App Chat in Salesforce Help.

Pass the Token to the SDK 

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()
4
5  // Call the Enhanced In-App Chat SDK with token info
6  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) {
4
5  // Handle the notification
6  let userInfo = response.notification.request.content.userInfo
7  if let aps = userInfo["aps"] as? [String: AnyObject] {
8
9    // 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        return
14      }
15
16      // After you have the conversation ID, you can open the messaging interface
17      // for that particular conversation...
18      // (that is, `Interface` for SwiftUI or
19      //       `InterfaceViewController` for Swift and Objective-C)
20      // Be sure to use the interface with correct config values for this conversation ID
21      // 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) {
4
5   // Check whether the app is in the background or open.
6   let backgroundState = UIApplication.shared.applicationState
7   if backgroundState != .background {
8        // Don't show the push notification
9        completionHandler([])
10   }
11
12   // Return the comptionHandler with the UNNotificationPresentationOptions of your choice.
13   completionHandler([.badge, .sound, .list, .banner])
14}

See also Apple’s UNNotificationPresentationOptions object reference documentation.

Deregister Push Notifications (Optional) 

To deregister push notifications, call one of these functions on CoreClient: deregisterDeviceWithCompletion, revokeTokenAndDeregisterDevice, or revokeTokenWithCompletion.

Deregister push notifications
1coreClient.deregister(completion: { error in
2
3})

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.