All Products
Search
Document Center

API reference - server

Last Updated: Apr 02, 2021

Message Push Service (MPS) provides the following OpenAPIs for the server to implement four types of message push: simple push, template push, multiple push, and broadcast push.

API Description
Simple push Pushes one message to one target ID.
Template push Pushes one message to one target ID. The message is created based on a template.
Multiple push Pushes different messages to multiple target IDs. Based on the template, configure different template placeholders for the target IDs to implement personalized message push.
Broadcast push Pushes the same message to all devices. The message is created based on a template.

SDK preparations

MPS supports four programming languages: Java, Python, Node.js, and PHP. Before you call the preceding APIs for message push, you should make different preparations for different programming languages.

The following examples describe the preparations needed before implementing the SDK for different programming languages.

Java

Before you call the preceding four APIs for message push, introduce the Maven configuration. Import the following dependencies to the main pom 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>

Python

Run the following commands to add relevant dependencies.

  1. ## Alibaba Cloud SDK
  2. pip install aliyun-python-sdk-core
  3. ## mpaas SDK
  4. pip install aliyun-python-sdk-mpaas

Node.js

Run the following commands to add relevant dependencies.

  1. npm i @alicloud/mpaas20190821

PHP

Run the following commands to add relevant dependencies.

  1. composer require alibabacloud/sdk

Simple push

You can call this API to push one message to one target ID.

Note: Before you call this API, you must introduce the required dependencies. For more information, see SDK preparations.

Parameters

Parameter Type Required Example Description
taskName String Yes simpleTest The name of push task
title String Yes Test Message title
content String Yes Test Message body
appId String Yes ONEX570DA89211721 mPaaS App ID
workspaceId String Yes test mPaaS workspace
deliveryType Long Yes 3 The type of target ID. Valid values:
  • 1: Android device
  • 2: iOS device
  • 3: User
targetMsgkey String Yes {“user1024”:”1578807462788”} Targets to which the message will be pushed, in the map format:
  • key: the target, which depends on the value of deliveryType.
    If the value of deliveryType is 1, the key is Android device ID.
    If the value of deliveryType is 2, the key is iOS device ID.
    If the value of deliveryType is 3, the key is userid.
  • value: the business ID of the message, which is user-defined and must be unique.
Note that the number of the targets cannot exceed 10.
expiredSeconds Long Yes 300 The validity period of message, in seconds.
extendedParams String No {“key1”:”value1”} The extension parameters, in the map format.
pushAction Long No 0 The redirection method upon a tap on the message. Valid values:
  • 0: Web URL
  • 1: Intent Activity
The default redirection method is Web URL.
uri String No http://www The URL to be redirected to upon a tap on the message.
silent Long No 1 Specifies whether the message is silent. Valid values:
  • 1: silent
  • 0: not silent

Java sample code

Click here for information about how to obtain the AccessKey ID and AccessKey secret in the following sample code.

  1. DefaultProfile.addEndpoint("cn-hongkong", "mpaas", "mpaas.cn-hongkong.aliyuncs.com");
  2. // Create a DefaultAcsClient instance and initialize it
  3. DefaultProfile profile = DefaultProfile.getProfile(
  4. "cn-hongkong", // The region ID.
  5. "******", // The AccessKey ID of the RAM account
  6. "*****"); // The AccessKey secret of the RAM account
  7. IAcsClient client = new DefaultAcsClient(profile);
  8. // Create an API request and set parameters
  9. PushSimpleRequest request = new PushSimpleRequest();
  10. request.setAppId("ONEX570DA89211721");
  11. request.setWorkspaceId("test");
  12. request.setTaskName("simpleTest");
  13. request.setTitle("Test");
  14. request.setContent("Test");
  15. request.setDeliveryType(3L);
  16. Map<String,String> extendedParam = new HashMap<String, String>();
  17. extendedParam.put("key1","value1");
  18. request.setExtendedParams(JSON.toJSONString(extendedParam));
  19. request.setExpiredSeconds(300L);
  20. Map<String,String> target = new HashMap<String, String>();
  21. String msgKey = String.valueOf(System.currentTimeMillis());
  22. target.put("user1024",msgKey);
  23. request.setTargetMsgkey(JSON.toJSONString(target));
  24. // Initiate the request and handle the response or exceptions
  25. PushSimpleResponse response;
  26. try {
  27. response = client.getAcsResponse(request);
  28. System.out.println(response.getResultCode());
  29. System.out.println(response.getResultMessage());
  30. } catch (ClientException e) {
  31. e.printStackTrace();
  32. }

