Offline push
Chat supports the integration of Google Firebase Cloud Messaging (FCM). This enables Android developers to use an offline push notification service. The service features low latency, high delivery, high concurrency, and no violation of the users' personal data.
Understand the tech
The following figure shows the basic workflow of Chat:
Assume that User A sends a message to User B, but User B goes offline before receiving it. The Chat server pushes a notification to the device of User B via FCM and temporarily stores this message. Once User B comes back online, the Chat SDK pulls the message from the server and pushes the message to User B using persistent connections.
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 FCM with Chat
This section guides you through how to integrate FCM with Chat.
1. Create a project in Firebase
-
Log in to the Firebase console and add a project.
-
Register the application in the project.
In the Download and then add config file step of the Add Firebase to your Android app page, click Download google-services.json and put the file into your Android app module root directory.
-
Query the sender ID. On the Project settings page, select the Cloud Messaging tab and view Sender ID. When uploading the FCM certificate to Agora Console, set the certificate name to the FCM sender ID.
-
On the Project settings page, select the Service accounts tab and click Generate new private key to generate a JSON file. Save this file and upload it to Agora Console when using the V1 certificate.
2. Upload FCM certificate to Agora Console
After successfully logging into Chat, you can upload the FCM push certificate to Agora Console:
-
Log in to Agora Console, and click Project Management in the left navigation bar.
-
On the Project Management page, locate the project that has Chat enabled and click Config.
-
On the project edit page, click Config next to Chat.
-
On the project config page, select Features > Push Certificate and click Add Push Certificate.
-
In the pop-up window, select the Google tab, and configure the following fields:
Parameter Type Required Description Certificate Type No Select whether to use a V1 certificate or a legacy certificate. - V1: Recommended. You need to configure a Private Key.
- Legacy: Will soon be deprecated. You need to configure a Push Key.
Private Key file Yes Click Generate new private key on the Project settings > Service accounts page of the Firebase Console to generate the .json
file, then upload it to Agora Console.Push Key String Yes FCM Server Key. Obtain the server key in the Cloud Messaging API (Legacy) area of the Project settings > Cloud Messaging page of the Firebase Console.
This parameter is only valid for legacy certificates.Certificate Name String Yes The sender ID configured for the FCM. - For the new version of the certificate, you can find the sender ID on the Project settings > Cloud Messaging page of the Firebase Console.
- For legacy certificates, go to the *Project settings > Cloud Messaging page of the Firebase Console, and get the sender ID in the Cloud Messaging API (Legacy) area.
The certificate name is the only condition used by the Agora server to determine which push channel the target device uses, so ensure that the sender set when integrating FCM in Chat is consistent with what is set here.Sound String No The ringtone flag for when the receiver gets the push notification. Push Priority No Message delivery priority. See Setting the priority of a message. Push Msg Type No The type of the message sent to the client through FCM. See Message types: - Data: Data message, processed by the client application.
- Notification: Notification message, automatically processed by FCM SDK.
- Both: Notification messages and data messages can be sent through the FCM client.
Switch from legacy to the V1 certificate
The legacy HTTP or XMPP API is being retired on June 20, 2024. In view of this, switch to the latest FCM API (HTTP v1) version of the certificate as soon as possible. See Firebase Console for details.
Make sure that the uploaded V1 certificate is available, as the legacy one will be deleted upon upload. If the new certificate is not available, the push will fail.
Take the following steps to switch from the old to the new certificate:
-
Click Edit in the Action column of the old certificate on the Push Certificate page.
-
In the Google tab of the Edit Push Certificate window, switch the Certificate Type to V1.
-
Click Upload to upload the locally saved V1 certificate file (.json).
-
Click OK to complete the switch.
3. Enable FCM in Chat
- Sync the project with the gradle files, extend
FirebaseMessagingService
, and registerFirebaseMessagingService
in theAndroidManifest.xml
file of your project.
- Initialize and enable FCM in the Chat SDK.
- Pass the device token of FCM to the Chat SDK.
- Listen for device token generation.
Rewrite the onNewToken
callback of FirebaseMessagingService
. Once a device token is generated, this callback passes the new device token to the Chat SDK at the earliest opportunity.
Set up push notifications
To optimize user experience when dealing with an influx of push notifications, Agora Chat provides fine-grained options for the push notification and do-not-disturb (DND) modes at both the app and conversation levels.
Push notification mode
Push Notification Mode | Description | Application Scope |
All | Receives push notifications for all offline messages. | Application and one-to-one and group chat conversations |
MENTION_ONLY | Only receives push notifications for mentioned messages. This mode is recommended for group chats. To mention one or more members in a group, you need to pass em_at_list":["user1", "user2" ...] for the ext field; to mention all members in a group, pass "em_at_list":"all" for the ext field. | |
NONE | Do not receive push notifications for offline messages. |
The setting of the push notification mode at the conversation level takes precedence over that at the app level, and those conversations that do not have specific settings for the push notification mode inherit the app setting by default.
For example, assume that the push notification mode of the app is set to MENTION_ONLY
, while that of the specified conversation is set to ALL
. You receive all the push notifications from this conversation, while you only receive the push notifications for mentioned messages from all the other conversations.
Do-not-disturb mode
You can specify both the DND duration and DND interval at the app level. During the specified DND time periods, you do not receive any push notifications.
Do-not-disturb Parameter | Type | Description | Application Scope |
SILENT_MODE_INTERVAL | Number | The interval during which the DND mode is scheduled everyday. The time is represented in the 24-hour notation in the form of H:M, for example, 8:30-10:00, where H ranges from SILENT_MODE_INTERVAL and SILENT_MODE_DURATION are set, the DND mode works in both periods. For example, at 8:00, you set SILENT_MODE_INTERVAL to 8:0-10:0 and SILENT_MODE_DURATION to 240 (4 hours) for the app, the app stays in DND mode during 8:00-12:00 on the current day and 8:00-10:00 in the later days. |
App |
SILENT_MODE_DURATION | Number | The DND duration in minutes. The value range is [0,10080], where 0 indicates that this parameter is invalid and 10080 indicates that the DND mode lasts for 7 days.
SILENT_MODE_INTERVAL set as a daily period, this parameter specifies that the DND mode works only for the given duration starting from the current time. For example, if this parameter is set to 240 (4 hours) for the app at 8:00, the DND mode lasts only during 8:00-12:00 on the current day.SILENT_MODE_INTERVAL and SILENT_MODE_DURATION are set, the DND mode works in both periods. For example, at 8:00, you set SILENT_MODE_INTERVAL to 8:0-10:0 and SILENT_MODE_DURATION to 240 (4 hours) for the app, the app stays in DND mode during 8:00-12:00 on the current day and 8:00-10:00 in the later days. |
App and one-to-one and group chat conversations in it |
For both the app and all the conversations in the app, the setting of the DND mode takes precedence over the setting of the push notification mode.
For example, assume that a DND time period is specified at the app level and the push notification mode of the specified conversation is set to ALL
. The DND mode takes effect regardless of the setting of the push notification mode, that is, you do not receive any push notifications during the specified DND time period.
Alternatively, assume that a DND time period is specified for a conversation, while the app does not have any DND settings and its push notification mode is set to ALL
. You do not receive any push notifications from this conversation during the specified DND time period, while the push of all the other conversations remains the same.
Set the push notifications of an app
You can call setSilentModeForAll
to set the push notifications at the app level and set the push notification mode and DND mode by specifying the SilentModeParam
field, as shown in the following code sample:
Retrieve the push notification setting of an app
You can call getSilentModeForAll
to retrieve the push notification settings at the app level, as shown in the following code sample:
Set the push notifications of a conversation
You can call setSilentModeForConversation
to set the push notifications for the conversation specified by the conversationId
and ConversationType
fields, as shown in the following code sample:
Retrieve the push notification setting of a conversation
You can call getSilentModeForConversation
to retrieve the push notification settings of the specified conversation, as shown in the following code sample:
Retrieve the push notification settings of multiple conversations
- You can retrieve the push notification settings of up to 20 conversations at each call.
- If a conversation inherits the app setting or its push notification setting has expired, the returned dictionary does not include this conversation.
You can call getSilentModeForConversations
to retrieve the push notification settings of multiple conversations, as shown in the following code sample:
Clear the push notification mode of a conversation
You can call clearRemindTypeForConversation
to clear the push notification mode of the specified conversation. Once the specific setting of a conversation is cleared, this conversation inherits the app setting by default.
The following code sample shows how to clear the push notification mode of a conversation:
Set up display attributes
Set the display attributes of push notifications
You can call updatePushNickname
to set the nickname displayed in your push notifications, as shown in the following code sample:
This nickname indicates the nickname of the message sender that is displayed in the push notification bar of the recipient's client when a message from the user is pushed. The nickname can be different from the nickname in user attributes. However, Agora recommends that you use the same nickname for both. Therefore, if either nickname is updated, the other should be changed at the same time. To update the nickname in user attributes, see Set user attributes.
You can also call updatePushDisplayStyle
to set the display style of push notifications, as shown in the following code sample:
Retrieve the display attributes of push notifications
You can call getPushConfigsFromServer
to retrieve the display attributes in push notifications, as shown in the following code sample:
Set up push translations
If a user enables the automatic translation feature and sends a message, the SDK sends both the original message and the translated message.
Push notifications work in tandem with the translation feature. As a receiver, you can set the preferred language of push notifications that you are willing to receive when you are offline. If the language of the translated message meets your setting, the translated message displays in push notifications; otherwise, the original message displays instead.
The following code sample shows how to set and retrieve the preferred language of push notifications:
Set up push templates
Chat allows users to use ready-made templates for push notifications.
You can create and provide push templates for users by referring to the following steps:
-
Log in to Agora Console, and click Project Management in the left navigation bar.
-
On the Project Management page, locate the project that has Chat enable and click Config.
-
On the project edit page, click Config next to Chat.
-
On the project config page, select Features > Push Template and click Add Push Template, and configure the fields in the pop-up window, as shown in the following figure:
Once the template creation is complete in Agora Console, users can choose this push template as their default layout when sending a message, as shown in the following code sample:
What's next
This section includes more versatile push notification features that you can use to implement additional functions if needed.
If the ready-made templates do not meet your requirements, Chat also enables you to customize your push notifications.
Custom fields
The following code sample shows how to add an extension field in push notifications:
Parameter | Description |
---|---|
txtBody | The content of the text message. |
toChatUsername | The username of the receiver. |
em_apns_ext | The custom key used to add the extension field. Note: Do not modify the key. Modify the value of the key only. |
The following example shows a RemoteMessage
object received by the remote user:
Parameter | Description |
---|---|
data | The custom data of the push notification. |
alert | The displayed content of the push notification. This value varies based on the setting of DisplayStyle . |
test1 | The custom field of the push notification. |
Custom displays
The following code sample shows how to customize the display style in push notifications:
Parameter | Description |
---|---|
toChatUsername | The username of the sender. |
em_apns_ext | The custom key used to add the extension field. Note: Do not modify the key. Modify the value of the key only. |
em_push_title | The custom key used to specify the custom titles of push notifications. Note: Do not modify the key. Modify the value of the key only. |
em_push_content | The custom key used to specify the custom displayed content of push notifications. Note: Do not modify the key. Modify the value of the key only. |
The following example shows a RemoteMessage
object received by the remote user:
Parameter | Description |
---|---|
data | The custom data of the push notification. |
alert | The displayed content of the push notification. This value varies based on the setting of DisplayStyle . |
push_title | The custom title of the push notification. |
push_content | The custom content of the push notification. |
Force push notifications
Once you force a push notification to a user, the user receives the message regardless of their settings on the push nosh notification and DND modes.
The following code sample shows how to force a push notification:
Parameter | Description |
---|---|
txtBody | The message body. |
toChatUsername | The username of the receiver. |
em_force_notification | Whether to force a push notification.true : Yesfalse : No |