Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

141 строка
6.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using com.igetui.api.openservice;
  8. using com.igetui.api.openservice.igetui;
  9. using com.igetui.api.openservice.igetui.template;
  10. using com.igetui.api.openservice.payload;
  11. using Newtonsoft.Json;
  12. namespace Quanjiang.DigitalScholl.PushNotifications.GeTui
  13. {
  14. public class PushMessage
  15. {
  16. //参数设置
  17. //http的域名
  18. private static readonly string Host = ConfigManager.AppSettings["HOST"].Value;
  19. //定义常量, appId、Appkey、Mastersecret
  20. private static readonly string Appid = ConfigManager.AppSettings["APPID"].Value;
  21. private static readonly string Appkey = ConfigManager.AppSettings["APPKEY"].Value;
  22. private static readonly string Mastersecret = ConfigManager.AppSettings["MASTERSECRET"].Value;
  23. /// <summary>
  24. /// 对单个用户推送消息
  25. /// </summary>
  26. /// <param name="clientId">个推业务层中的对外用户标识,用于标识客户端身份,由第三方客户端获取并保存到第三方服务端,是个推SDK的唯一识别号,简称CID。</param>
  27. /// <param name="notifyTitle">通知标题</param>
  28. /// <param name="notifyContent">通知内容</param>
  29. /// <param name="newsId">新闻ID</param>
  30. public static void PushMessageToSingle(string clientId, string notifyTitle, string notifyContent, string newsId)
  31. {
  32. var push = new IGtPush(Host, Appkey, Mastersecret);
  33. //消息模版:TransmissionTemplate:透传模板
  34. var template = GeTuiTransmissionTemplate(notifyTitle, notifyContent, newsId);
  35. // 单推消息模型
  36. var message = new SingleMessage();
  37. message.IsOffline = true; // 用户当前不在线时,是否离线存储,可选
  38. message.OfflineExpireTime = 1000 * 3600 * 12; // 离线有效时间,单位为毫秒,可选
  39. message.Data = template;
  40. Target target = new Target();
  41. target.appId = Appid;
  42. target.clientId = clientId;
  43. try
  44. {
  45. push.pushMessageToSingle(message, target);
  46. }
  47. catch (RequestException e)
  48. {
  49. String requestId = e.RequestId;
  50. //发送失败后的重发
  51. push.pushMessageToSingle(message, target, requestId);
  52. }
  53. }
  54. /// <summary>
  55. /// 对指定列表用户推送消息
  56. /// </summary>
  57. /// <param name="clientIdList">用户列表</param>
  58. /// <param name="notifyTitle">通知标题</param>
  59. /// <param name="notifyContent">通知内容</param>
  60. /// <param name="newsId">新闻ID</param>
  61. public static void PushMessageToList(List<string> clientIdList, string notifyTitle, string notifyContent, string newsId)
  62. {
  63. // 推送主类(方式1,不可与方式2共存)
  64. var push = new IGtPush(Host, Appkey, Mastersecret);
  65. // 推送主类(方式2,不可与方式1共存)此方式可通过获取服务端地址列表判断最快域名后进行消息推送,每10分钟检查一次最快域名
  66. //IGtPush push = new IGtPush("",Appkey,Mastersecret);
  67. var message = new ListMessage();
  68. var template = GeTuiTransmissionTemplate(notifyTitle, notifyContent, newsId);
  69. // 用户当前不在线时,是否离线存储,可选
  70. message.IsOffline = true;
  71. // 离线有效时间,单位为毫秒,可选
  72. message.OfflineExpireTime = 1000 * 3600 * 12;
  73. message.Data = template;
  74. message.PushNetWorkType = 0; //判断是否客户端是否wifi环境下推送,1为在WIFI环境下,0为不限制网络环境。
  75. //设置接收者
  76. var targetList = new List<Target>();
  77. foreach (var cid in clientIdList)
  78. {
  79. var msgTarget = new Target
  80. {
  81. appId = Appid,
  82. clientId = cid
  83. };
  84. targetList.Add(msgTarget);
  85. }
  86. var contentId = push.getContentId(message);
  87. push.pushMessageToList(contentId, targetList);
  88. }
  89. /*
  90. *
  91. * 所有推送接口均支持四个消息模板,依次为透传模板,通知透传模板,通知链接模板,通知弹框下载模板
  92. * 注:IOS离线推送需通过APN进行转发,需填写pushInfo字段,目前仅不支持通知弹框下载功能
  93. *
  94. */
  95. /// <summary>
  96. /// 透传模板
  97. /// </summary>
  98. /// <returns></returns>
  99. public static TransmissionTemplate GeTuiTransmissionTemplate(string notifyTitle, string notifyContent, string newsId)
  100. {
  101. var template = new TransmissionTemplate();
  102. template.AppId = Appid;
  103. template.AppKey = Appkey;
  104. //应用启动类型,1:强制应用启动 2:等待应用启动
  105. template.TransmissionType = "2";
  106. template.TransmissionContent = JsonConvert.SerializeObject(new { title = notifyTitle, content = notifyContent, payload =new{ newsId,flag=true } });
  107. //APN高级推送 IOS
  108. APNPayload apnpayload = new APNPayload();
  109. DictionaryAlertMsg alertMsg = new DictionaryAlertMsg();
  110. alertMsg.Body = notifyContent;
  111. alertMsg.ActionLocKey = "ActionLocKey";
  112. alertMsg.LocKey = "LocKey";
  113. alertMsg.addLocArg("LocArg");
  114. alertMsg.LaunchImage = "LaunchImage";
  115. //iOS8.2支持字段
  116. alertMsg.Title = notifyTitle;
  117. alertMsg.TitleLocKey = "TitleLocKey";
  118. alertMsg.addTitleLocArg("TitleLocArg");
  119. apnpayload.AlertMsg = alertMsg;
  120. //应用icon上显示的数字
  121. apnpayload.Badge = 1;
  122. //包含此键值1以配置后台更新通知。当此键出现时,系统会在后台唤醒您的应用程序,并将通知发送给其应用程序代理。
  123. apnpayload.ContentAvailable = 1;
  124. //apnpayload.Category = "";
  125. //当您希望系统播放声音时,请加入此键。此键的值是应用程序主包中或Library / Sounds应用程序数据容器文件夹中的声音文件的名称。如果找不到声音文件,或者如果您指定default了该值,系统将播放默认警报声音。
  126. apnpayload.Sound = "test1.wav";
  127. apnpayload.addCustomMsg("payload", new { newsId, flag = true });
  128. template.setAPNInfo(apnpayload);
  129. return template;
  130. }
  131. }
  132. }