All Products
Search
Document Center

Quick start

Last Updated: Jul 16, 2021

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

The complete access process mainly includes the following 4 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.
  • You can continue using the AliPushRcvService method in the earlier version. Please submit a ticket or contact the mPaaS support staff to obtain the old version documents.

Add SDK

Add the SDK dependencies and AndroidManifest configuration.

Add SDK dependencies

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.

Portal & Bundle

Install the PUSH component in the Portal and Bundle projects through Component management (AAR).

Fore more information, see Add component dependencies.

Add AndroidManifest configuration

In the AndroidManifest.xml file, add the following content:

 
  1. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  2. <service
  3. android:name="com.alipay.pushsdk.push.NotificationService"
  4. android:enabled="true"
  5. android:exported="false"
  6. android:label="NotificationService"
  7. android:process=":push">
  8. <intent-filter>
  9. <action android:name="${applicationId}.push.action.START_PUSHSERVICE" />
  10. </intent-filter>
  11. </service>
  12. <receiver
  13. android:name="com.alipay.pushsdk.BroadcastActionReceiver"
  14. android:enabled="true"
  15. android:process=":push">
  16. <intent-filter android:priority="2147483647">
  17. <action android:name="android.intent.action.BOOT_COMPLETED" />
  18. <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
  19. <action android:name="android.intent.action.USER_PRESENT" />
  20. <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
  21. </intent-filter>
  22. </receiver>

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

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

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

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():

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

       
    1. MPPush.setup(this);
    2. MPPush.init(this);

Portal & Bundle

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

 
  1. MPPush.init(context);

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.

 
  1. public class MyPushMsgService extends MPPushMsgServiceAdapter {
  2. /**
  3. * Call back upon receiving the token delivered by the self-built channel
  4. *
  5. * @param token Device token delivered by the self-built channel
  6. */
  7. @Override
  8. protected void onTokenReceive(String token) {
  9. Log.d("Receive the token delivered by the self-built channel: " + token);
  10. }
  11. }

Declare the service in AndroidManifest.xml:

 
  1. <service
  2. android:name="com.mpaas.demo.push.MyPushMsgService"
  3. android:exported="false">
  4. <intent-filter>
  5. <action android:name="${applicationId}.push.action.MESSAGE_RECEIVED" />
  6. <action android:name="${applicationId}.push.action.REGISTRATION_ID" />
  7. <category android:name="${applicationId}" />
  8. </intent-filter>
  9. </service>

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

Bind user ID

This 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:

 
  1. String userId = "Custom userId";
  2. ResultPbPB bindResult = MPPush.bind(context, userId, token);
  3. 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:

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

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

 
  1. ResultPbPB unbindResult = MPPush.unbind(context, userId, token);
  2. 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 access 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 Access 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.