Python sample code

  1. from aliyunsdkcore.client import AcsClient
  2. from aliyunsdkmpaas.request.v20190821 import PushSimpleRequest
  3. import json
  4. # Initialize AcsClient instance
  5. client = AcsClient(
  6. "***",
  7. "***",
  8. "cn-hongkong"
  9. );
  10. # Initialize a request and set parameters
  11. request = PushSimpleRequest.PushSimpleRequest()
  12. request.set_endpoint("mpaas.cn-hongkong.aliyuncs.com")
  13. request.set_AppId("ONEX570DA89211721")
  14. request.set_WorkspaceId("test")
  15. request.set_Title( "Python test")
  16. request.set_Content( "Test 2")
  17. request.set_DeliveryType(3)
  18. request.set_TaskName("The test task of Python template push")
  19. request.set_ExpiredSeconds(600)
  20. target = {"user1024":str(time.time())}
  21. request.set_TargetMsgkey(json.dumps(target))
  22. # Print response
  23. response = client.do_action_with_exception(request)
  24. print response

Node.js sample code

  1. const sdk = require('@alicloud/mpaas20190821');
  2. const { default: Client, PushSimpleRequest } = sdk;
  3. // Create a client
  4. const client = new Client({
  5. accessKeyId: '****',
  6. accessKeySecret: '*****',
  7. endpoint: 'mpaas.cn-hongkong.aliyuncs.com',
  8. apiVersion: '2019-08-21'
  9. });
  10. // Initialize the request.
  11. const request = new PushSimpleRequest();
  12. request.appId = "ONEX570DA89211721";
  13. request.workspaceId = "test";
  14. request.title = "Node test";
  15. request.content = "Test";
  16. request.deliveryType = 3;
  17. request.taskName = "Node test task";
  18. request.expiredSeconds=600;
  19. const extendedParam = {
  20. test: 'Custom extension parameter'
  21. };
  22. request.extendedParams = JSON.stringify(extendedParam);
  23. // The value is the ID of the business message. Make sure that the ID is unique.
  24. const target = {
  25. "userid1024": String(new Date().valueOf())
  26. };
  27. request.targetMsgkey = JSON.stringify(target);
  28. // Call the API operation.
  29. try {
  30. client.pushSimple(request).then(res => {
  31. console.log('SUCCESS', res);
  32. }).catch(e => {
  33. console.log('FAIL', e);
  34. });
  35. } catch(e) {
  36. console.log('ERROR', e);
  37. }

PHP sample code

  1. <?php
  2. use AlibabaCloud\Client\AlibabaCloud;
  3. use AlibabaCloud\MPaaS\MPaaS;
  4. AlibabaCloud::accessKeyClient('accessKeyId', 'accessKeySecret')
  5. ->regionId('cn-hongkong')
  6. ->asDefaultClient();
  7. class Demo {
  8. public function run() {
  9. try {
  10. $this->simplePush();
  11. } catch (\Exception $e) {
  12. }
  13. }
  14. public function simplePush() {
  15. $request = MPaaS::v20190821()->pushSimple();
  16. $result = $request->withAppId("ONEX570DA89211721")
  17. ->withWorkspaceId("test")
  18. ->withTitle("PHP test")
  19. ->withContent("Test 3")
  20. ->withDeliveryType(3)
  21. ->withTaskName("PHP test task")
  22. ->withExpiredSeconds(600)
  23. ->withTargetMsgkey(
  24. json_encode(["userid1024" => "".time() ]
  25. ))
  26. // endpoint
  27. ->host("mpaas.cn-hongkong.aliyuncs.com")
  28. // Specify whether to enable the debug mode
  29. ->debug(true)
  30. ->request();
  31. }
  32. }

Template push

Template push refers to pushing one message to a single target ID. The message is created based on a template. Multiple IDs can share the same template.

Note:
  • Before you call this interface, you must create a template in the MPS console. For more information, see Create a template.
  • Before you call this interface, you must introduce the required dependencies. For more information, see SDK preparations.

Parameters

Parameter Type Required Example Description
taskName String Yes templateTest The name of push task
appId String Yes ONEX570DA89211721 mPaaS App ID
workspaceId String Yes test mPaaS workspace
deliveryType Long Yes 3 The type of target ID. Valid values:
  • 1: Android device
  • 2: iOS device
  • 3: User
targetMsgkey String Yes {“user1024”:”1578807462788”} Targets to which the message will be pushed, in the map format:
  • key: the target, which depends on the value of deliveryType.
    If the value of deliveryType is 1, the key is Android device ID.
    If the value of deliveryType is 2, the key is iOS device ID.
    If the value of deliveryType is 3, the key is userid.
  • value: the business ID of the message, which is user-defined and must be unique.
