using Jiguang.JPush; using Jiguang.JPush.Model; using System; using System.Collections.Generic; using System.Linq; using System.Web; using VueWebCoreApi.Models; namespace VueWebCoreApi.Tools { public class JPushManage { public static ToMessage mes = new ToMessage(); //定义全局返回信息对象 private static JPushClient client = new JPushClient("502772cf67ff42b7b55c8a3f", "55f2881b7afde3e08e30da58"); //"自己的key", "自己的MasterSecret" //List registration_id, string title, string content /// /// 推送到个人,每次推送最多1000人 /// /// 请求用户 /// 车间编码 /// 设备编码 /// 提交的数据 public static ToMessage ExecutePushByUserId(string username, string wkshpcode, string eqpcode, List json) { try { for (int i = 0; i < json.Count; i++) { //List registration_id = new List(); List alias = new List(); string title = json[i].name.ToString(); //标题 string content = "来自用户:" + username.ToString() + "内容:车间编号:【" + wkshpcode + "】,对应设备编号:【" + eqpcode + "】,时间" + DateTime.Now.ToString("yyyy/MM/ddhh:mm:ss") + "的消息通知!"; //内容 //registration_id = json[i].children.Select(x => x.rid).Distinct().ToList(); //获取App集成SDK生成的Rid[ "111", "22" ] 去除重复 alias = json[i].children.Select(x => x.alias).Distinct().ToList(); //获取别名[ "111", "22" ] 去除重复 //audience : { "registration_id" : [ "111", "22" ]} 设备标识。一次推送最多 1000 个。 var pushPayload = new PushPayload() { Platform = new List { "android" }, //推送平台设置(必填) //Audience = "{\"registration_id\" :" + Newtonsoft.Json.JsonConvert.SerializeObject(registration_id) + "}", //推送设备指定(必填) Audience = new { alias = alias.ToArray()}, //推送人员指定(必填) Notification = new Notification { Android = new Android { Alert = content, Title = title }, }, Options = new Options { TimeToLive = 60//单位秒,最大值10天 } }; var response = client.SendPush(pushPayload); mes.code = response.StatusCode.ToString(); mes.message = response.Content; mes.data = null; } } catch (Exception e) { mes.code = "300"; mes.message = e.Message; mes.data = null; } return mes; } } }