All Products
Search
Document Center

Integrate service with Java SDK

Last Updated: Apr 02, 2021

This topic describes how to access the data synchronization service on the server by using Java SDK.

Import JAR package

After completing the Maven configuration, introduce the following dependencies in the master pom.xml file.

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>aliyun-java-sdk-mpaas</artifactId>
  4. <version>1.0.0.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.aliyun</groupId>
  8. <artifactId>aliyun-java-sdk-core</artifactId>
  9. <optional>true</optional>
  10. <version>[4.3.2,5.0.0)</version>
  11. </dependency>

API description

Single data synchronization interface

The single data synchronization interface is used to synchronize data to a specified user or device.

Parameters

Business parameters are as follows:

Parameter Data type Required Example Description
appId String Required ONEX570DA892117 Get App ID from the mPaaS console.
workspaceId String Required PROD Get Workspace ID from the mPaaS console.
bizType String Required UCHAT The synchronization identifier configured in the mPaaS console. See Console introduction for more details.
linkToken String Required Push target ID. Enter the user ID if the push is based on users. Enter the device ID if the push is based on devices.
payload String Required testtestatapalayd Actual business message body in custom format, no more than 4,096 characters in length.
thirdMsgId String Required 1760339273 Request ID for one data synchronization. Unique for one synchronization configuration. Requests of duplicate IDs will be ignored. The ID must be no more than 100 bytes.
osType String No iOS/Android Specifies the operating system of the mobile phone to which the data is to be pushed. By default, no parameters will be passed, that is, no specifications, and data will be pushed to both iOS and Android platforms.
appMinVersion String No 0.0.0.0 Specify the client version to which the data is pushed. Data is sent only to clients of the specified or later versions.
appMaxVersion String No 100.100.100.100 Specify the client version to which the data is pushed. Data is sent only to clients of the specified or earlier versions.
validTimeStart String No 1584448493913 Data will be pushed only when the current time is later than or equal to validTimeStart.
validTimeEnd String No 1584452093913 Data will be pushed only when the current time is earlier than or equal to validTimeEnd.

Result codes

Result code Description Solution
Success Synchronization succeeded. Synchronization succeeded.
ARGS_IS_NULL Required parameters are empty Check if the parameters have been completely passed according to the non-empty logical operation.
PAYLOAD_LONG PAYLOAD message body is too long Check if the length of the playload property parameter exceeds the limit.
THIRD_MSG_ID_LONG Third-party service ID is too long. Check if the third-party service ID exceeds the limit.
BIZ_NOT_ONLINE The synchronization identifier of the service scenario is not submitted. Go to mPaaS Console > Mobile Sync Service to check if the bizType synchronization identifier has been configured and submitted.
THIRD_MSG_ID_IS_NULL Third-party service ID is empty Check if the third-party service ID is empty.
SYSTEM_ERROR System error Contact technical support to confirm the cause of system errors.
INVALID_TENANT_ID Invalid tenant ID Check if the App ID is correct and If you have the permission to use the App ID.

