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 Get Started with Android.
- You understand the call frequency limit of the Chat APIs supported by different pricing plans as described in Limitations.
- You have activated the advanced features for push in Agora Console. Advanced features allow you to set the push notification mode, do-not-disturb mode, and custom push template.
You must contact support@agora.io to disable the advanced features for push as this operation will delete all the relevant configurations.
Integrate FCM with Chat
This section guides you through how to integrate FCM with Chat.
1. Create an Android project in Firebase
-
Log in to Firebase console, and click Add project.
-
In the Create a project page, enter a project name, and click Create project.
-
After the project is ready, click Continue to redirect to the project page, and click the Android icon to register an Android project.
-
In the Add Firebase to your Android app page, perform the following operations:
- In the Register app step, enter an Android package name, app nickname (optional), and debug signing certificate SHA-1 (optional), and click Register app.
- In the Download google-services.json step, download
google-services.json
, move this file into your Android app module root directory, and click Next. - In the Add Firebase SDK step, modify your
build.gradle
files to use Firebase, and click Next. - In the Next steps step, click Continue to console to go back to the project page.
-
In the project page, click the Android project you have created.
-
In the Project settings page, select the Cloud Messaging tab, and locate the Server key and Sender ID.
2. Upload FCM 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:
- Certificate Name: Fill in the Sender ID.
- Push Key: Fill in the Server Key.
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, Chat provides fine-grained options for the push notification and do-not-disturb (DND) modes at both the app and conversation levels, as shown in the following table:
ALL : Receives push notifications for all offline messages. | |||
MENTION_ONLY : Only receives push notifications for mentioned messages. | |||
NONE : Do not receive push notifications for offline messages. | |||
SILENT_MODE_DURATION : Do not receive push notifications for the specified duration. | |||
SILENT_MODE_INTERVAL : Do not receive push notifications in the specified time frame. |
Push notification mode
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 time frame at the app level. During the specified DND time periods, you do not receive any push notifications.
- Conversations only support the DND duration setting; the setting of the DND time frame does not take effect.
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:
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 |