/// <summary> /// 发送个推信息【可批量,支持IOS和安卓 批量与单个用户,通知与传透】 /// </summary> /// <param name="condtion"></param> /// <returns></returns> internal static ReplayBase SendGTuiPushByPusSets(SendGeTuiPushBySetRequestModel condtion) { ReplayBase result = new ReplayBase(); try { condtion.sets = condtion.pushSets; if (condtion.sets.Count > 0) { ReplayBase record = SysSmsDataBaseManager.RunSaveSentPush(condtion); SendGeTuiPushReplay r = GTuiPushInterFaceService.PushMessage(condtion); SysSmsDataBaseManager.RunUpdateSentPushStatus(record.ReturnMessage, r); result.ReturnMessage = r.sentResultAndroid + "&" + r.sentResultIos; } else { result.ReturnCode = EnumErrorCode.ServiceError; result.ReturnMessage = "请至少传入一条接收者的推送设置"; } } catch (Exception e) { result.ReturnCode = EnumErrorCode.ServiceError; result.ReturnMessage = "服务器错误 500"; SysManagerService.SysSaveErrorLogMsg(e.ToString(), condtion); } return(result); }
/// <summary> /// 更新推送设置的状态与结果 /// </summary> /// <param name="pushId">推送的ID</param> /// <param name="r">推送结果信息</param> internal static void RunUpdateSentPushStatus(string pushId, SendGeTuiPushReplay r) { using (SysTemDataBaseDataContext context = new SysTemDataBaseDataContext(SqlConnection)) { Us_RunSentPush dbp = context.Us_RunSentPush.Single(p => p.Us_RunSentPushId == pushId); dbp.sentStats = r.ReturnCode == EnumErrorCode.Success ? true : false; dbp.sentResultAndroid = r.sentResultAndroid; dbp.sentResultIos = r.sentResultIos; context.SubmitChanges(); } }
/// <summary> /// 发送推送[目前仅支持传透和通知,支持Android和IOS] /// </summary> /// <param name="condtion"></param> internal static SendGeTuiPushReplay PushMessage(SendGeTuiPushBySetRequestModel condtion) { SendGeTuiPushReplay result = new SendGeTuiPushReplay(); IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET); ListMessage listMessage = new ListMessage() { IsOffline = IsOffline, OfflineExpireTime = OfflineExpireTime, PushNetWorkType = PushNetWorkType }; SingleMessage singleMessage = new SingleMessage() { IsOffline = IsOffline, OfflineExpireTime = OfflineExpireTime, PushNetWorkType = PushNetWorkType }; TransmissionTemplate tt = TransmissionTemplateDemo(); //传透 NotificationTemplate nt = NotificationTemplateDemo(); //通知 if (condtion.sets.Count > 0) { //分离请求中的设备类型 List <GeTuiSetModel> ios = condtion.sets.Where(p => p.deviceType != EnumUserDeviceType.Android).ToList(); List <GeTuiSetModel> android = condtion.sets.Where(p => p.deviceType == EnumUserDeviceType.Android).ToList(); //IOS和安卓分开处理 if (ios.Count > 0) { //IOS设备发送 //IOS需使用传透方式来发送 APNTemplate template = new APNTemplate(); APNPayload apnpayload = new APNPayload(); DictionaryAlertMsg alertMsg = new DictionaryAlertMsg(); alertMsg.Body = condtion.msg; alertMsg.ActionLocKey = ""; alertMsg.LocKey = ""; alertMsg.addLocArg(""); alertMsg.LaunchImage = ""; //IOS8.2支持字段 alertMsg.Title = condtion.title; alertMsg.TitleLocKey = ""; alertMsg.addTitleLocArg(""); apnpayload.AlertMsg = alertMsg; apnpayload.Badge = 1; apnpayload.ContentAvailable = 1; //apnpayload.Category = ""; apnpayload.Sound = "sms-received1.caf"; apnpayload.addCustomMsg("customInfo", condtion.customInfo); template.setAPNInfo(apnpayload); template.AppId = APPID; template.AppKey = APPKEY; if (ios.Count > 1) { //批量 listMessage.Data = template; List <string> devicetokenlist = new List <string>(); foreach (GeTuiSetModel set in ios) { devicetokenlist.Add(set.clientId); } string contentId = push.getAPNContentId(APPID, listMessage); result.sentResultIos = push.pushAPNMessageToList(APPID, contentId, devicetokenlist); } else if (ios.Count == 1) { //单个 singleMessage.Data = template; result.sentResultIos = push.pushAPNMessageToSingle(APPID, ios[0].clientId, singleMessage); } } if (android.Count > 0) { switch (condtion.messageType) { case EnumPushMessagesType.透传消息: tt.TransmissionContent = condtion.msg; listMessage.Data = tt; singleMessage.Data = tt; break; case EnumPushMessagesType.通知: nt.Title = condtion.title; nt.Text = condtion.msg; nt.TransmissionContent = Newtonsoft.Json.JsonConvert.SerializeObject(condtion.customInfo); listMessage.Data = nt; singleMessage.Data = nt; break; } //安卓设备发送 if (android.Count > 1) { //多个用户 List <Target> ts = new List <Target>(); foreach (GeTuiSetModel set in android) { ts.Add(new Target() { appId = APPID, clientId = set.clientId }); } string contentId = push.getContentId(listMessage, "ToList_" + DateTime.Now.ToString("yyyyMMddHHmmss")); result.sentResultAndroid = push.pushMessageToList(contentId, ts); } else if (android.Count == 1) { //单个用户 Target t = new Target(); t.appId = APPID; t.clientId = android[0].clientId; result.sentResultAndroid = push.pushMessageToSingle(singleMessage, t); } } result.ReturnCode = EnumErrorCode.Success; } else { result.ReturnCode = EnumErrorCode.EmptyDate; result.ReturnMessage = "没有设置任何的接收者"; SysManagerService.SysSaveSysLog("推送消息:[" + condtion.msg + "]没有设置任何的接收者", EnumSysLogType.警告); } return(result); }