AliyunPush.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Aliyun.Acs.Push.Model.V20160801;
  6. using Aliyun.Acs.Push.Transform.V20160801;
  7. using Aliyun.Acs.Core;
  8. using Aliyun.Acs.Core.Exceptions;
  9. using Aliyun.Acs.Core.Profile;
  10. //using MySystem.BsModels;
  11. using Library;
  12. namespace MySystem
  13. {
  14. public class AliyunPush
  15. {
  16. private string AccessKeyID = "LTAImYRe25JQ5nr8";
  17. private string AccessKeySecret = "znOWxVGLnKl8KHlPSIKruYC3C1HYIy";
  18. public void PushMessageToAndroid()
  19. {
  20. IClientProfile clientProfile = DefaultProfile.GetProfile("cn-hangzhou", AccessKeyID, AccessKeySecret);
  21. DefaultAcsClient client = new DefaultAcsClient(clientProfile);
  22. PushMessageToAndroidRequest request = new PushMessageToAndroidRequest();
  23. request.AppKey = 24566887;
  24. request.Target = "ALL";//推送目标: DEVICE:按设备推送 ALIAS : 按别名推送 ACCOUNT:按帐号推送 TAG:按标签推送; ALL: 广播推送
  25. request.TargetValue = "All";////根据Target来设定,如Target=DEVICE, 则对应的值为 设备id1,设备id2. 多个值使用逗号分隔.(帐号与设备一次最多100)
  26. request.Title = "android title";
  27. request.Body = "android body";
  28. try
  29. {
  30. PushMessageToAndroidResponse response = client.GetAcsResponse(request);
  31. Console.WriteLine("RequestId:" + response.RequestId);
  32. Console.WriteLine("ResponseId:" + response.MessageId);
  33. Console.ReadLine();
  34. }
  35. catch (ServerException e)
  36. {
  37. Console.WriteLine(e.ErrorCode);
  38. Console.WriteLine(e.ErrorMessage);
  39. Console.ReadLine();
  40. }
  41. catch (ClientException e)
  42. {
  43. Console.WriteLine(e.ErrorCode);
  44. Console.WriteLine(e.ErrorMessage);
  45. Console.ReadLine();
  46. }
  47. }
  48. public void PushMessageToiOS()
  49. {
  50. IClientProfile clientProfile = DefaultProfile.GetProfile("cn-hangzhou", AccessKeyID, AccessKeySecret);
  51. DefaultAcsClient client = new DefaultAcsClient(clientProfile);
  52. PushMessageToiOSRequest request = new PushMessageToiOSRequest();
  53. request.AppKey = 24566887;
  54. request.Target = "ALL";//推送目标: DEVICE:按设备推送 ALIAS : 按别名推送 ACCOUNT:按帐号推送 TAG:按标签推送; ALL: 广播推送
  55. request.TargetValue = "All";//根据Target来设定,如Target=DEVICE, 则对应的值为 设备id1,设备id2. 多个值使用逗号分隔.(帐号与设备一次最多100个)
  56. request.Title = "iOS Title";
  57. request.Body = "iOS Body";
  58. try
  59. {
  60. PushMessageToiOSResponse response = client.GetAcsResponse(request);
  61. Console.WriteLine("RequestId:" + response.RequestId);
  62. Console.WriteLine("ResponseId:" + response.MessageId);
  63. Console.ReadLine();
  64. }
  65. catch (ServerException e)
  66. {
  67. Console.WriteLine(e.ErrorCode);
  68. Console.WriteLine(e.ErrorMessage);
  69. Console.ReadLine();
  70. }
  71. catch (ClientException e)
  72. {
  73. Console.WriteLine(e.ErrorCode);
  74. Console.WriteLine(e.ErrorMessage);
  75. Console.ReadLine();
  76. }
  77. }
  78. public void PushNoticeToAndroid()
  79. {
  80. IClientProfile clientProfile = DefaultProfile.GetProfile("cn-hangzhou", AccessKeyID, AccessKeySecret);
  81. DefaultAcsClient client = new DefaultAcsClient(clientProfile);
  82. PushNoticeToAndroidRequest request = new PushNoticeToAndroidRequest();
  83. request.AppKey = 24566887;
  84. request.Target = "ALL";//推送目标: DEVICE:按设备推送 ALIAS : 按别名推送 ACCOUNT:按帐号推送 TAG:按标签推送; ALL: 广播推送
  85. request.TargetValue = "all";//根据Target来设定,如Target=DEVICE, 则对应的值为 设备id1,设备id2. 多个值使用逗号分隔.(帐号与设备一次最多100个)
  86. request.Title = "android notice title";
  87. request.Body = "android notice body";
  88. request.ExtParameters = "{\"Url\":\"http://www.yunmuit.com/\"}";
  89. try
  90. {
  91. PushNoticeToAndroidResponse response = client.GetAcsResponse(request);
  92. Console.WriteLine("RequestId:" + response.RequestId);
  93. Console.WriteLine("ResponseId:" + response.MessageId);
  94. Console.ReadLine();
  95. }
  96. catch (ServerException e)
  97. {
  98. Console.WriteLine(e.ErrorCode);
  99. Console.WriteLine(e.ErrorMessage);
  100. Console.ReadLine();
  101. }
  102. catch (ClientException e)
  103. {
  104. Console.WriteLine(e.ErrorCode);
  105. Console.WriteLine(e.ErrorMessage);
  106. Console.ReadLine();
  107. }
  108. }
  109. public void PushNoticeToiOS()
  110. {
  111. IClientProfile clientProfile = DefaultProfile.GetProfile("cn-hangzhou", AccessKeyID, AccessKeySecret);
  112. DefaultAcsClient client = new DefaultAcsClient(clientProfile);
  113. PushNoticeToiOSRequest request = new PushNoticeToiOSRequest();
  114. request.AppKey = 24566887;
  115. // iOS的通知是通过APNS中心来发送的,需要填写对应的环境信息. DEV:表示开发环境, PRODUCT: 表示生产环境
  116. request.ApnsEnv = "DEV";
  117. request.Target = "ALL";
  118. request.TargetValue = "ALL";
  119. request.Title = "test";
  120. request.Body = "testg";
  121. request.ExtParameters = "{\"Url\":\"http://www.yunmuit.com/\"}";
  122. try
  123. {
  124. PushNoticeToiOSResponse response = client.GetAcsResponse(request);
  125. Console.WriteLine("RequestId:" + response.RequestId);
  126. Console.WriteLine("ResponseId:" + response.MessageId);
  127. Console.ReadLine();
  128. }
  129. catch (ServerException e)
  130. {
  131. Console.WriteLine(e.ErrorCode);
  132. Console.WriteLine(e.ErrorMessage);
  133. Console.ReadLine();
  134. }
  135. catch (ClientException e)
  136. {
  137. Console.WriteLine(e.ErrorCode);
  138. Console.WriteLine(e.ErrorMessage);
  139. Console.ReadLine();
  140. }
  141. }
  142. public string Push(string Title, string Body, string Url, string PushType = "NOTICE", string DeviceType = "ALL", string DeviceList = "", bool Silent = false, String pushTime = "")
  143. {
  144. IClientProfile clientProfile = DefaultProfile.GetProfile("cn-hangzhou", AccessKeyID, AccessKeySecret);
  145. DefaultAcsClient client = new DefaultAcsClient(clientProfile);
  146. PushRequest request = new PushRequest();
  147. // 推送目标
  148. if (DeviceType == "ANDROID")
  149. {
  150. request.AppKey = 27532904;
  151. }
  152. else if (DeviceType == "iOS")
  153. {
  154. request.AppKey = 27536906;
  155. }
  156. if (string.IsNullOrEmpty(DeviceList))
  157. {
  158. //推送目标: DEVICE:按设备推送 ALIAS : 按别名推送 ACCOUNT:按帐号推送 TAG:按标签推送; ALL: 广播推送
  159. request.Target = "ALL";
  160. //根据Target来设定,如Target=DEVICE, 则对应的值为 设备id1,设备id2. 多个值使用逗号分隔.(帐号与设备有一次最多100个的限制)
  161. request.TargetValue = "All";
  162. }
  163. else
  164. {
  165. request.Target = "DEVICE";
  166. request.TargetValue = DeviceList;
  167. }
  168. //消息类型 MESSAGE NOTICE
  169. request.PushType = PushType;
  170. //设备类型 ANDROID iOS ALL.
  171. request.DeviceType = DeviceType;
  172. // 消息的标题
  173. request.Title = Title;
  174. // 消息的内容
  175. request.Body = Body;
  176. // 推送配置: iOS
  177. // iOS应用图标右上角角标
  178. //request.IOSBadge = 50;
  179. //开启静默通知
  180. request.IOSSilentNotification = Silent;
  181. // iOS通知声音
  182. if (Silent)
  183. {
  184. request.IOSMusic = "";
  185. }
  186. else
  187. {
  188. request.IOSMusic = "default";
  189. }
  190. //iOS10通知副标题的内容
  191. request.IOSSubtitle = "";
  192. //指定iOS10通知Category
  193. request.IOSNotificationCategory = "iOS10 Notification Category";
  194. //是否允许扩展iOS通知内容
  195. request.IOSMutableContent = true;
  196. //iOS的通知是通过APNs中心来发送的,需要填写对应的环境信息。"DEV" : 表示开发环境 "PRODUCT" : 表示生产环境
  197. request.IOSApnsEnv = "PRODUCT";
  198. //消息推送时设备不在线(既与移动推送的服务端的长连接通道不通),则这条推送会做为通知,通过苹果的APNs通道送达一次。注意:离线消息转通知仅适用于生产环境
  199. request.IOSRemind = true;
  200. //iOS消息转通知时使用的iOS通知内容,仅当iOSApnsEnv=PRODUCT && iOSRemind为true时有效
  201. request.IOSRemindBody = Body;
  202. //自定义的kv结构,开发者扩展用 针对iOS设备
  203. string PageId = Url;
  204. string Params = "";
  205. if (Url.Contains("?"))
  206. {
  207. Params = Url.Split("?")[1];
  208. PageId = Url.Split("?")[0];
  209. }
  210. request.IOSExtParameters = "{\"Url\":\"" + PageId + "\",\"Params\":\"" + Params + "\",\"SoundType\":\"0\"}";
  211. // 推送配置: Android
  212. // 通知的提醒方式 "VIBRATE" : 震动 "SOUND" : 声音 "BOTH" : 声音和震动 NONE : 静音
  213. if (Silent)
  214. {
  215. request.AndroidNotifyType = "NONE";
  216. }
  217. else
  218. {
  219. request.AndroidNotifyType = "SOUND";
  220. }
  221. //通知栏自定义样式0-100
  222. request.AndroidNotificationBarType = 3;
  223. //通知栏显示优先级
  224. request.AndroidNotificationBarPriority = 0;
  225. //点击通知后动作 "APPLICATION" : 打开应用 "ACTIVITY" : 打开AndroidActivity "URL" : 打开URL "NONE" : 无跳转
  226. request.AndroidOpenType = "APPLICATION";
  227. //Android收到推送后打开对应的url,仅当AndroidOpenType="URL"有效
  228. //request.AndroidOpenUrl = "http://www.baidu.com";
  229. //设定通知打开的activity,仅当AndroidOpenType="Activity"有效
  230. //request.AndroidActivity = "com.alibaba.push2.demo.XiaoMiPushActivity";
  231. request.AndroidRemind = true;
  232. request.AndroidPopupActivity = "com.yunmuapp.yunmuapp.activity.WaitActivity";
  233. request.AndroidPopupTitle = "创业帮";
  234. request.AndroidPopupBody = Body;
  235. request.AndroidNotificationChannel = "1";
  236. //Android通知音乐
  237. request.AndroidMusic = "default";
  238. //设置该参数后启动小米托管弹窗功能,此处指定通知点击后跳转的Activity(托管弹窗的前提条件:1. 继承小米辅助通道;2. storeOffline设为true)
  239. request.AndroidXiaoMiActivity = "com.yunmuapp.yunmuapp.activity.WaitActivity";
  240. //小米弹窗标题
  241. request.AndroidXiaoMiNotifyTitle = "创业帮";
  242. //小米弹窗内容
  243. request.AndroidXiaoMiNotifyBody = Body;
  244. // 设定android类型设备通知的扩展属性
  245. request.AndroidExtParameters = "{\"Url\":\"" + PageId + "\",\"Params\":\"" + Params + "\",\"SoundType\":\"0\"}";
  246. // 推送控制
  247. //String pushTime = DateTime.UtcNow.AddSeconds(3).ToString("yyyy-MM-ddTHH\\:mm\\:ssZ");
  248. if (!string.IsNullOrEmpty(pushTime))
  249. {
  250. TimeSpan ts = DateTime.Parse(pushTime) - DateTime.Now;
  251. request.PushTime = DateTime.UtcNow.Add(ts).ToString("yyyy-MM-ddTHH\\:mm\\:ssZ");//延迟3秒发送
  252. }
  253. String expireTime = DateTime.UtcNow.AddDays(1).ToString("yyyy-MM-ddTHH\\:mm\\:ssZ");
  254. request.ExpireTime = expireTime;//设置过期时间为2天
  255. request.StoreOffline = true;
  256. string result = "";
  257. try
  258. {
  259. PushResponse response = client.GetAcsResponse(request);
  260. Console.WriteLine("RequestId:" + response.RequestId);
  261. Console.WriteLine("ResponseId:" + response.MessageId);
  262. Console.ReadLine();
  263. result = "1|" + response.MessageId;
  264. }
  265. catch (ServerException e)
  266. {
  267. Console.WriteLine(e.ErrorCode);
  268. Console.WriteLine(e.ErrorMessage);
  269. Console.ReadLine();
  270. function.WriteLog(e.ErrorCode, "阿里云推送异常");
  271. function.WriteLog(e.ErrorMessage, "阿里云推送异常");
  272. }
  273. catch (ClientException e)
  274. {
  275. Console.WriteLine(e.ErrorCode);
  276. Console.WriteLine(e.ErrorMessage);
  277. Console.ReadLine();
  278. function.WriteLog(e.ErrorCode, "阿里云推送异常");
  279. function.WriteLog(e.ErrorMessage, "阿里云推送异常");
  280. }
  281. return result;
  282. }
  283. public void CancelPush(string msgid)
  284. {
  285. IClientProfile clientProfile = DefaultProfile.GetProfile("cn-hangzhou", AccessKeyID, AccessKeySecret);
  286. DefaultAcsClient client = new DefaultAcsClient(clientProfile);
  287. CancelPushRequest request = new CancelPushRequest();
  288. request.AppKey = 24566887;
  289. request.MessageId = msgid;
  290. try
  291. {
  292. CancelPushResponse response = client.GetAcsResponse(request);
  293. Console.WriteLine("RequestId:" + response.RequestId);
  294. Console.ReadLine();
  295. }
  296. catch (ServerException e)
  297. {
  298. Console.WriteLine(e.ErrorCode);
  299. Console.WriteLine(e.ErrorMessage);
  300. Console.ReadLine();
  301. }
  302. catch (ClientException e)
  303. {
  304. Console.WriteLine(e.ErrorCode);
  305. Console.WriteLine(e.ErrorMessage);
  306. Console.ReadLine();
  307. }
  308. }
  309. }
  310. }