Push Notifications Setup Guide: iOS

Chatkit relies on Apple Push Notification service (APNs) to deliver push notifications to iOS application users on your behalf. When we deliver push notifications, we use your APNs Key. This page guides you through the process of getting an APNs Key and how to provide it to Chatkit.


Open Apple Developer Account

Login into your Apple Developer Account.

Create a New Key

Navigate to the keys section in your Apple Developer Center, where you'll create an APNs Key. Enter the key name and make sure that APNs checkbox is checked. Press the continue button.

In order to generate the key, you'll either need to be an Account Holder or Admin, App Manager with Certificates, Identifiers & Profiles access enabled. You can learn more about the roles, here.

Confirm your key configuration

If information entered in previous step is correct procced by pressing the confirm button.

Download the key

Press download button to download the key. Finalize the process and press the done button.

Now go to the Chatkit instance in the dashboard, and use this APNs Key to configure your iOS integration.

Enable Capabilities

In the project navigator, select your project, and click on the Capabilities tab. Enable Push Notifications by turning the switch ON.

Register with APNs

For your iOS app to receive push notifications, it must first register the device with APNs. You should do this when the application finishes launching, i.e. in its application:didFinishLaunchingWithOptions: handler:

import UIKit
 
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
 
    var window: UIWindow?
 
    let pusherChat = ChatManager(
      instanceLocator: "your-instance-locator",
      tokenProvider: yourTokenProvider,
      userID: "your-user-id"
    )
 
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        ChatManager.registerForRemoteNotifications()
 
        return true
    }
}

Register with Chatkit

APNs will respond with a device token identifying your app instance. This device token is passed to your application with the application:didRegisterForRemoteNotificationsWithDeviceToken: method. Chatkit requires the deviceToken in order to send push notifications to the app instance. Your app should register with Push Notifications, passing along its device token. Add a handler for it:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
   ChatManager.registerDeviceToken(deviceToken)
}

Enable Push Notifications

Final step is to enable push notifications. This can be done once the connection with the Chatkit service was successfully established on the instance of the PCCurrentUser.

chatManager.connect(delegate: yourDelegate) { currentUser, error in
  guard error == nil else {
      print("Error connecting: \(error.localizedDescription)")
      return
  }
 
  currentUser.enablePushNotifications()
}

Disabling push notifications

You must explicitly disable push notifications for the current user when they sign out. In addition, you should also disable push notifications when the app launches and you're not in a logged in state. If you don't, people will receive notifications after they have signed out which will be unexpected. This will also prevent them from receiving push notifications if they sign in as a different user in future!

In your block of code where you sign your user out, call the following:

1
ChatManager.disablePushNotifications()