Register push notification credentials
To notify users of events that take place on a client app, you should register notification credentials first. The following steps explain how to download your push notification credentials for APNs and register them to the Sendbird server via your dashboard.
Step 1: Create your certificate signing request
To set up push notifications on a client app, have an app ID assigned to your client app. To obtain a signing certificate required to sign apps for installation on iOS devices, you should first create a certificate signing request (CSR) file through Keychain Access on your Mac.
- Open Keychain Access from the Utilities folder, go to Keychain Access > Certificate Assistant, and then click Request a Certificate From a Certificate Authority.
- When the Certificate Information dialog box appears, enter the email address that you use in your Apple Developer account, and enter a common name for your private key. Don't enter CA email address, choose Saved to disk, and then click the Continue button.
- Specify the name of your CSR to save and choose the location to save the file on your local disk. Then your CSR file is created, which contains a public/private key pair.
Step 2: Create your push notification SSL certificate
- Sign in to your account at the Apple Developer Member Center and choose Certificates, Identifiers & Profiles. In the Identifiers > App IDs, select the Push Notifications service, and then click the Configure button.
- When prompted by a pop-up window, choose which SSL certificate to create either
Development
orProduction
.
- In the Certificates, Identifiers & Profiles pane that appears after the selection, under Upload a Certificate Signing Request, upload the CSR file you created through the Choose File button. Then to complete the process, click the Continue button above. When the certificate is ready, choose Download to save it to your Mac.
- In order to install the downloaded certificate to the Keychain Access on your Mac, double-click it. You can find the certificate in the Keychain Access > login > Certificates.
Step 3-1: Export a .p12 file and upload to Sendbird Dashboard
The Sendbird server needs to use some of your confidential information in your .p12
file to communicate with and send notification requests to APNs on behalf of your server. A .p12
file is a set of certificates that you generated when building a client app for iOS.
Note: Now you can use a
.p8
file to communicate with APNs and register it in the Sendbird Dashboard. See Step 3-2: Export a .p8 file and upload to Sendbird Dashboard for more information.
You can register your .p12
file to the Sendbird server through your dashboard or by using the Chat API's add an APNs push configuration action. The following steps show how to register a .p12
file in the Dashboard.
- Open Keychain Access and go to login > Certificates. Find the push notification SSL certificate you just added, right-click the certificate, and then select Export "YOUR_PROJECT_NAME" from the context menu to get a
.p12
file that contains the certificate.
- Type a name for the
.p12
file and save it to your Mac. Do not provide an export password when prompted. Then sign in to your dashboard and upload the file under Settings > Chat > Push notifications.
- When a dialog opens up, select
.p12
for Format. Then click the Choose file and select your.p12
certificate to upload. Once the file is uploaded, a password box appears. If needed, you can also set a password for the file.
Step 3-2: Export a .p8 file and upload to Sendbird Dashboard
The Sendbird server needs to use some of your confidential information in your .p8
file to send notification requests to APNs on behalf of your server. A .p8
file is an authentication token that is generated when you build a client app for iOS.
You can register your .p8
file to the Sendbird server through the Sendbird Dashboard or by using the platform API’s add an APNs push configuration action. The following steps show how to register a .p8
file in the dashboard.
Note: To learn more about the authentication token with a
.p8
extension, visit Apple Developer's Documentation on a token-based connection to APNs.
- Log into your Apple Developer account and go to Certificates, Identifiers & Profiles > Keys. Then click Create a key.
- Once an authentication key is created, register the key with a key name of your choice and select a service you want to use. In this case, check Apple Push Notifications service (APNs) in the table. Then click Continue.
- Make sure the key name and service chosen are correct. Then click Register.
- Once the key is registered, you can download the key as a file with a
.p8
extension. Download the file so that you can add it to the Sendbird Dashboard.
Note: The file can be only downloaded once. Make sure to remember the directory where your file is downloaded.
- Sign in to your Sendbird Dashboard and upload the file under Settings > Chat > Push notifications.
- When a dialog opens up, select
.p8
for Format. Then click Choose file and select your.p8
key file to upload. Once the file is uploaded, select SSL certificate type between Production and Developer.
Step 3-3: Enable the push notifications capability in Xcode
To use remote push notifications in your app's xcode project, you need to enable the push notifications capability.
For more information, follow the steps in the Enable the Push Notifications Capability section of Apple's Registering Your App with APNs article.
Step 4: Register a device token to the Sendbird server
When requested to send notifications to a specific client app on an iOS device, APNs requires the app instance's globally-unique and app-specific device token which has been issued by itself. Therefore, to send notification requests to APNs on behalf of your server, the Sendbird server needs every device token of your client app instances. If an app instance is successfully registered to APNs via the registerForRemoteNotifications
method, a device token is passed to an app delegate object's application:didRegisterForRemoteNotificationsWithDeviceToken:
method which is called by the app. Like the following sample code, you should register this token to the Sendbird server by passing it as an argument to a parameter in the registerDevicePushToken(_:unique:completionHandler:)
method.
Note: A user can have up to 20 APNs device tokens. If a user already has 20 device tokens, the oldest token will be deleted before a new token is added. Only the 20 newest tokens will be maintained for users who already have more than 20.
Note: If
pending
is returned throughcompletionHandler
, this means that your user is not being connected to the Sendbird server whenregisterDevicePushToken(_:unique:completionHandler:)
is called. In this case, we recommend that you first get a pending device token by using thegetPendingPushToken()
method, and then register the token by invokingregisterDevicePushToken(_:unique:completionHandler:)
in a callback ofconnect(userId:completionHandler:)
orconnect(userId:authToken:completionHandler:)
when your user has been connected to the server afterward.
Make sure you call the following code in the appropriate place to receive permissions from your users.
Step 5: Handle a notification payload
The Sendbird server sends notification requests to APNs with the following options.
Option | Description |
---|---|
alert |
|
sound | The default alert sound of iOS |
badge | The total number of each user's unread messages. You can turn off this badge count from your dashboard. |
The server also sends an additional JSON
object with the sendbird
key. For notification requests, APNs then delivers notifications to iOS devices. At the client side, you can parse the data payload and handle the result to implement user reactions within the application:didReceiveRemoteNotification:fetchCompletionHandler:
method, which covers all notifications from the application states of inactive
, foreground
, and background
.
The following is a complete payload format of the sendbird
object, which contains a set of provided key-value items. As for channel_unread_count
, the attribute can be added into or removed from the payload in Settings > Chat > Push notifications on the Sendbird Dashboard.
Step 6: Enable multi-device support on Sendbird Dashboard
After the above implementation is completed, determine whether push notification support for multi-device users is required for the client app. To set it up, go to Settings > Chat > Push notifications > Push notifications for multi-device users on our dashboard.
The following table explains when a notification message is sent and whether it is displayed on the device depending on your notification support settings and the device' online status. Refer to the table to find out which notification support best suits the client app use cases.
Send when all devices are offline | Send as long as one device is offline | |
---|---|---|
Single-device user | Displayed when disconnected from the server and thus offline | Displayed when disconnected from the server and thus offline |
Multi-device user | Displayed only when all devices are offline | Displayed on all offline devices, regardless of whether one or more are online |