Note that the number of the targets cannot exceed 10.
expiredSeconds Long Yes 300 The validity period of message, in seconds.
templateName String Yes testTemplate The name of template. Create a template in the MPS console.
templateKeyValue String No {“money”:”200”,”name”:”Bob”} The parameters of template, in the map format. The parameters depend on the template specified by templateName. Key refers to the placeholder while value refers to the specific value that is used to replace the placeholder. For example, the content of a template can be Congratulations to #name# for winning RMB #money#. The string between two number signs “#” is the name of the placeholder.
extendedParams String No {“key1”:”value1”} The extension parameters, in the map format.

Java sample code

Click here for information about how to obtain the AccessKey ID and AccessKey secret in the following sample code.

  1. DefaultProfile.addEndpoint("cn-hongkong", "mpaas", "mpaas.cn-hongkong.aliyuncs.com");
  2. // Create a DefaultAcsClient instance and initialize it.
  3. DefaultProfile profile = DefaultProfile.getProfile(
  4. "cn-hongkong", // The region ID.
  5. "******", // The AccessKey ID of the RAM account.
  6. "******"); // The AccessKey secret of the RAM account.
  7. IAcsClient client = new DefaultAcsClient(profile);
  8. // Create an API request and set parameters
  9. PushTemplateRequest request = new PushTemplateRequest();
  10. request.setAppId("ONEX570DA89211721");
  11. request.setWorkspaceId("test");
  12. request.setTemplateName("testTemplate");
  13. // Hello #name#. Congratulations to you for winning RMB #money#.
  14. Map<String,String> templatekv = new HashMap<String, String>();
  15. templatekv.put("name","Bob");
  16. templatekv.put("money","200");
  17. request.setTemplateKeyValue(JSON.toJSONString(templatekv));
  18. request.setExpiredSeconds(600L);
  19. request.setTaskName("templateTest");
  20. request.setDeliveryType(3L);
  21. Map<String,String> target = new HashMap<String, String>();
  22. String msgKey = String.valueOf(System.currentTimeMillis());
  23. target.put("userid1024",msgKey);
  24. request.setTargetMsgkey(JSON.toJSONString(target));
  25. PushTemplateResponse response;
  26. try {
  27. response = client.getAcsResponse(request);
  28. System.out.println(response.getResultCode());
  29. System.out.println(response.getResultMessage());
  30. } catch (ClientException e) {
  31. e.printStackTrace();
  32. }

Python sample code

  1. from aliyunsdkcore.client import AcsClient
  2. from aliyunsdkmpaas.request.v20190821 import PushTemplateRequest
  3. import json
  4. import time
  5. # Initialize AcsClient instance
  6. client = AcsClient(
  7. "AccessKey ID",
  8. "AccessKey Secret",
  9. "cn-hongkong"
  10. );
  11. # Initialize a request and set parameters
  12. request = PushTemplateRequest.PushTemplateRequest()
  13. request.set_endpoint("mpaas.cn-hongkong.aliyuncs.com")
  14. request.set_AppId("ONEX570DA89211721")
  15. request.set_WorkspaceId("test")
  16. request.set_TemplateName("template1024")
  17. templatekv = {"name":"Bob","money":"200"}
  18. request.set_TemplateKeyValue(json.dumps(templatekv))
  19. request.set_DeliveryType(3)
  20. request.set_TaskName("The test task of Python template push")
  21. request.set_ExpiredSeconds(600)
  22. target = {"userid1024":str(time.time())}
  23. request.set_TargetMsgkey(json.dumps(target))
  24. # Print response
  25. response = client.do_action_with_exception(request)
  26. print response

Node.js sample code

  1. const sdk = require('@alicloud/mpaas20190821');
  2. const { default: Client, PushTemplateRequest } = sdk;
  3. // Create a client.
  4. const client = new Client({
  5. accessKeyId: 'accessKeyId',
  6. accessKeySecret: 'accessKeySecret',
  7. endpoint: 'mpaas.cn-hongkong.aliyuncs.com',
  8. apiVersion: '2019-08-21'
  9. });
  10. // Initialize the request.
  11. const request = new PushTemplateRequest();
  12. request.appId = "ONEX570DA89211721";
  13. request.workspaceId = "test";
  14. request.templateName= "template1024";
  15. const templatekv = {
  16. name: 'Bob',
  17. money:'300'
  18. };
  19. request.templateKeyValue = JSON.stringify(templatekv);
  20. request.deliveryType = 3;
  21. request.taskName = "Node test task";
  22. request.expiredSeconds=600;
  23. const extendedParam = {
  24. test: 'Custom extension parameter'
  25. };
  26. request.extendedParams = JSON.stringify(extendedParam);
  27. const target = {
  28. "userid1024": String(new Date().valueOf())
  29. };
  30. request.targetMsgkey = JSON.stringify(target);
  31. // Call the API operation.
  32. try {
  33. client.pushTemplate(request).then(res => {
  34. console.log('SUCCESS', res);
  35. }).catch(e => {
  36. console.log('FAIL', e);
  37. });
  38. } catch(e) {
  39. console.log('ERROR', e);
  40. }

