logo
In-app Chat
SDK Error Codes
Powered Byspreading
On this page

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.

Warning

The ZPNs SDK must be used with ZIM SDK 2.0.0 or later.

Solution

The solution we provide is as follows:

  1. 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.

  2. The push servers return tokens.

  3. 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.

  4. The sender starts sending messages, and the messages are stored in the ZIM Server.

  5. The ZIM Server checks whether the receiver is online.

  6. If the receiver is offline, then the messages will be transferred to the ZPNs Server.

  7. The ZPNs Server sends offline push notifications to the Google FCM push server.

  8. 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.

Untitled
<!-- 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" />
1
Copied!

Set up the offline push notification feature using the ZPNs SDK

  1. Create a new Java class MyZIMPushReceiver that extends the ZPNsMessageReceiver 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
    Copied!
  2. Implement the callbacks in the receiver that has inherited the class ZPNsMessageReceiver, 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.

    Untitled
    public 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
    Copied!
  3. 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 the setPushConfig method to configure the push notification channel.

    Untitled
    ZPNsConfig zpnsConfig = new ZPNsConfig();
    zpnsConfig.enableFCMPush(); // FCM
    zpnsManager.setPushConfig(zpnsConfig);
    
    1
    Copied!
  4. Set up the offline push notification feature.

    Untitled
    ZPNsManager.getInstance().registerPush(this.getApplication());
    
    1
    Copied!

    After setting up the offline push notification feature, inherit the callback onRegistered of the class ZPNsMessageReceiver, and get the pushID to push offline push notifications to specified devices.

  5. After finishing using this feature, call the unregisterPush method to cancel this feature. Users can't receive any notifications after it is canceled successfully.

    Untitled
    ZPNsManager.getInstance().unregisterPush();
    
    1
    Copied!

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.

Warning

Before implementing offline push, you should:

Send one-on-one messages with offline push notification

  1. Set the offline push notification title, content, and other properties in the ZIMPushConfig object.

    Untitled
    ZIMTextMessage 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
    Copied!
  2. Set up the configurations for offline push notification by modifying the pushConfig parameter of the ZIMMessageSendConfig object.

    Untitled
    ZIMMessageSendConfig sentConfig = new ZIMMessageSendConfig();
    sentConfig.pushConfig = pushConfig;
    
    1
    Copied!
  3. The message sender calls the sendMessage method with the sendConfig to send one-to-one messages.

    Untitled
    zim. sendMessage(textMessage, "myUserID", ZIMConversationType.PEER, sentConfig, new ZIMMessageSentCallback() {
         @Override
         public void onMessageSent(ZIMMessage message, ZIMError errorInfo) {
    
         }
    
         @Override
         public void onMessageAttached(ZIMMessage message) {
    
         }
    });
    
    1
    Copied!
  4. The receiver will receive the offline messages when gets back online.

Send group messages with offline push notification

  1. Set the offline push notification title, content, and other properties in the ZIMPushConfig object.

    Untitled
    ZIMTextMessage 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
    Copied!
  2. Set up the configurations for offline push notification by modifying the pushConfig parameter of the ZIMMessageSendConfig object.

    Untitled
    ZIMMessageSendConfig sentConfig = new ZIMMessageSendConfig();
    sentConfig.pushConfig = pushConfig;
    
    1
    Copied!
  3. The message sender calls the sendMessage method with the sendConfig to send group messages.

    Untitled
    zim.sendMessage(textMessage, "myGroupID", ZIMConversationType.GROUP, sentConfig, new ZIMMessageSentCallback() {
         @Override
         public void onMessageSent(ZIMMessage message, ZIMError errorInfo) {
    
         }
         
         @Override
         public void onMessageAttached(ZIMMessage message) {
    
         }
    });
    
    1
    Copied!
  4. The group members who are offline can receive offline messages when getting back online in the group.

Send call invitations with offline push notification

  1. Set the offline push notification title, content, and other properties in the ZIMPushConfig object.

    Untitled
    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
    Copied!
  2. Set up the configurations for offline push notification by modifying the pushConfig parameter of the ZIMCallInviteConfig object.

    Untitled
    ZIMCallInviteConfig callInviteConfig = new ZIMCallInviteConfig();
    callInviteConfig.pushConfig = pushConfig;
    
    1
    Copied!
  3. The message sender calls the callInvite method with the callInviteConfig to send group messages.

    Untitled
    ArrayList<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
    Copied!
  4. 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.

Previous

Instruction

Next

Set custom push rules