Offline Push (Google FCM)

Last updated: 2020-06-23 15:28:42

    To use FCM offline push, you need to install Google Play Services on your mobile phone and use it outside Mainland China.

    Offline Push Process

    The process of implementing offline message push is as follows:

    1. Register with the vendor and complete the developer verification process. Apply to enable the push service.
    2. Create a push service and bind app information to obtain the push certificate, password, key, and other data.
    3. Log in to the IM Console to upload the certificate and enter other required information. The IM server uses the certificate to generate a unique certificate ID.
    4. Integrate the push messaging SDK provided by the vendor with your project and configure it according to the vendor's instructions.
    5. Send your certificate ID and device information to IM server.
    6. When the client App is killed by the system or user without IM logout, the IM server will remind the user via message push.

    Procedure

    Step 1: set Firebase and FCM SDK

    The website in this step is the official website of Firebase, which is accessible only outside Mainland China.

    1. Refer to Firebase Cloud Message Transfer to set Firebase and integrate the FCM SDK. After launching the app, obtain the device registration token.
    2. Refer to FCM Test Guide to test notification messages and make sure that FCM has been integrated successfully.
    3. Log in to the Firebase console and click your app card to enter the app configuration page.
    4. Click on the right side of Project Overview, and choose Project Settings -> Service Account.
    5. Click Generate New Private Key to download the private key.

    Step 2: generate a certificate ID

    1. Log in to the IM Console and click the desired app. The app configuration page appears.
    2. Click Add Certificate under Android Platform Push Settings.

      You already have a certificate and only need to change its information, click Edit.

    3. Upload the private key you obtained in step 1.
    4. Click OK to save the information. Certificate information takes effect 10 minutes after you save it.
    5. Record the Certificate ID once it is generated.

    Step 3: report the push information to the IM server

    After users successfully log in, use the setOfflinePushToken method of TIMManager to report the Certificate ID, generated and hosted by the IM console, and token, generated by the client after FCM integration, to the IM server.

    After the token and certificate ID are correctly reported, the IM service can bind users with the corresponding device information, thus enabling the use of FCM to push notifications.

    The following is sample code from the demo:

    Define the certificate ID constant:

    /****** FCM offline push parameter start ******/
    // Use your certificate ID in the FCM push certificate information on the IM console
    public static final long GOOGLE_FCM_PUSH_BUZID = 6768;
    /****** FCM offline push parameter end ******/

    Reporting the push certificate ID and token:

    /**
     * Report the push certificate ID and device information in ThirdPushTokenMgr.java
     */
    public class ThirdPushTokenMgr {
        private static final String TAG = "ThirdPushTokenMgr";
        private String mThirdPushToken;
    
        public static ThirdPushTokenMgr getInstance () {
            return ThirdPushTokenHolder.instance;
        }
    
        private static class ThirdPushTokenHolder {
            private static final ThirdPushTokenMgr instance = new ThirdPushTokenMgr();
        }
    
        public String getThirdPushToken() {
            return mThirdPushToken;
        }
    
        public void setThirdPushToken(String mThirdPushToken) {
            this.mThirdPushToken = mThirdPushToken;  // Token value specified here
        }
    
        public void setPushTokenToTIM(){
            String token = ThirdPushTokenMgr.getInstance().getThirdPushToken();
            if(TextUtils.isEmpty(token)){
                QLog.i(TAG, "setPushTokenToTIM third token is empty");
                mIsTokenSet = false;
                return;
            }
            TIMOfflinePushToken param = new TIMOfflinePushToken(Constants.GOOGLE_FCM_PUSH_BUZID, token);
            TIMManager.getInstance().setOfflinePushToken(param, new TIMCallBack() {
                @Override
                public void onError(int code, String desc) {
                    Log.d(TAG, "setOfflinePushToken err code = " + code);
                }
    
                @Override
                public void onSuccess() {
                    Log.d(TAG, "setOfflinePushToken success");
                    mIsTokenSet = true;
                }
            });
        }
    }

    Step 4: offline push

    After the certificate ID and token are successfully reported, the IM server sends messages via FCM push notifications to the user before IM user logout on the device and even if the app is killed.

    • FCM push is not 100% successful in reaching target users.
    • FCM push may be delayed. Usually, this is related to the timing of app killing. In some cases, it is related to the FCM push service.
    • If the IM user has logged out or been forced offline by the IM server (for example, due to login on another device), the device cannot receive pushed messages.

    Custom Content Pass Through

    Step 1: custom content configuration (Sender)

    Set the custom content for the notification bar message before sending the message.

    • Android sample:

      String extContent = "ext content";
      
      TIMMessageOfflinePushSettings settings = new TIMMessageOfflinePushSettings();
      settings.setExt(extContent.getBytes());
      timMessage.setOfflinePushSettings(settings);
      mConversation.sendMessage(false, timMessage, callback);
    • For information on configurations for the IM server, refer to the OfflinePushInfo Format Example.

    Step 2: custom content configuration (receiver)

    The client will obtain the custom content from the corresponding Activity once the notification bar message is clicked.

      Bundle bundle = getIntent().getExtras();
      String value = bundle.getString("ext"); 

    FAQs

    Can I set a custom notification sound?

    Currently, FCM push does not support custom alert sounds.

    I cannot receive pushed messages. What should I do?

    1. No push service is 100% successful in reaching target users, and FCM is no exception. Therefore, if 1 or 2 pushed messages fail to reach users during a fast, continuous push process, it is usually due to the restrictions of FCM's push frequency control.
    2. According to the push process, confirm whether the FCM push certificate information is correctly configured on the IM console.
    3. Confirm that your FCM project has been configured correctly and has obtained a token normally.
    4. Confirm that you have reported push information to the IM server correctly.
    5. Manually kill the App on your device, send a few messages, and confirm whether you receive notifications within 1 minute.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help