PHP sample code

  1. <?php
  2. use AlibabaCloud\Client\AlibabaCloud;
  3. use AlibabaCloud\MPaaS\MPaaS;
  4. AlibabaCloud::accessKeyClient('accessKeyId', 'accessKeySecret')
  5. ->regionId('cn-hongkong')
  6. ->asDefaultClient();
  7. class Demo {
  8. public function run() {
  9. try {
  10. $this->templatePush();
  11. } catch (\Exception $e) {
  12. }
  13. }
  14. public function templatePush() {
  15. $request = MPaaS::v20190821()->pushTemplate();
  16. $result = $request->host("mpaas.cn-hongkong.aliyuncs.com")
  17. // Specify whether to enable the debug mode.
  18. ->debug(true)
  19. ->withAppId("ONEX570DA89211721")
  20. ->withWorkspaceId("test")
  21. ->withTemplateName("template1024")
  22. ->withTemplateKeyValue(json_encode(["name" => "Bob", "money" => "200"]))
  23. ->withDeliveryType(3)
  24. ->withTaskName("PHP test task")
  25. ->withExpiredSeconds(600)
  26. ->withTargetMsgkey(
  27. json_encode(["userid1024" => "".time() ])
  28. )
  29. ->request();
  30. }
  31. }

Multiple push

You can call this API to push different messages to different target IDs. This API allows you to create a personalized message for a target ID by replacing the template placeholders. Different from template push, multiple push allows you to send messages of different content to different target IDs.

Note:
  • Before you call this interface, you must create a template in the MPS console and make sure that the template contains placeholders. Otherwise, you will not be able to implement personalized message push. Namely, you will not be able to push different messages to different target IDs. For more information, see Create a template.
  • Before you call this interface, you must introduce the required dependencies. For more information, see SDK preparations.

Parameters

Parameter Type Required Example Description
taskName String Yes multipleTest The name of push task
appId String Yes ONEX570DA89211721 mPaaS App ID
workspaceId String Yes test mPaaS workspace
deliveryType Long Yes 3 The type of target ID. Valid values:
  • 1: Android device
  • 2: iOS device
  • 3: User
templateName String Yes testTemplate The name of template.
targetMsgs List Yes The list of TargetMsg objects. The list of push targets. For information about the parameters of each object, see TargetMsg objects.
expiredSeconds Long Yes 300 The validity period of message, in seconds.
extendedParams String No {“key1”:”value1”} The extension parameters, in the map format.

TargetMsg objects

Parameter Type Required Example Description
target String Yes userid1024 The target ID, which depends on the value of the deliveryType parameter.
msgKey String Yes 1578807462788 The ID of business message. The ID is used for message troubleshooting. The ID is user defined and must be unique.
templateKeyValue String No {“money”:”200”,”name”:”Bob”} The parameters of template, in the map format. The parameters depend on the template specified by templateName. Key refers to the placeholder while value refers to the specific value that is used to replace the placeholder. For example, the content of a template can be Congratulations to #name# for winning RMB #money#. The string between two number signs “#” is the name of the placeholder.
extendedParams String No {“key1”:”value1”} The extension parameters, in the map format. Different messages have different extension parameters.

Java sample code

