All Products
Search
Document Center

Quick start

Last Updated: Mar 23, 2022

This guide briefly describes how to fast integrate MPS to the Android client. You can integrate Message Push Service (MPS) through Native AAR, mPaaS Inside, or Portal & Bundle method.

The complete integration process mainly includes the following four steps:

  1. Add SDK: Add the SDK dependencies and AndroidManifest configuration.

  2. Initialize the SDK: Initialize the push service to establish persistent connection between the client and the mobile push gateway.

  3. Create a service: Create a service to receive Android device IDs (Ad-tokens), so you can push messages based on device ID.

  4. Bind user ID: Report user ID to the server to bind the user ID and the device ID, so you can push messages based on the user ID.

Prerequisites

  • You have completed the basic configuration with reference to the general operations.

  • You have obtained the .config configuration file from the mPaaS console. For how to generate and download the configuration file, see Add configuration file to project.

  • The MPPushMsgServiceAdapter method described in this guide only works in the baseline 10.1.68.32 or later version. If your current baseline version is lower than 10.1.68.32, please refer to mPaaS upgrade guide to upgrade the baseline version to 10.1.68.32.

    Note

    You can continue using the AliPushRcvService method in the earlier version. Click here to download the documentation about using AliPushRcvService.

Procedure

To use MPS, you should complete the following steps.

  1. Add MPS SDK.

    Add the push SDK dependencies and AndroidManifest configuration.

    1. Add SDK dependencies. Choose an integration method, and complete the required steps accordingly.

      • Native AAR: Follow the instructions in AAR component management to install the PUSH component in the project through Component management (AAR).

      • mPaaS Inside: Install the PUSH component in the project through Component management (AAR). Fore more information, see Add component dependencies.

      • Componentized integration mode (Portal & Bundle): Install the PUSH component in the Portal and Bundle projects through Component management (AAR). Fore more information, see Add component dependencies.

    2. Add AndroidManifest configuration. In the AndroidManifest.xml file, add the following content:

      Note

      If you add the SDK through Portal & Bundle, you should add the above content in the Portal project.

      <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
      
      <service
          android:name="com.alipay.pushsdk.push.NotificationService"
          android:enabled="true"
          android:exported="false"
          android:label="NotificationService"
          android:process=":push">
          <intent-filter>
              <action android:name="${applicationId}.push.action.START_PUSHSERVICE" />
          </intent-filter>
      </service>
      <receiver
          android:name="com.alipay.pushsdk.BroadcastActionReceiver"
          android:enabled="true"
          android:process=":push">
          <intent-filter android:priority="2147483647">
              <action android:name="android.intent.action.BOOT_COMPLETED" />
              <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
              <action android:name="android.intent.action.USER_PRESENT" />
              <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
          </intent-filter>
      </receiver>

      If you don't need to boot the listening system, just delete the following content:

      <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
      <action android:name="android.intent.action.BOOT_COMPLETED" />
  2. Initialize the SDK.

    Initialize the message push service to establish persistent connection between the client and the Mobile Push Gateway. The persistent connection is maintained by the SDK, and is regarded as the self-built channel.

    • Native AAR/mPaaS Inside

      • If you have called the mPaaS initialization method in Application, you can call the following method behind QuinoxlessFramework.init():

        MPPush.init(this);
      • If you haven't called the mPaaS initialization method, you can call the following methods in Application:

          MPPush.setup(this);
          MPPush.init(this);
    • Portal & Bundle

      In LauncherApplicationAgent or LauncherActivityAgent, call the following method in postInit:

      MPPush.init(context);
  3. Create a service.

    Create a service to inherit MPPushMsgServiceAdapter, and override the onTokenReceive method to receive the device token delivered by the self-built channel.

    public class MyPushMsgService extends MPPushMsgServiceAdapter {
    
        /**
         * Call back upon receiving the token delivered by the self-built channel
         *
         * @param token Device token delivered by the self-built channel
         */
        @Override
        protected void onTokenReceive(String token) {
            Log.d("Receive the token delivered by the self-built channel: " + token);
        }
    
    }

    Declare the service in AndroidManifest.xml:

    <service
        android:name="com.mpaas.demo.push.MyPushMsgService"
        android:exported="false">
        <intent-filter>
            <action android:name="${applicationId}.push.action.MESSAGE_RECEIVED" />
            <action android:name="${applicationId}.push.action.REGISTRATION_ID" />
            <category android:name="${applicationId}" />
        </intent-filter>
    </service>

    After you complete this step, you can push messages by device on the console. The device ID required refers to the token.

  4. Bind user ID.

    The user ID is customized by the developer. It can be the user ID of the real user system or other parameters that can form a mapping relationship with users, such as account and mobile phone number.

    After receiving the token, you can bind the token with the user ID:

    String userId = "Custom userId";
    ResultPbPB bindResult = MPPush.bind(context, userId, token);
    Log.d("Bind userId " + (bindResult.success ? "Succeeded" : ("Error:" + bindResult.code)));

    If you have already set the user ID by calling MPLogger, you don’t have to pass the user ID when binding it. For example:

    MPLogger.setUserId("Custom userId");
    ResultPbPB bindResult = MPPush.bind(context, token);

    To unbind the user ID, for example, the user exits the app, you can call the following method:

    ResultPbPB unbindResult = MPPush.unbind(context, userId, token);
    ResultPbPB unbindResult = MPPush.unbind(context, token);

    After you complete this step, you can push messages by user on the console. The user ID required refers to the custom user ID.

  • To improve the message arrival rate, you are recommended to integrate the push channels provided by Android mobile phone venders. Currently, MPS supports Huawei, Xiaomi, OPPO, and vivo push channels. For how to access the push channels of those vendors, see Integrate third-party channels.

  • A notification will be sent automatically when the third-party channel receives the message. The users can click on the notification to open the Web page. If you need to jump to the in-app page according to a customized DeepLink, or customize the behavior after receiving the message, see Process notification click.

For more functions, see Advanced functions.

Sample code

Click here to download the sample code.

What to do next

After you successfully integrate MPS to your Android client, you can call the RESTful interface through the server. For more information, see Configure server > Push messages.