Implement offline push notification
ZEGOCLOUD's In-app Chat (the ZIM SDK) provides the capability of sending offline push notifications. That is, in one-on-one chat or group chat, if your app is frozen, killed by the system or a user in the backend, and get disconnected with the ZEGOCLOUD service backend due to timeout, with the offline push notification feature, the ZEGOCLOUD backend will send offline push notifications to the target users.
You can integrate the ZPNs SDK and use it together with the ZIM SDK to implement the offline push notifications feature.
The ZPNs SDK must be used with ZIM SDK 2.0.0 or later.
Solution
The solution we provide is as follows:
-
The receiver (i.e., the user receiving offline messages) enables the push channels of Google FCM, sends requests to the Google FCM push server, and obtains tokens.
-
The push servers return tokens.
-
The receiver generates a PushID, and sends it to the ZIM Server for binding the client user and PushID.
If you use the ZPNs SDK together with the ZIM SDK, the SDK will automatically bind the client user to PushID, you don't need to do other operations; If you use the ZPNs SDK alone, you will need to connect to the ZPNs server and implement the binding logic. Note: Before switching the userID on the same device, remember to call the
logout
method to remove the PushID that userID is binding. -
The sender starts sending messages, and the messages are stored in the ZIM Server.
-
The ZIM Server checks whether the receiver is online.
-
If the receiver is offline, then the messages will be transferred to the ZPNs Server.
-
The ZPNs Server sends offline push notifications to the Google FCM push server.
-
The Google FCM service pushes the offline push notifications to the receiver. The receiver receives the offline messages when gets back online.
Access Google FCM
To implement the offline push notification feature, you need to access Google FCM. For more, refer to Integrate Google FCM push notification (Android).
Integrate the ZPNs SDK
Import the ZPNs SDK
Developers have two ways to import ZPNs SDK:
- Method 1: Automatically Integrate SDK Using Maven Central
- Method 2: Manual Integration by Copying SDK Files
Set permissions
Permissions can be set as needed.
Navigate to the app/src/main
directory, open the AndroidManifest.xml
file to add permissions.
<!-- Permissions required by the ZPNs SDK. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
Set up the offline push notification feature using the ZPNs SDK
-
Create a new Java class
MyZIMPushReceiver
that extends theZPNsMessageReceiver
broadcast class in ZPNs, used for receiving push messages from Google FCM.Untitled<receiver android:name=".MyZPNsReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="im.zego.zim.zpns.intent.action.MESSAGE" /> </intent-filter> </receiver>
1 -
Implement the callbacks in the
receiver
that has inherited the classZPNsMessageReceiver
, which can be used to send related notifications to Google FCM.-
onThroughMessageReceived
: Callback for passing the notifications directly. For Google FCM, The callback can only be received when the APP is in the foreground or background. -
onRegistered
: Callback for the results of setting up the offline push notifications, this can be used to get the Push ID.
Untitledpublic class MyZPNsReceiver extends ZPNsMessageReceiver { // Callback for passing the notifications directly. @Override protected void onThroughMessageReceived(Context context, ZPNsMessage message) { Log.e("MyZPNsReceiver", "onThroughMessageReceived message:" + message.toString()); } // Callback for the results of set up the offline push notifications, this can be used to get the Push ID. @Override protected void onRegistered(Context context, ZPNsRegisterMessage message) { Log.e("MyZPNsReceiver", "onRegistered: message:" + message.getCommandResult()); } }
1 -
-
Configure the Google FCM push notification channel.
After access the Google FCM SDK as described above, call the
enableFCMPush
method to enable the Google FCM push notification service, and then call thesetPushConfig
method to configure the push notification channel.UntitledZPNsConfig zpnsConfig = new ZPNsConfig(); zpnsConfig.enableFCMPush(); // FCM zpnsManager.setPushConfig(zpnsConfig);
1 -
Set up the offline push notification feature.
UntitledZPNsManager.getInstance().registerPush(this.getApplication());
1After setting up the offline push notification feature, inherit the callback
onRegistered
of the classZPNsMessageReceiver
, and get thepushID
to push offline push notifications to specified devices. -
After finishing using this feature, call the unregisterPush method to cancel this feature. Users can't receive any notifications after it is canceled successfully.
UntitledZPNsManager.getInstance().unregisterPush();
1
Implement the offline push notification feature with the ZIM SDK
ZEGOCLOUD's In-app Chat (the ZIM SDK) provides the capability of sending offline push notifications for one-on-one or group chats, and call invitations.
Before implementing offline push, you should:
- refer to Send and receive messages to send one-on-one/group chat messages.
- refer to Call invitation to send and receive call invitations.
Send one-on-one messages with offline push notification
-
Set the offline push notification title, content, and other properties in the
ZIMPushConfig
object.UntitledZIMTextMessage textMessage = new ZIMTextMessage(); ZIMPushConfig pushConfig = new ZIMPushConfig(); pushConfig.title = "offline push notification title"; pushConfig.content = "offline push notification content"; pushConfig.payload = "Customizable field, optional"; pushConfig.resourcesID = "Resource ID, optional";
1 -
Set up the configurations for offline push notification by modifying the
pushConfig
parameter of theZIMMessageSendConfig
object.UntitledZIMMessageSendConfig sentConfig = new ZIMMessageSendConfig(); sentConfig.pushConfig = pushConfig;
1 -
The message sender calls the
sendMessage
method with thesendConfig
to send one-to-one messages.Untitledzim. sendMessage(textMessage, "myUserID", ZIMConversationType.PEER, sentConfig, new ZIMMessageSentCallback() { @Override public void onMessageSent(ZIMMessage message, ZIMError errorInfo) { } @Override public void onMessageAttached(ZIMMessage message) { } });
1 -
The receiver will receive the offline messages when gets back online.
Send group messages with offline push notification
-
Set the offline push notification title, content, and other properties in the
ZIMPushConfig
object.UntitledZIMTextMessage textMessage = new ZIMTextMessage(); ZIMPushConfig pushConfig = new ZIMPushConfig(); pushConfig.title = "offline push notification title"; pushConfig.content = "offline push notification content"; pushConfig.payload = "Customizable field, optional"; pushConfig.resourcesID = "Resource ID, optional";
1 -
Set up the configurations for offline push notification by modifying the
pushConfig
parameter of theZIMMessageSendConfig
object.UntitledZIMMessageSendConfig sentConfig = new ZIMMessageSendConfig(); sentConfig.pushConfig = pushConfig;
1 -
The message sender calls the
sendMessage
method with thesendConfig
to send group messages.Untitledzim.sendMessage(textMessage, "myGroupID", ZIMConversationType.GROUP, sentConfig, new ZIMMessageSentCallback() { @Override public void onMessageSent(ZIMMessage message, ZIMError errorInfo) { } @Override public void onMessageAttached(ZIMMessage message) { } });
1 -
The group members who are offline can receive offline messages when getting back online in the group.
Send call invitations with offline push notification
-
Set the offline push notification title, content, and other properties in the
ZIMPushConfig
object.UntitledZIMPushConfig pushConfig = new ZIMPushConfig(); pushConfig.title = "offline push notification title"; pushConfig.content = "offline push notification content"; pushConfig.payload = "Customizable field, optional"; pushConfig.resourcesID = "Resource ID, optional";
1 -
Set up the configurations for offline push notification by modifying the
pushConfig
parameter of theZIMCallInviteConfig
object.UntitledZIMCallInviteConfig callInviteConfig = new ZIMCallInviteConfig(); callInviteConfig.pushConfig = pushConfig;
1 -
The message sender calls the
callInvite
method with thecallInviteConfig
to send group messages.UntitledArrayList<String> invitees = new ArrayList<>(); invitees.add("userA"); invitees.add("userB"); zim.callInvite(invitees, callInviteConfig, new ZIMCallInvitationSentCallback() { @Override public void onCallInvitationSent(String callID, ZIMCallInvitationSentInfo info, ZIMError errorInfo) { } });
1 -
The invitees who are offline can receive an offline push notification. Once they come online, if the call invitation is still ongoing, they will receive the callback onCallInvitationReceived.