This topic describes how to integrate the MSS on the server-side using the Java SDK.
Import the JAR package
After you configure Maven, add the following dependencies to the main pom.xml file:
For users outside the finance zone, the latest version of the Message Push V2.0 SDK is 5.0.2. For users in the finance zone, the latest version of the Message Push V2.0 SDK is 2.1.11.
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>mpaas20201028</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea-openapi</artifactId>
<version>0.3.6</version>
</dependency>Configure environment variables
Configure the MPAAS_AK_ENV and MPAAS_SK_ENV environment variables.
For Linux and macOS, run the following commands:
export MPAAS_AK_ENV=<access_key_id> export MPAAS_SK_ENV=<access_key_secret>NoteReplace
access_key_idwith your AccessKey ID andaccess_key_secretwith your AccessKey secret.Windows system configuration
Create the MPAAS_AK_ENV and MPAAS_SK_ENV environment variables. Set their values to your AccessKey ID and AccessKey secret.
Restart Windows.
API reference
Single data synchronization API
The single data synchronization API syncs data to a specific user or device.
Parameter descriptions
The parameters are as follows:
Name | Type | Required | Example | Description |
appId | String | Yes | ONEX570DA892117 | The App ID obtained from the mPaaS console. |
workspaceId | String | Yes | PROD | The Workspace ID obtained from the mPaaS console. |
bizType | String | Yes | UCHAT | The synchronization identity configured in the mPaaS console. For more information, see Console overview. |
linkToken | String | Yes | The ID of the push target. If you push data based on users, enter the user ID. If the synchronization is configured to push data based on devices, enter the device ID. | |
payload | String | Yes | testtestatapalayd | The actual business message body. The format is customizable. The length cannot exceed 4,096 characters. |
thirdMsgId | String | Yes | 1760339273 | The ID of a data synchronization request. The ID must be unique within the same synchronization identity. Requests with duplicate IDs are ignored. The ID must be less than 100 bytes. |
osType | String | No | iOS/Android | Filters pushes by mobile platform. By default, this parameter is not passed, and no filtering is performed. Data is pushed to both iOS and Android platforms. |
appMinVersion | String | No | 0.0.0.0 | Filters pushes by client version. Data is pushed only to clients with a version number greater than or equal to this value. |
appMaxVersion | String | No | 100.100.100.100 | Filters pushes by client version. Data is pushed only to clients with a version number less than or equal to this value. |
validTimeStart | String | No | 1584448493913 | Data is pushed only when the current time is greater than or equal to `validTimeStart`. |
validTimeEnd | String | No | 1584452093913 | Data is pushed only when the current time is less than or equal to `validTimeEnd`. |
Result codes for single data synchronization
Result code | Description | Solution |
SUCCESS | Success | Success |
ARGS_IS_NULL | A required parameter is empty. | Check that all required parameters are provided. |
PAYLOAD_LONG | The message payload is too long. | Check that the length of the payload parameter does not exceed the limit. |
THIRD_MSG_ID_LONG | The third-party business ID is too long. | Check that the length of the third-party business ID does not exceed the limit. |
BIZ_NOT_ONLINE | The synchronization identity for the business scenario is not published. | Go to the mPaaS console > Data Synchronization. Check that the synchronization identity that corresponds to `bizType` is configured and published. |
THIRD_MSG_ID_IS_NULL | The third-party business ID is empty. | Check that the third-party business ID is not empty. |
SYSTEM_ERROR | A system error occurred. | Contact technical support to identify the cause of the error. |
INVALID_TENANT_ID | The tenant ID is invalid. | Check that the appId is correct and that you have the permission to use it. |
Code example
import com.alibaba.fastjson.JSON;
import com.aliyun.mpaas20201028.Client;
import com.aliyun.mpaas20201028.models.CreateOpenSingleDataRequest;
import com.aliyun.mpaas20201028.models.CreateOpenSingleDataResponse;
import com.aliyun.teaopenapi.models.Config;
public static void main(String[] args) throws Exception {
// An Alibaba Cloud account AccessKey has full access to all APIs. We recommend that you use a Resource Access Management (RAM) user for API calls and routine O&M.
// To prevent AccessKey leaks and protect your resources, do not store your AccessKey ID and AccessKey secret in your project code.
// This example shows how to store the AccessKey ID and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
Config config = new Config();
// Required. Your AccessKey ID.
config.setAccessKeyId(System.getenv("MPAAS_AK_ENV"));
// Required. Your AccessKey secret.
config.setAccessKeySecret(System.getenv("MPAAS_SK_ENV"));
// The REGION_ID and Endpoint of mPaaS. This example uses a non-finance region in Hangzhou.
config.setRegionId("cn-hangzhou");
config.setEndpoint("mpaas.cn-hangzhou.aliyuncs.com");
Client client = new Client(config);
CreateOpenSingleDataRequest singleRequest = new CreateOpenSingleDataRequest();
//*************Required properties*************/
// The App ID obtained from the mPaaS console.
singleRequest.setAppId("xxxxxxx");
// The Workspace ID obtained from the mPaaS console.
singleRequest.setWorkspaceId("xxxxxxxx");
// The synchronization identity configured in Mobile Sync in the mPaaS console.
singleRequest.setBizType("TEST-SYNC");
// The ID of the user or device (UTDID) to push to.
singleRequest.setLinkToken("testUserId");
// The actual business message body. The custom length cannot exceed 4,096 characters.
singleRequest.setPayload("testPayload");
// The business ID. It must be unique and cannot exceed 100 characters in length.
singleRequest.setThirdMsgId("test_third_msg_id_" + System.currentTimeMillis());
//************Optional properties*************/
// The OS of the target device. Valid values: iOS and Android. If left empty, the OS is not restricted.
singleRequest.setOsType("IOS");
// The minimum client version number, such as 8.6.0.0.9999. If left empty, the minimum version is not restricted.
singleRequest.setAppMinVersion("0.0.0.0");
// The maximum client version number, such as 9.0.0.0.9999. If left empty, the maximum version is not restricted.
singleRequest.setAppMaxVersion("100.100.100.100");
// The start of the validity period. If left empty, the start time is not restricted.
singleRequest.setValidTimeStart(System.currentTimeMillis());
// The end of the validity period. If left empty, the end time is not restricted. The maximum validity period is 30 days.
singleRequest.setValidTimeEnd(System.currentTimeMillis() + (1000 * 3600));
CreateOpenSingleDataResponse openSingleData = client.createOpenSingleData(singleRequest);
System.out.println("response==>"+JSON.toJSONString(openSingleData));
}Make sure your AccessKey has the `AliyunMPAASFullAccess` permission. For more information, see Application-level access control for RAM accounts.
Global data synchronization API
Global data synchronization synchronizes data to all devices.
Parameter descriptions
The following are the business parameters:
Name | Type | Required | Example | Description |
appId | String | Yes | ONEX570DA892117 | The App ID obtained from the mPaaS console. |
workspaceId | String | Yes | PROD | The Workspace ID obtained from the mPaaS console. |
bizType | String | Yes | UCHAT | The synchronization identity configured in the mPaaS console. For more information, see Console overview. |
payload | String | Yes | testtestatapalayd | The actual business message body. The format is customizable. The length cannot exceed 4,096 characters. |
thirdMsgId | String | Yes | 1760339273 | The ID of a data synchronization request. The ID must be unique within the same synchronization identity. Requests with duplicate IDs are ignored. The ID must be less than 100 bytes. |
osType | String | No | IOS/ANDROID | Filters pushes by mobile platform. By default, this parameter is not passed, and no filtering is performed. Data is pushed to iOS and Android platforms. |
appMinVersion | String | No | 0.0.0.0 | Filters pushes by client version. Data is pushed only to clients with a version number greater than or equal to this value. |
appMaxVersion | String | No | 100.100.100.100 | Filters pushes by client version. Data is pushed only to clients with a version number less than or equal to this value. |
validTimeStart | String | No | 1584448493913 | Data is pushed only when the current time is greater than or equal to `validTimeStart`. |
validTimeEnd | String | No | 1584452093913 | Data is pushed only when the current time is less than or equal to `validTimeEnd`. |
maxUid | Long | No | 99 | The maximum UID for the synchronization range (the second and third to last digits of the user ID or device ID). If the digits are not letters, convert them to ASCII codes. |
minUid | Long | No | 00 | The minimum UID for the synchronization range (the second and third to last digits of the user ID or device ID). If the digits are not letters, convert them to ASCII codes. |
uids | String | No | 01,02,99 | This parameter has a higher priority than `maxUid` and `minUid`. Specifies discrete user ID segments (the second and third to last digits of the user ID or device ID). If the digits are not letters, convert them to ASCII codes. |
Result codes for global data synchronization
Result code | Description | Solution |
SUCCESS | Success | Success |
ARGS_IS_NULL | A required parameter is empty. | Check that all required parameters are provided. |
PAYLOAD_LONG | The message payload is too long. | Check that the length of the payload parameter does not exceed the limit. |
THIRD_MSG_ID_LONG | The third-party business ID is too long. | Check that the length of the third-party business ID does not exceed the limit. |
BIZ_NOT_ONLINE | The synchronization identity for the business scenario is not published. | Go to the mPaaS console > Data Synchronization. Check that the synchronization identity that corresponds to `bizType` is configured and published. |
THIRD_MSG_ID_IS_NULL | The third-party business ID is empty. | Check that the third-party business ID is not empty. |
SYSTEM_ERROR | A system error occurred. | Contact technical support to identify the cause of the error. |
NOT_SUPPORT_GLOBAL | The synchronization identity does not support global calls. | Go to the mPaaS console > Data Synchronization. Check if the synchronization identity that corresponds to the `BizType` is configured for pushing to a specific user or device. |
INVALID_TENANT_ID | The tenant ID is invalid. | Check that the appId is correct and that you have the permission to use it. |
Code example
import com.alibaba.fastjson.JSON;
import com.aliyun.mpaas20201028.Client;
import com.aliyun.mpaas20201028.models.CreateOpenGlobalDataRequest;
import com.aliyun.mpaas20201028.models.CreateOpenGlobalDataResponse;
import com.aliyun.teaopenapi.models.Config;
public static void main(String[] args) throws Exception {
// An Alibaba Cloud account AccessKey has full access to all APIs. We recommend that you use a Resource Access Management (RAM) user for API calls and routine O&M.
// To prevent AccessKey leaks and protect your resources, do not store your AccessKey ID and AccessKey secret in your project code.
// This example shows how to store the AccessKey ID and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
Config config = new Config();
// Required. Your AccessKey ID.
config.setAccessKeyId(System.getenv("MPAAS_AK_ENV"));
// Required. Your AccessKey secret.
config.setAccessKeySecret(System.getenv("MPAAS_SK_ENV"));
// The REGION_ID and Endpoint of mPaaS. This example uses a non-finance region in Hangzhou.
config.setRegionId("cn-hangzhou");
config.setEndpoint("mpaas.cn-hangzhou.aliyuncs.com");
Client client = new Client(config);
CreateOpenGlobalDataRequest globalRequest = new CreateOpenGlobalDataRequest();
//*************Required properties*************/
// The App ID obtained from the mPaaS console.
globalRequest.setAppId("BE9C457161429");
// The Workspace ID obtained from the mPaaS console.
globalRequest.setWorkspaceId("sit");
// The synchronization identity configured in Mobile Sync in the mPaaS console.
globalRequest.setBizType("test-global");
// The actual business message body. The custom length cannot exceed 4,096 characters.
globalRequest.setPayload("testtestata");
// The business ID. It must be unique and cannot exceed 100 characters in length.
globalRequest.setThirdMsgId("test_third_msg_id_" + System.currentTimeMillis());
//************Optional properties*************/
// The OS of the target device. Valid values: iOS and Android. If left empty, the OS is not restricted.
globalRequest.setOsType("IOS");
// The minimum client version number, such as 8.6.0.0.9999. If left empty, the minimum version is not restricted.
globalRequest.setAppMinVersion("0.0.0.0");
// The maximum client version number, such as 9.0.0.0.9999. If left empty, the maximum version is not restricted.
globalRequest.setAppMaxVersion("100.100.100.100");
// The maximum UID.
globalRequest.setMaxUid(Long.valueOf(99));
// The minimum UID.
globalRequest.setMinUid(Long.valueOf(1));
// The grayscale UIDs to push to, from 00 to 99. This is a string array.
globalRequest.setUids("01,02,99");
globalRequest.setValidTimeStart(System.currentTimeMillis());
globalRequest.setValidTimeEnd(System.currentTimeMillis() + (1000 * 3600));
CreateOpenGlobalDataResponse openGlobalData = client.createOpenGlobalData(globalRequest);
System.out.println("response==>"+JSON.toJSONString(openGlobalData));
}Make sure your AccessKey has the `AliyunMPAASFullAccess` permission. For more information, see Application-level access control for RAM accounts.