Click here for information about how to obtain the AccessKey ID and AccessKey secret in the following sample code.

  1. DefaultProfile.addEndpoint("cn-hongkong", "mpaas", "mpaas.cn-hongkong.aliyuncs.com");
  2. // Create a DefaultAcsClient instance and initialize it
  3. DefaultProfile profile = DefaultProfile.getProfile(
  4. "cn-hongkong", // The region ID.
  5. "******", // The AccessKey ID of the RAM account
  6. "******"); // The AccessKey secret of the RAM account
  7. IAcsClient client = new DefaultAcsClient(profile);
  8. // Create an API request and set parameters
  9. PushMultipleRequest request = new PushMultipleRequest();
  10. request.setAppId("ONEX570DA89211721");
  11. request.setWorkspaceId("test");
  12. request.setDeliveryType(3L);
  13. request.setTaskName("multipleTest");
  14. request.setTemplateName("testTemplate");
  15. // Hello #name#. Congratulations to you for winning RMB #money#.
  16. List<PushMultipleRequest.TargetMsg> targetMsgs = new ArrayList<PushMultipleRequest.TargetMsg>();
  17. PushMultipleRequest.TargetMsg targetMsg = new PushMultipleRequest.TargetMsg();
  18. targetMsg.setTarget("userid1024");
  19. targetMsg.setMsgKey(String.valueOf(System.currentTimeMillis()));
  20. Map<String, String> templatekv = new HashMap<String, String>();
  21. templatekv.put("name", "Bob");
  22. templatekv.put("money", "200");
  23. targetMsg.setTemplateKeyValue(JSON.toJSONString(templatekv));
  24. // The number of TargetMsg objects can be up to 400
  25. targetMsgs.add(targetMsg);
  26. request.setTargetMsgs(targetMsgs);
  27. request.setExpiredSeconds(600L);
  28. PushMultipleResponse response;
  29. try {
  30. response = client.getAcsResponse(request);
  31. System.out.println(response.getResultCode());
  32. System.out.println(response.getResultMessage());
  33. } catch (ClientException e) {
  34. e.printStackTrace();
  35. }

Python sample code

  1. # -*- coding: utf8 -*-
  2. from aliyunsdkcore.client import AcsClient
  3. from aliyunsdkmpaas.request.v20190821 import PushMultipleRequest
  4. import json
  5. import time
  6. # Initialize AcsClient instance
  7. client = AcsClient(
  8. "AccessKey ID",
  9. "AccessKey Secret",
  10. "cn-hongkong"
  11. );
  12. # Initialize a request and set parameters
  13. request = PushMultipleRequest.PushMultipleRequest()
  14. request.set_endpoint("mpaas.cn-hongkong.aliyuncs.com")
  15. request.set_AppId("ONEX570DA89211721")
  16. request.set_WorkspaceId("test")
  17. request.set_TemplateName("template1024")
  18. request.set_DeliveryType(3)
  19. request.set_TaskName("The test task of Python template push")
  20. request.set_ExpiredSeconds(600)
  21. msgkey = str(time.time())
  22. targets = [
  23. {
  24. "Target": "user1024",
  25. "MsgKey": msgkey,
  26. "TemplateKeyValue": {
  27. "name": "Bob",
  28. "money": "200"
  29. }
  30. }
  31. ]
  32. request.set_TargetMsgs(targets)
  33. # Print response
  34. response = client.do_action_with_exception(request)
  35. print response

Node.js sample code

  1. const sdk = require('@alicloud/mpaas20190821');
  2. const { default: Client, PushMultipleRequest,PushMultipleRequestTargetMsg } = sdk;
  3. // Create a client
  4. const client = new Client({
  5. accessKeyId: 'accessKeyId',
  6. accessKeySecret: 'AccessKey Secret',
  7. endpoint: 'mpaas.cn-hongkong.aliyuncs.com',
  8. apiVersion: '2019-08-21'
  9. });
  10. // Initialize request
  11. const request = new PushMultipleRequest();
  12. request.appId = "ONEX570DA89211721";
  13. request.workspaceId = "test";
  14. request.templateName= "template1024";
  15. const templatekv = {
  16. name: 'Bob',
  17. money:'300'
  18. };
  19. //request.templateKeyValue = JSON.stringify(templatekv);
  20. request.deliveryType = 3;
  21. request.taskName = "Node test task";
  22. request.expiredSeconds=600;
  23. const extendedParam = {
  24. test: 'Custom extension parameter'
  25. };
  26. request.extendedParams = JSON.stringify(extendedParam);
  27. const targetMsgkey = new PushMultipleRequestTargetMsg();
  28. targetMsgkey.target = "userid1024";
  29. targetMsgkey.msgKey = String(new Date().valueOf());
  30. targetMsgkey.templateKeyValue = JSON.stringify(templatekv);;
  31. request.targetMsg = [targetMsgkey];
  32. // Call the API operation.
  33. try {
  34. client.pushMultiple(request).then(res => {
  35. console.log('SUCCESS', res);
  36. }).catch(e => {
  37. console.log('FAIL', e);
  38. });
  39. } catch(e) {
  40. console.log('ERROR', e);
  41. }

