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 SDK Core 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.

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
  let token = tokenParts.joined()

  // Call the Enhanced In-App Chat SDK with token info
  CoreFactory.provide(deviceToken: token ?? "")
}

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.

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {

  // Handle the notification
  let userInfo = response.notification.request.content.userInfo
  if let aps = userInfo["aps"] as? [String: AnyObject] {

    // Extract the conversation ID (if one exists)
    if let conversationId = aps["conversationId"] as? String {
      guard let conversationID = UUID(uuidString: conversationId) else {
        completionHandler()
        return
      }

      // After you have the conversation ID, you can open the messaging interface
      // for that particular conversation...
      // (that is, `Interface` for SwiftUI or
      //       `InterfaceViewController` for Swift and Objective-C)
      // Be sure to use the interface with correct config values for this conversation ID
      // or a new conversation is created.
    }
  }
  completionHandler()
}

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.

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

   // Check whether the app is in the background or open.
   let backgroundState = UIApplication.shared.applicationState
   if backgroundState != .background {
        // Don't show the push notification
        completionHandler([])
   }

   // Return the comptionHandler with the UNNotificationPresentationOptions of your choice.
   completionHandler([.badge, .sound, .list, .banner])
}

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.

coreClient.deregister(completion: { error in

})

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.