Sample code

  1. public static void main(String[] args) {
  2. //Request information, fixed except AccessKey ID and AccessKey secret
  3. DefaultProfile.addEndpoint("cn-hongkong", "mpaas", "mpaas.cn-hongkong.aliyuncs.com");
  4. // Create and initialize a DefaultAcsClient instance.
  5. DefaultProfile profile = DefaultProfile.getProfile(
  6. "cn-hongkong", // Region ID
  7. "xxxxxx", // AccessKey ID of the RAM account
  8. "xxxxxx"); // AccessKey secret of the RAM account
  9. IAcsClient client = new DefaultAcsClient(profile);
  10. CreateOpenSingleDataRequest singleRequest = constuctSingleRequest();
  11. CreateOpenSingleDataResponse singleDataResponse;
  12. try {
  13. singleDataResponse = client.getAcsResponse(singleRequest);
  14. System.out.println("singleDataResponse:" +
  15. ToStringBuilder
  16. .reflectionToString(singleDataResponse, ToStringStyle.SHORT_PREFIX_STYLE));
  17. } catch (ServerException e) {
  18. e.printStackTrace();
  19. } catch (ClientException e) {
  20. e.printStackTrace();
  21. } catch (com.aliyuncs.exceptions.ClientException e) {
  22. e.printStackTrace();
  23. } catch (Throwable throwable) {
  24. throwable.printStackTrace();
  25. }
  26. }
  27. private static CreateOpenSingleDataRequest constuctSingleRequest() {
  28. CreateOpenSingleDataRequest singleRequest
  29. = new CreateOpenSingleDataRequest();
  30. //*************Required properties*************/
  31. //App ID obtained from the mPaaS console
  32. singleRequest.setAppId("xxxxxxx");
  33. //WorkspaceId obtained from the mPaaS console
  34. singleRequest.setWorkspaceId("xxxxxxxx");
  35. //The synchronization identifier configured during mobile synchronization in the mPaaS console
  36. singleRequest.setBizType("TEST-SYNC");
  37. //User ID or device ID to be pushed (UTDID)
  38. singleRequest.setLinkToken("testUserId");
  39. //Actual service message body, custom format with not more than 4096 characters in length.
  40. singleRequest.setPayload("testPayload");
  41. //Service ID, unique, not more than 100 characters in length.
  42. singleRequest.setThirdMsgId("test_third_msg_id_" + System.currentTimeMillis());
  43. //************Non-required properties*************/
  44. //No restriction on the operating system when the operating system of the target device, iOS or Android, is empty.
  45. singleRequest.setOsType("IOS");
  46. //Minimum client version supported, such as 8.6.0.0.9999. If the version specified here is empty, there will be no limit on the minimum client version.
  47. singleRequest.setAppMinVersion("0.0.0.0");
  48. //Maximum client version supported, such as 9.0.0.0.9999. If the version specified here is empty, there will be no limit on the maximum client version.
  49. singleRequest.setAppMaxVersion("100.100.100.100");
  50. //Start of the validity period. If it is empty, there will be no limit on the start of the validity period.
  51. singleRequest.setValidTimeStart(System.currentTimeMillis());
  52. //End of the validity period. If it is empty, there will be no limit on the end of the validity period. The longest validity period is 30 days.
  53. singleRequest.setValidTimeEnd(System.currentTimeMillis() + (1000 * 3600));
  54. return singleRequest;
  55. }

Global data synchronization interface

Global data synchronization interface is used to synchronize data to all devices.

Parameters

Business parameters are as follows:

Parameter Data type Required Example Description
appId String Required ONEX570DA892117 Get App ID from the mPaaS console.
workspaceId String Required PROD Get Workspace ID from the mPaaS console.
bizType String Required UCHAT The synchronization identifier configured in the mPaaS console. See Console introduction for more details.
payload String Required testtestatapalayd Actual service message body, custom format with not more than 4096 characters in length.
thirdMsgId String Required 1760339273 One data synchronization request ID. Unique for one synchronization identifier. Requests from duplicate IDs will be ignored. The ID must be no more than 100 bytes.
osType String No IOS/ANDROID Specifies the operating system of the mobile phone to which the data is to be pushed. By default no parameters will be passed, that is, no specifications, and data will be pushed to both iOS and Android platforms.
appMinVersion String No 0.0.0.0 Specify the client version to which the data is pushed. Data is sent only to clients of the specified or later versions.
appMaxVersion String No 100.100.100.100 Specify the client version to which the data is pushed. Data is sent only to clients of the specified or earlier versions.
validTimeStart String No 1584448493913 Data will be pushed only when the current time is later than or equal to validTimeStart.
validTimeEnd String No 1584452093913 Data will be pushed only when the current time is earlier than or equal to validTimeEnd.
maxUid Long No 99 The maximum Uid in the synchronization range. Uid is the second last character and the third last character of the user ID or device ID. If the Uid is not alphabetic, you need to convert the Uid to ASCII.
minUid Long No 00 The minimum Uid in the synchronization range. Uid is the second last character and the third last character of the user ID or device ID. If the Uid is not alphabetic, you need to convert the Uid to ASCII.
uids String No 01,02,99 The priority is higher than maxUid and minUid.
The discrete Uid segment. Uid is the second last character and the third last character of the user ID or device ID. If the Uid is not alphabetic, you need to convert the Uid to ASCII.

Result codes

