全部产品
云市场

sdk手册

更新时间:2018-05-19 10:57:35

1. 获取AccessKeyId和AccessKeySecret

为了保证云服务的安全,您需要创建一个能访问移动推送资源的 RAM 子账号,获取该子账号的 AK 密钥,并使用这个 RAM 子账号调用移动推送 OpenAPI。

以下是获取 RAM 子账号 AK 密钥的操作步骤:

2. 获取AppKey

  • 前往Emas控制台获取,如果没有APP,您可以在控制台创建。

3. 获取SDK

您可以选择任意一种SDK。

特别提示: Java SDK可直接使用以下Maven引用

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>aliyun-java-sdk-push</artifactId>
  4. <version>3.10.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.aliyun</groupId>
  8. <artifactId>aliyun-java-sdk-core</artifactId>
  9. <version>3.2.8</version>
  10. </dependency>

4. 参考Demo

5. 示例代码

5.1 Java

  1. import com.aliyuncs.profile.DefaultProfile;
  2. import com.aliyuncs.DefaultAcsClient;
  3. import com.aliyuncs.IAcsClient;
  4. import com.aliyuncs.exceptions.ClientException;
  5. import com.aliyuncs.exceptions.ServerException;
  6. import com.aliyuncs.push.model.v20160801.PushRequest;
  7. import com.aliyuncs.push.model.v20160801.PushResponse;
  8. public class Main {
  9. public static void main(String[] args) {
  10. DefaultProfile profile = DefaultProfile.getProfile(
  11. "cn-hangzhou",
  12. "<your-access-key-id>", // The AccessKey ID of the RAM account
  13. "<your-access-key-secret>"); // The AccessKey Secret of the RAM account
  14. IAcsClient client = new DefaultAcsClient(profile);
  15. PushRequest request = new PushRequest();
  16. request.setAppKey( < Your AppKey >); //Your AppKey
  17. request.setPushType("NOTICE");
  18. request.setDeviceType("ANDROID");
  19. request.setTarget("DEVICE");
  20. request.setTargetValue("<your device id>");
  21. request.setTitle("Push Title");
  22. request.setBody("Push Body");
  23. try {
  24. PushResponse response = client.getAcsResponse(request);
  25. System.out.println(response.getRequestId());
  26. System.out.println(response.getMessageId());
  27. } catch (ServerException e) {
  28. e.printStackTrace();
  29. } catch (ClientException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }

5.2 .NET

  1. using Aliyun.Acs.Core;
  2. using Aliyun.Acs.Core.Profile;
  3. using Aliyun.Acs.Core.Exceptions;
  4. using Aliyun.Acs.Push.Model.V20160801;
  5. class TestProgram
  6. {
  7. static void Main(string[] args)
  8. {
  9. // Create a client used for initiating a request
  10. IClientProfile profile = DefaultProfile.GetProfile(
  11. "<your-region-id>",
  12. "<your-access-key-id>",
  13. "<your-access-key-secret>");
  14. DefaultAcsClient client = new DefaultAcsClient(profile);
  15. PushRequest request = new PushRequest();
  16. request.PushType = "NOTICE";
  17. request.DeviceType = "ALL";
  18. request.AppKey = < your appKey >;
  19. request.Target = "DEVICE";
  20. request.TargetValue = "<your device id>";
  21. request.Title = "Push Title";
  22. request.Body = "Push body";
  23. try
  24. {
  25. // Initiate the request and get the response
  26. PushResponse response = client.GetAcsResponse(request);
  27. System.Console.WriteLine(response.RequestId);
  28. System.Console.WriteLine(response.MessageId);
  29. }
  30. catch (ServerException ex)
  31. {
  32. System.Console.WriteLine(ex.ToString());
  33. }
  34. catch (ClientException ex)
  35. {
  36. System.Console.WriteLine(ex.ToString());
  37. }
  38. }
  39. }

5.3 PHP

  1. include_once '../aliyun-php-sdk-core/Config.php';
  2. include 'Push/Request/V20160801/PushRequest.php';
  3. use \Push\Request\V20160801 as Push;
  4. $accessKeyId = "your accessKeyId";
  5. $accessKeySecret = "your accessKeySecret";
  6. $appKey = "your appKey";
  7. $deviceId = "your deviceId";
  8. $iClientProfile = DefaultProfile::getProfile("cn-hangzhou", $accessKeyId, $accessKeySecret);
  9. $client = new DefaultAcsClient($iClientProfile);
  10. $request = new Push\PushRequest();
  11. $request->setAppKey($appKey);
  12. $request->setTarget("DEVICE");
  13. $request->setTargetValue("ALL");
  14. $request->setDeviceType("ANDROID"); //设备类型 ANDROID iOS ALL.
  15. $request->setPushType("MESSAGE"); //消息类型 MESSAGE NOTICE
  16. $request->setTitle("php title"); // 消息的标题
  17. $request->setBody("php body"); // 消息的内容
  18. $response = $client->getAcsResponse($request);
  19. print_r($response);

5.4 python

  1. import properties
  2. from aliyunsdkpush.request.v20160801 import PushRequest
  3. from aliyunsdkcore import client
  4. from datetime import *
  5. import time
  6. clt = client.AcsClient(properties.accessKeyId, properties.accessKeySecret, properties.regionId)
  7. request = PushRequest.PushRequest()
  8. request.set_AppKey(properties.appKey)
  9. request.set_Target('ALL')
  10. request.set_TargetValue('ALL')
  11. request.set_DeviceType("ANDROID")
  12. request.set_PushType("NOTICE")
  13. request.set_Title("Open Api Push Title")
  14. request.set_Body("Body from ali push open api2.0 : Push")
  15. result = clt.do_action(request)
  16. print result