---
schema_version: "1"
id: dadcd4ff126c2b1f1f3d2f90fe21b929d48478fd8f2b8df2870bd6179ab446ee
title: Set Up Messaging Push Notifications for iOS
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/service-cloud/messaging-in-app/guides/ios-notifications.md
canonical_url: https://developer.salesforce.com/docs/service/messaging-in-app/guide/ios-notifications.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 63764007cf54785b7d4376296dc1c0941387cc3b2605918abea1c80a281d5214
products:
  - Enhanced In-App Chat
  - Service Cloud Developer Content
---
# 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](https://developer.apple.com/notifications/) as described in the Apple documentation. Then follow Salesforce org setup instructions at [Set Push Notifications for Enhanced In-App Chat](https://help.salesforce.com/s/articleView?id=service.miaw_inapp_push.htm) 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`](https://salesforce-async-messaging.github.io/messaging-in-app-ios/documentation/smiclientcore/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](https://developer.apple.com/documentation/usernotifications/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](#pass-the-token-to-the-sdk) section for information.
