AliyunPush.cs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Library;
  6. using Tea;
  7. using Tea.Utils;
  8. namespace MySystem
  9. {
  10. public class AliyunPush
  11. {
  12. private static string AccessKeyID = "LTAI5tGH621tQ3FVd3jXYUEx";
  13. private static string AccessKeySecret = "SDjL74Lk9FUp8VPKl4YYcsbY7MIkRr";
  14. /**
  15. * 使用AK&SK初始化账号Client
  16. * @return Client
  17. * @throws Exception
  18. */
  19. public static AlibabaCloud.SDK.Push20160801.Client CreateClient()
  20. {
  21. // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
  22. // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378671.html。
  23. AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
  24. {
  25. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
  26. AccessKeyId = AccessKeyID, //Environment.GetEnvironmentVariable(AccessKeyID),
  27. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
  28. AccessKeySecret = AccessKeySecret, //Environment.GetEnvironmentVariable(AccessKeySecret),
  29. };
  30. // Endpoint 请参考 https://api.aliyun.com/product/Push
  31. config.Endpoint = "cloudpush.aliyuncs.com";
  32. return new AlibabaCloud.SDK.Push20160801.Client(config);
  33. }
  34. public static string Push(string Account, string Title, string Body)
  35. {
  36. AlibabaCloud.SDK.Push20160801.Client client = CreateClient();
  37. AlibabaCloud.SDK.Push20160801.Models.PushRequest request = new AlibabaCloud.SDK.Push20160801.Models.PushRequest
  38. {
  39. AppKey = 333673923,
  40. PushType = "NOTICE",
  41. DeviceType = "ANDROID",
  42. StoreOffline = false,
  43. IOSRemind = true,
  44. AndroidRemind = true,
  45. Target = "ACCOUNT",
  46. TargetValue = Account,
  47. Title = Title,
  48. Body = Body,
  49. IOSRemindBody = Body,
  50. AndroidPopupTitle = Title,
  51. AndroidPopupBody = Body,
  52. AndroidNotifyType = "BOTH",
  53. AndroidOpenType = "NONE",
  54. AndroidExtParameters = "{\"Url\":\"user-payment-list\",\"Jump\":\"1\"}",
  55. AndroidNotificationChannel = "1",
  56. };
  57. AlibabaCloud.SDK.Push20160801.Models.PushResponse response = client.Push(request);
  58. return AlibabaCloud.TeaUtil.Common.ToJSONString(response.Body.ToMap());
  59. }
  60. public static string PushForIos(string Account, string Title, string Body)
  61. {
  62. AlibabaCloud.SDK.Push20160801.Client client = CreateClient();
  63. AlibabaCloud.SDK.Push20160801.Models.PushRequest request = new AlibabaCloud.SDK.Push20160801.Models.PushRequest
  64. {
  65. AppKey = 333673916,
  66. PushType = "NOTICE",
  67. DeviceType = "iOS",
  68. StoreOffline = false,
  69. IOSRemind = true,
  70. Target = "ACCOUNT",
  71. TargetValue = Account,
  72. Title = Title,
  73. Body = Body,
  74. IOSRemindBody = Body,
  75. IOSMutableContent = true,
  76. IOSMusic = "pushSound.mp3",
  77. IOSApnsEnv = Library.ConfigurationManager.EnvironmentFlag == 1 ? "DEV" : "PRODUCT",
  78. IOSExtParameters = "{\"Url\":\"user-payment-list\"}",
  79. };
  80. AlibabaCloud.SDK.Push20160801.Models.PushResponse response = client.Push(request);
  81. return AlibabaCloud.TeaUtil.Common.ToJSONString(response.Body.ToMap());
  82. }
  83. }
  84. }