PHP sample code

  1. <?php
  2. use AlibabaCloud\Client\AlibabaCloud;
  3. use AlibabaCloud\MPaaS\MPaaS;
  4. AlibabaCloud::accessKeyClient('accessKeyId', 'accessKeySecret')
  5. ->regionId('cn-hongkong')
  6. ->asDefaultClient();
  7. class Demo {
  8. public function run() {
  9. try {
  10. $this->multiPush();
  11. } catch (\Exception $e) {
  12. }
  13. }
  14. public function multiPush() {
  15. $request = MPaaS::v20190821()->pushMultiple();
  16. $result = $request->host("mpaas.cn-hongkong.aliyuncs.com")
  17. // Specify whether to enable the debug mode
  18. ->debug(true)
  19. ->withAppId("ONEX570DA89211721")
  20. ->withWorkspaceId("test")
  21. ->withTemplateName("template1024")
  22. ->withDeliveryType(3)
  23. ->withTaskName("The test task of PHP multiple push")
  24. ->withExpiredSeconds(600)
  25. ->withTargetMsg(
  26. [
  27. [
  28. "Target" => "userid1024",
  29. "MsgKey" => "" . time(),
  30. "TemplateKeyValue" => json_encode([
  31. "name" => "Bob",
  32. "money" => "200",
  33. ])
  34. ]
  35. ]
  36. )
  37. ->request();
  38. }
  39. }

Broadcast push

You can call this interface to push the same message to all devices. The message is created based on a template.

Note:
  • Before you call this interface, you must create a template in the MPS console. For more information, see Create a template.
  • Before you call this interface, you must introduce the required dependencies. For more information, see SDK preparations.

Parameters

Parameter Type Required Example Description
taskName String Yes broadcastTest The name of push task
appId String Yes ONEX570DA89211721 mPaaS App ID
workspaceId String Yes test mPaaS workspace
deliveryType Long Yes 1 The type of target ID. Valid values:
  • 1: Android broadcast
  • 2: iOS broadcast
msgkey String Yes 1578807462788 The ID of business message. The ID is used for message troubleshooting. The ID is user defined and must be unique.
expiredSeconds Long Yes 300 The validity period of message, in seconds.
templateName String Yes broadcastTemplate The name of template. Create a template in the MPS console.
templateKeyValue String No {“content”:”Announcement”} The parameters of template, in the map format. The parameters depend on the template specified by templateName. Key refers to the placeholder while value refers to the specific value that is used to replace the placeholder.
pushStatus Long No 0 This parameter only works for iOS push, which refers to the logon status of users who can receive the message. Valid values:
  • 0: All the bound users can receive the message (default).
  • 1: All the users can receive the message, including unbound users.
unBindPeriod Long No 1 The logout period. This parameter is required when the value of pushStatus is 1. Valid values:
  • 1: users who unbound their accounts within the last 7 days.
  • 2: users who unbound their accounts within the last 15 days.
  • 3: users who unbound their accounts within the last 60 days.
  • 4: users who unbound their accounts permanently.

Java sample code

Click here for information about how to obtain the AccessKey ID and AccessKey secret in the following sample code.

  1. DefaultProfile.addEndpoint("cn-hongkong", "mpaas", "mpaas.cn-hongkong.aliyuncs.com");
  2. // Create a DefaultAcsClient instance and initialize it.
  3. DefaultProfile profile = DefaultProfile.getProfile(
  4. "cn-hongkong", // The region ID.
  5. "******", // The AccessKey ID of the RAM account.
  6. "******"); // The AccessKey secret of the RAM account.
  7. IAcsClient client = new DefaultAcsClient(profile);
  8. PushBroadcastRequest request = new PushBroadcastRequest();
  9. request.setAppId("ONEX570DA89211720");
  10. request.setWorkspaceId("test");
  11. request.setDeliveryType(2L);
  12. request.setMsgkey(String.valueOf(System.currentTimeMillis()));
  13. request.setExpiredSeconds(600L);
  14. request.setTaskName("broadcastTest");
  15. request.setTemplateName("broadcastTemplate");
  16. // This is an announcement: #content#.
  17. Map<String, String> templatekv = new HashMap<String, String>();
  18. templatekv.put("content", "The content of the announcement");
  19. request.setTemplateKeyValue(JSON.toJSONString(templatekv));
  20. PushBroadcastResponse response;
  21. try {
  22. response = client.getAcsResponse(request);
  23. System.out.println(response.getResultCode());
  24. System.out.println(response.getResultMessage());
  25. } catch (ClientException e) {
  26. e.printStackTrace();
  27. }

