This guide briefly describes how to fast integrate MPS to the Android client. You can integrate Message Push Service (MPS) through Native AAR or Portal & Bundle method.
The complete integration process mainly includes the following four steps:
Add SDK: Add the SDK dependencies and
AndroidManifest
configuration.Initialize the SDK: Initialize the push service to establish persistent connection between the client and the mobile push gateway.
Create a service: Create a service to receive Android device IDs (Ad-tokens), so you can push messages based on device ID.
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.
If you integrate MPS through Native AAR, ensure that you have added mPaaS to project.
If you integrate MPS in componentized integration mode (through Portal & Bundle projects), ensure that you have completed the componentized integration process.
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.NoteYou can continue using the
AliPushRcvService
method in the earlier version. Click here to download the documentation about usingAliPushRcvService
.
Procedure
To use MPS, you should complete the following steps.
Add MPS SDK.
Add the push SDK dependencies and
AndroidManifest
configuration.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).
Componentized integration mode (Portal & Bundle): Install the PUSH component in the Portal and Bundle projects through Component management (AAR). For more information, see Add component dependencies.
Add
AndroidManifest
configuration. In theAndroidManifest.xml
file, add the following content:NoteIf 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>
In order to improve the arrival rate of messages, the push SDK has a built-in process keep-alive function, including the above-mentioned
com.alipay.pushsdk.BroadcastActionReceiver
to listen to the system broadcast to wake up the push process, and automatically restart after the process is recycled. When accessing, you can decide whether to enable these functions according to your own needs:If you do not need to monitor the system startup broadcast, you can delete:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <action android:name="android.intent.action.BOOT_COMPLETED" />
If you do not need to monitor the network switching broadcast, you can delete:
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
If you do not need to monitor the user wake-up broadcast, you can delete:
<action android:name="android.intent.action.USER_PRESENT" />
If you do not need to monitor the charging status change broadcast, you can delete:
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
If you do not need to monitor all the above broadcasts, you can set the
android:enabled
attribute ofcom.alipay.pushsdk.BroadcastActionReceiver
to false .If you do not need to automatically restart after the push process is recycled, you can add the following configuration under the
application
node:<meta-data android:name="force.kill.push" android:value="on" />
NoteThis configuration is only valid in baseline version 10.2.3.21 and above.
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
If you have called the mPaaS initialization method in
Application
, you can call the following method behindMP.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
orLauncherActivityAgent
, call the following method inpostInit
:MPPush.init(context);
Create a service.
Create a service to inherit
MPPushMsgServiceAdapter
, and override theonTokenReceive
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.
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.
Related operations
To improve the message arrival rate, you are recommended to integrate the push channels provided by Android mobile phone vendors. 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.