Integrate offline push
Updated
Integrate push notifications.
Follow this guide to integrate and test push notifications in your environment.
Prerequisites
Before proceeding, ensure that you meet the following requirements:
- You have initialized the Chat SDK. For details, see SDK quickstart.
- You understand the call frequency limit of the Chat APIs supported by different pricing plans as described in Limitations.
Integrate APNs push
This section guides you through how to integrate APNs with Chat. To integrate APNs into Chat, follow these steps:
1. Create a push certificate on the Apple Developer Platform
APNs supports p8 and p12 certificates. The Agora server needs your APNs certificate to communicate with APNs and send push notifications to the client.
The following steps describe how to create a p12 certificate on Apple's developer platform:
-
Apply for the Certificate Signing Request (CSR) file.
-
Open the Keychain Access app on your device and select Keychain Access > Certificate Assistant > Request a Certificate from a Certificate Authority.
-
In the Certificate Assistant dialog box, fill in the User Email Address and Common Name, select Saved to disk for Request is, click Continue, and add a storage path to save the file.
-
The CSR file
CertificateSigningRequest.certSigningRequestis generated under the storage path.
2. Create an App ID
- Log in to iOS Developer Center,choose Account > Certificates, Identifiers & Profiles > Identifiers.
- On the Identifiers tab, click the + icon to the right of Identifiers.
- On the Register a new identifier page, select App IDs and click Continue.
- For Select a type, select App and click Continue.
- On the Register an App ID page, configure the following fields:
- Description: Description of the App ID.
- Bundle ID: Can be set to com.YourCompany.YourProjectName.
- Capabilities: Choose Push Notification.
- Make sure the information is correct and click Register.
3. Create push notification certificates
Create certificates for development and production environments, respectively:
- On the Identifiers tab, select the App ID created in the previous step.
- On the Edit your App ID Configuration page, find Push Notifications and click Configure.
- In the Apple Push Notification service SSL Certificates dialog box, click Create Certificate to create a push certificate for development or production environments.
- On the Create a New Certificate page, select iOS as the Platform, upload the CSR file created in step 1, and click Continue.
- On the Download Your Certificate page, click Download to generate the APNs certificate.
4. Generate the push certificate
- Double-click to import the push certificate created in Keychain in previous steps.
- Open Keychain Access , select login > Certificates, find the imported certificate, right-click and select the certificate to export as a
.p12file, and set the certificate key.
5. Generate the Provisioning Profile file
- Open iOS Developer Center,select Account > Certificates, Identifiers & Profiles > *Profiles.
- On the Provisioning tab, click the + icon to the right of Profiles.
- On the Register a New Provisioning Profile page, select iOS App Development for Development, select Ad Hoc for Distribution, and click Continue. For the official version on the App Store, select App Store for Distribution.
- On the Generate a Provisioning Profile page, configure the following fields:
- App ID: Enter the App ID created in step 2.
- Select Certificates: Select the
.p12file generated in step 4. - Select Devices: Select the device to be developed.
- Provisioning Profile Name: Enter the Provisioning Profile file name.
- Confirm the information and click Download to generate the Provisioning Profile file.
6. Upload the APNs certificate in the Agora Console
After successfully logging in to the Chat SDK, you can configure the push policy for multi-device login use-cases and upload the APNs push certificate in Agora Console.
-
Log in to Agora Console and click Project Management in the left navigation bar.
-
On the Project Management page, click Config in the Action column for the project that has Chat enabled.
-
On the Edit Project page, in the Features area, click Enable/Config for Chat.
-
On the project configuration page, select Features > Push Certificate.
-
On the Push Certificate page, click Add Push Certificate. In the dialog box that pops up, select the Apple tab and configure the fields.
If you use FCM push, select the Google tab and configure the FCM push parameters.
| Parameter | Type | Required (Yes/No) | Description |
|---|---|---|---|
| Certificate Type | Yes | Message push certificate type, currently supports p8 and p12. | |
| Certificate Name | String | Yes | The name of the message push certificate. Enter the name of the message push certificate. |
| Push Key | String | Yes | Message push certificate key. Fill in the certificate key set when exporting the message push certificate file. Configure this parameter only when using a p12 certificate. |
| Upload Certificate | File | Yes | Click Upload to upload the message push certificate file. |
| Key ID | String | Yes | Enter the Key ID of the push certificate. Configure this parameter only when using a p8 certificate. |
| **TeamID ** | String | Yes | Enter the Team ID of the push certificate. Configure this parameter only when using a p8 certificate. |
| Integration Environment | Yes | Integration environment: - Development: Development environment; - Production: Production environment. | |
| Bundle ID | String | Yes | The bundle ID. The bundle ID set when creating the App ID. |
| sound | String | No | The ringtone alert when the receiver receives the push notification. |
7. Integrate APNs on the client
-
Open Xcode and select Targets > Capability > Push Notifications to enable push notification permission.
-
Pass the certificate name to the SDK:
#import - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Apply for notification permission UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { NSLog(@"request authorization succeed"); } }]; // Register push [application registerForRemoteNotifications]; // Initialize options and set App Key AgoraChatOptions *options = [AgoraChatOptions optionsWithAppkey:@"XXXX#XXXX"]; // Fill in the name you set when uploading the certificate. Make sure the certificate name you set here is consistent with the certificate name you filled in on Agora Console. options.apnsCertName = @"PushCertName"; [AgoraChatClient.sharedClient initializeSDKWithOptions:options]; return YES; } -
Get the device token and pass it to the server.
After the device token is registered, the iOS system will call back the device token to you in the following way. Pass the device token to the SDK.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[AgoraChatClient.sharedClient registerForRemoteNotificationsWithDeviceToken:deviceToken completion:^(AgoraChatError *aError) {
if (aError) {
NSLog(@"bind deviceToken error: %@", aError.errorDescription);
}
}];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Register Remote Notifications Failed");
}Test APNs push
After integrating and enabling FCM, you can test whether the push feature is successfully integrated.
Make sure your test device is a non-jail-broken iOS device. For more reliable testing, use a physical device.
Test push notifications
-
Log in to the app on your device and confirm that the device token is successfully bound.
You can check the log or call RESTful API for getting user details to confirm whether the device token is successfully bound. If successful, there will be a
pushInfofield under theentitiesfield, andpushInfowill have relevant information such asdevice_Id,device_token,notifier_name, and others. -
Enable app notification bar permissions.
-
Kill the application process.
-
Send a test message in the Agora Console.
Select Operation Management > User in the left navigation bar. On the Users page, select Send Admin Message in the Action column for the corresponding user ID. In the dialog box that pops up, select the message type, enter the message content, and then click Send .
In the Push Certificate page, in the Action column of each certificate, click More and Test will appear. This is to directly call the third-party interface to push. The message sending test in the Users page first calls the Chat message sending interface and then the third-party interface when the conditions are met (that is, the user is offline, the push certificate is valid and bound to the device token).
-
Check your device to see if it has received the push notification.
Troubleshooting
In case of issues, take the following steps:
-
Check whether APNs push is correctly integrated or enabled:
Select Operation Management > User in the left navigation bar. On the User Management page, select Push Certificate in the Action column for the corresponding user ID. In the pop-up box, check whether the certificate name and device token are displayed correctly.
-
Check whether the correct APNs certificate has been uploaded in Agora Console and the correct certificate environment has been set.
-
Check whether the message was pushed in the chat room. Chat rooms do not support offline message push.
-
Check whether online-only delivery is enabled (
AgoraChatMessage#deliverOnlineOnly = YES). Messages set to be delivered only when the receiver is online will not be pushed.