Python sample code

  1. # -*- coding: utf8 -*-
  2. from aliyunsdkcore.client import AcsClient
  3. from aliyunsdkmpaas.request.v20190821 import PushBroadcastRequest
  4. import json
  5. import time
  6. # Initialize AcsClient instance
  7. client = AcsClient(
  8. "AccessKey ID",
  9. "AccessKey Secret",
  10. "cn-hongkong"
  11. );
  12. # Initialize a request and set parameters
  13. request = PushBroadcastRequest.PushBroadcastRequest()
  14. request.set_endpoint("mpaas.cn-hongkong.aliyuncs.com")
  15. request.set_AppId("ONEX570DA89211720")
  16. request.set_WorkspaceId("test")
  17. request.set_TemplateName("broadcastTemplate")
  18. templatekv = {"content":"This is an announcement"}
  19. request.set_TemplateKeyValue(json.dumps(templatekv))
  20. request.set_DeliveryType(1)
  21. request.set_TaskName("The test task of Python broadcast push")
  22. request.set_ExpiredSeconds(600)
  23. request.set_Msgkey(str(time.time()))
  24. # Print response
  25. response = client.do_action_with_exception(request)
  26. print response

Node.js sample code

  1. const sdk = require('@alicloud/mpaas20190821');
  2. const { default: Client, PushBroadcastRequest } = sdk;
  3. // Create a client.
  4. const client = new Client({
  5. accessKeyId: 'accessKeyId',
  6. accessKeySecret: 'AccessKey Secret',
  7. endpoint: 'mpaas.cn-hongkong.aliyuncs.com',
  8. apiVersion: '2019-08-21'
  9. });
  10. // Initialize the request.
  11. const request = new PushBroadcastRequest();
  12. request.appId = "ONEX570DA89211720";
  13. request.workspaceId = "test";
  14. request.templateName= "broadcastTemplate";
  15. const templatekv = {
  16. content: 'This is an announcement',
  17. };
  18. request.templateKeyValue = JSON.stringify(templatekv);
  19. request.deliveryType = 1;
  20. request.taskName = "Node test task";
  21. request.expiredSeconds=600;
  22. const extendedParam = {
  23. test: 'Custom extension parameter'
  24. };
  25. request.extendedParams = JSON.stringify(extendedParam);
  26. request.msgkey = String(new Date().valueOf())
  27. // Call the API operation.
  28. try {
  29. client.pushBroadcast(request).then(res => {
  30. console.log('SUCCESS', res);
  31. }).catch(e => {
  32. console.log('FAIL', e);
  33. });
  34. } catch(e) {
  35. console.log('ERROR', e);
  36. }

PHP sample code

  1. <?php
  2. use AlibabaCloud\Client\AlibabaCloud;
  3. use AlibabaCloud\MPaaS\MPaaS;
  4. AlibabaCloud::accessKeyClient('accessKeyId', 'accessKeySecret')
  5. ->regionId('cn-hongkong')
  6. ->asDefaultClient();
  7. class Demo {
  8. public function run() {
  9. try {
  10. $this->broadcastPush();
  11. } catch (\Exception $e) {
  12. }
  13. }
  14. public function broadcastPush(){
  15. $request = MPaaS::v20190821()->pushBroadcast();
  16. $result = $request->host("mpaas.cn-hongkong.aliyuncs.com")
  17. // Specify whether to enable the debug mode.
  18. ->debug(true)
  19. ->withAppId("ONEX570DA89211720")
  20. ->withWorkspaceId("test")
  21. ->withTemplateName("broadcastTemplate")
  22. ->withTemplateKeyValue(
  23. json_encode(["content" => "This is an announcement"])
  24. )
  25. ->withDeliveryType(1)
  26. ->withTaskName("The test task of PHP broadcast push")
  27. ->withExpiredSeconds(600)
  28. ->withMsgkey("". time())
  29. ->request();
  30. }
  31. }

Extension parameters

Extension parameters are passed to the client with message body. You can define or process these parameters.

Extension parameters include the following three types:

  • System extension parameters

    These extension parameters are occupied by the system. Do not modify the values of these parameters. System extension parameters include:

    • notifyType
    • action
    • silent
    • pushType
    • templateCode
    • channel
    • taskId
  • System extension parameters with some significance

    Extension parameters of this type are occupied by the system. Each parameter has a specific meaning. You can set the values of these extension parameters. The following table describes the extension parameters with specific meanings:

    Key Description
    sound The custom ringtone of the message. The value of this parameter is the path of the ringtone. This parameter only applies to Xiaomi phones and iPhones.
    badge The badge of the App icon. Its value is a specific number. This extension parameter will be passed to the client together with the message body.
    • For Android devices, you need to implement the badge logic by yourself.
    • For iOS devices, the system automatically implements the badge logic. When a message is pushed to the target mobile phone, the number that you specified in value appears in the badge of the App icon.
    mutable-content Custom push ID of Apple Push Notification service (APNs). A push notification carrying this parameter indicates the support of iOS 10 UNNotificationServiceExtension. If the push notification not carrying this parameter indicates a common push. Set the value to 1.
    badge_add_num Number of added push badges for Huawei push channel.
    badge_class Activity class corresponding to the desktop icon for Huawei push channel
    big_text Big text style. This parameter has a fixed value “1”. Any other value is invalid. This parameter is only valid for Xiaomi and Huawei devices.
  • User-defined extension parameters

    All the parameters other than the preceding system extension parameters are user-defined extension parameters. User-defined extension parameters are passed to the client together with a message body. You can define and process these parameters.