Result code Description Solution
Success The task is successful. The task is successful.
ARGS_IS_NULL Required parameters are empty Check if the parameters have been completely passed according to the non-empty logical operation.
PAYLOAD_LONG PAYLOAD message body is too long Check if the length of the playload property parameter exceeds the limit.
THIRD_MSG_ID_LONG Third-party service ID is too long. Check if the third-party service ID exceeds the limit.
BIZ_NOT_ONLINE The synchronization identifier of the service scenario is not submitted. Go to mPaaS Console > Mobile Sync Service to check if the bizType synchronization identifier has been configured and submitted.
THIRD_MSG_ID_IS_NULL Third-party service ID is empty Check if the third-party service ID is empty.
SYSTEM_ERROR System error Contact technical support to confirm the cause of system errors.
NOT_SUPPORT_GLOBAL Does not support calls with global service synchronization identifier According to BizType, go to mPaaS Console > Mobile Sync Service to check if the synchronization identifier is user-based or device-based.
INVALID_TENANT_ID Invalid tenant ID Check if the App ID is correct and If you have the permission to use the App ID.

Sample code

  1. public static void main(String[] args) {
  2. //Request information, fixed except AccessKey ID and AccessKey secret
  3. DefaultProfile.addEndpoint("cn-hongkong", "mpaas", "mpaas.cn-hongkong.aliyuncs.com");
  4. // Create and initialize a DefaultAcsClient instance.
  5. DefaultProfile profile = DefaultProfile.getProfile(
  6. "cn-hongkong", // Region ID
  7. "xxxxxx", // AccessKey ID of the RAM account
  8. "xxxxxx"); // AccessKey secret of the RAM account
  9. IAcsClient client = new DefaultAcsClient(profile);
  10. CreateOpenGlobalDataRequest globalDataRequest = constuctGlobelRequest();
  11. CreateOpenGlobalDataResponse globalDataResponse;
  12. try {
  13. globalDataResponse = client.getAcsResponse(globalDataRequest);
  14. System.out.println("globalDataResponse:" +
  15. ToStringBuilder
  16. .reflectionToString(globalDataResponse, ToStringStyle.SHORT_PREFIX_STYLE));
  17. } catch (ServerException e) {
  18. e.printStackTrace();
  19. } catch (ClientException e) {
  20. e.printStackTrace();
  21. } catch (com.aliyuncs.exceptions.ClientException e) {
  22. e.printStackTrace();
  23. } catch (Throwable throwable) {
  24. throwable.printStackTrace();
  25. }
  26. }
  27. private static CreateOpenGlobalDataRequest constuctGlobelRequest() {
  28. CreateOpenGlobalDataRequest globalRequest
  29. = new CreateOpenGlobalDataRequest();
  30. //************Required properties*************/
  31. //App ID obtained from the mPaaS console
  32. globalRequest.setAppId("BE9C457161429");
  33. //WorkspaceId obtained from the mPaaS console
  34. globalRequest.setWorkspaceId("sit");
  35. //The synchronization identifier configured during mobile synchronization in the mPaaS console
  36. globalRequest.setBizType("test-global");
  37. //Actual service message body, custom format with not more than 4096 characters in length.
  38. globalRequest.setPayload("testtestata");
  39. //Service ID, unique, not more than 100 characters in length.
  40. globalRequest.setThirdMsgId("test_third_msg_id_" + System.currentTimeMillis());
  41. //************Non-required properties*************/
  42. //No restriction on the operating system when the operating system of the target device, iOS or Android, is empty.
  43. globalRequest.setOsType("IOS");
  44. //Minimum client version supported, such as 8.6.0.0.9999. If the version specified here is empty, there will be no limit on the minimum client version.
  45. globalRequest.setAppMinVersion("0.0.0.0");
  46. //Maximum client version supported, such as 9.0.0.0.9999. If the version specified here is empty, there will be no limit on the maximum client version.
  47. globalRequest.setAppMaxVersion("100.100.100.100");
  48. //Maximum Uid
  49. globalRequest.setMaxUid(Long.valueOf(99));
  50. //Minimum Uid
  51. globalRequest.setMinUid(Long.valueOf(1));
  52. //Uid 00-99 to be pushed for the phased-release, which is a string array.
  53. globalRequest.setUids("01,02,99");
  54. globalRequest.setValidTimeStart(System.currentTimeMillis());
  55. globalRequest.setValidTimeEnd(System.currentTimeMillis() + (1000 * 3600));
  56. return globalRequest;
  57. }