Result codes

Result code Message Description
100 SUCCESS Succeeded
-1 SIGNATURE_MISMATCH Signature mismatched.
3001 NEED_DELIVERYTOKEN deliveryToken is empty.
3002 NEED_FILE The file is empty.
3003 NEED_APPID_WORKSPACEID The App ID or workspace is empty.
3007 APPID_WRONG Invalid App ID or workspace.
3008 OS_TYPE_NOT_SUPPORTED Push platform not supported.
3009 DELIVERY_TYPE_NOT_SUPPORTED deliveryType not supported.
3012 NEED_USERID UserId is empty.
3019 TASKNAME_NULL Task name is empty.
3020 EXPIREDSECONDS_WRONG Ilegal message timeout length.
3021 TOKEN_OR_USERID_NULL Target is empty.
3022 TEMPLATE_NOT_EXIST Template doesn’t exist.
3023 TEMPLATEKV_NOT_ENOUGH Template parameter mismatched.
3024 PAYLOAD_NOT_ENOUGH Title or content is empty.
3025 NEED_TEMPLATE Template is empty.
3026 EXPIREDTIME_TOO_LONG The validity period of message is too long.
3028 INVALID_PARAM Ilegal parameter.
3029 SINGLE_PUSH_TARGET_TOO_MUCH Too many targets.
3030 BROADCAST_ONLY_SUPPORT_BY_DEVICE Only broadcast push by device is supported.
3031 REQUEST_SHOULD_BE_UTF8 The request body must be UTF-8 encoded.
3032 REST_API_SWITCH_NOT_OPEN The push API has been closed.
3033 UNKNOWN_REST_SIGN_TYPE Signature type not supported.
3035 EXTEND_PARAM_TO_MUCH Too many extension parameters. A maximum of 20 extension parameters are allowed.
3036 TEMPLATE_ALREADY_EXIST The template already exists.
3037 TEMPLATE_NAME_NULL Template name is empty.
3038 TEMPLATE_NAME_INVALID Ilegal template name.
3039 TEMPLATE_CONTENT_INVALID Ilegal template content.
3040 TEMPLATE_TITLE_INVALID Ilegal template title.
3041 TEMPLATE_DESC_INFO_INVALID Ilegal template description.
3042 TEMPLATE_URI_INVALID Ilegal template URI.
3043 SINGLE_PUSH_CONTENT_TOO_LONG Message body is too long.
3044 INVALID_EXTEND_PARAM Ilegal extension parameter.
3049 MULTIPLE_INNER_EXTEND_PARAM_TO_MUCH The number of internal extension parameters for multiple push cannot exceed 10.
3050 MSG_PAYLOAD_TOO_LONG Message body is too long.
3051 BROADCAST_ALL_USER_NEED_UNBIND_PERIOD Unbinding parameters are required for the broadcast push targeting at all users (including both login and logout users).
3052 BROADCAST_ALL_USER_UNBIND_PERIOD_INVALID Illegal unbinding parameters for broadcast push.
3053 BROADCAST_ALL_USER_NOT_SUPPORT_SELFCHANNEL_ANDROID MPS self-built push channel doesn’t supports the broadcast push targeting at all users (including both login and logout users).
3054 DELIVERYTOKEN_INVALID Illegal MPS self-built channel token.
3055 MULTIPLE_TARGET_NUMBER_TOO_MUCH The number of push targets exceeds the threshold.
3056 TEMPLATE_NUM_TOO_MUCH The number of message templates exceeds the threshold.
3057 ANDROID_CHANNEL_PARAM_INVALID Invalid androidChannel.
3058 BADGE_ADD_NUM_INVALID Invalid badge_add_num.
3059 BADGE_ADD_NUM_NEED_BADGE_CLASS The parameter badge_class is required for badge_add_num.
9000 SYSTEM_ERROR System error.