using com.igetui.api.openservice; using com.igetui.api.openservice.igetui; using com.igetui.api.openservice.igetui.template; using com.igetui.api.openservice.payload; using LYFZ.WanYuKeFuData.Model; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace LYFZ.WanYuKeFu { public class ErpGetuiApiSDK { public ErpGetuiApiSDK() { } #region 生产环境配置 static string _APPID = ""; public static string APPID { get { return ErpGetuiApiSDK._APPID; } // set { ErpGetuiApiSDK._APPID = value; } } static string _APPKEY = ""; public static string APPKEY { get { return ErpGetuiApiSDK._APPKEY; } //set { ErpGetuiApiSDK._APPKEY = value; } } static string _AppSecret = ""; public static string AppSecret { get { return ErpGetuiApiSDK._AppSecret; } // set { ErpGetuiApiSDK._AppSecret = value; } } static string _MASTERSECRET = ""; public static string MASTERSECRET { get { return ErpGetuiApiSDK._MASTERSECRET; } // set { ErpGetuiApiSDK._MASTERSECRET = value; } } static string _HOST = ""; public static string HOST { get { return ErpGetuiApiSDK._HOST; } // set { ErpGetuiApiSDK._HOST = value; } } #endregion #region 开发环境配置 static string _Dev_APPID = ""; public static string Dev_APPID { get { return ErpGetuiApiSDK._Dev_APPID; } } static string _Dev_APPKEY = ""; public static string Dev_APPKEY { get { return ErpGetuiApiSDK._Dev_APPKEY; } } static string _Dev_AppSecret = ""; public static string Dev_AppSecret { get { return ErpGetuiApiSDK._Dev_AppSecret; } } static string _Dev_MASTERSECRET = ""; public static string Dev_MASTERSECRET { get { return ErpGetuiApiSDK._Dev_MASTERSECRET; } } static string _Dev_HOST = ""; public static string Dev_HOST { get { return ErpGetuiApiSDK._Dev_HOST; } } #endregion static string _WanYuKFUrl = ""; public static string WanYuKFUrl { get { return ErpGetuiApiSDK._WanYuKFUrl; } } public static void LoadGetuiApiSDKConfig() { string configString = System.IO.File.ReadAllText(LYFZ.WinAPI.CustomPublicMethod.GetFullDirectoryPath(LYFZ.WinAPI.CustomPublicMethod.BasePath)+"admin\\json\\systemParameter.json",System.Text.Encoding.UTF8); dynamic configJson = JsonConvert.DeserializeObject(configString); string GetuiAPI = configJson["GetuiAPI"].Value; ErpGetuiApiSDK._WanYuKFUrl = configJson["WanYuKFUrl"].Value; ErpGetuiApiSDK._Dev_APPID = configJson["DevAPPID"].Value; ErpGetuiApiSDK._Dev_APPKEY = configJson["DevAPPKEY"].Value; ErpGetuiApiSDK._Dev_AppSecret = configJson["DevAppSecret"].Value; ErpGetuiApiSDK._Dev_MASTERSECRET = configJson["DevMASTERSECRET"].Value; ErpGetuiApiSDK._Dev_HOST = configJson["DevHOST"].Value; ErpGetuiApiSDK._APPID = configJson["APPID"].Value; ErpGetuiApiSDK._APPKEY = configJson["APPKEY"].Value; ErpGetuiApiSDK._AppSecret = configJson["AppSecret"].Value; ErpGetuiApiSDK._MASTERSECRET = configJson["MASTERSECRET"].Value; ErpGetuiApiSDK._HOST = configJson["HOST"].Value; /* ErpGetuiApiSDK._APPID = ErpGetuiApiSDK._Dev_APPID; ErpGetuiApiSDK._APPKEY = ErpGetuiApiSDK._Dev_APPKEY; ErpGetuiApiSDK._AppSecret = ErpGetuiApiSDK._Dev_AppSecret; ErpGetuiApiSDK._MASTERSECRET = ErpGetuiApiSDK._Dev_MASTERSECRET; ErpGetuiApiSDK._HOST = ErpGetuiApiSDK._Dev_HOST; */ Global.WriteLog(String.Format("Pro ErpGetuiApiSDK._APPID:{0} ErpGetuiApiSDK._APPKEY:{1} ErpGetuiApiSDK._AppSecret:{2} ErpGetuiApiSDK._MASTERSECRET:{3} ErpGetuiApiSDK._HOST:{4}", ErpGetuiApiSDK._APPID, ErpGetuiApiSDK._APPKEY, ErpGetuiApiSDK._AppSecret, ErpGetuiApiSDK._MASTERSECRET, ErpGetuiApiSDK._HOST), "-1", Global.LogsDongleDomainList); Global.WriteLog(String.Format("Dev ErpGetuiApiSDK._APPID:{0} ErpGetuiApiSDK._APPKEY:{1} ErpGetuiApiSDK._AppSecret:{2} ErpGetuiApiSDK._MASTERSECRET:{3} ErpGetuiApiSDK._HOST:{4}", ErpGetuiApiSDK._Dev_APPID, ErpGetuiApiSDK._Dev_APPKEY, ErpGetuiApiSDK._Dev_AppSecret, ErpGetuiApiSDK._Dev_MASTERSECRET, ErpGetuiApiSDK._Dev_HOST), "-1", Global.LogsDongleDomainList); } /// /// 批量单推消息 /// /// /// /// public static string pushMessageToSingleBatchApp(LYFZ.WanYuKeFuData.Model.GTMsgRecord msgRecord, LYFZ.WanYuKeFuData.DAL.GTMessagePushUser msgPushUserDal) { try { string retStr = "{\"result\":\"失败\",\"contentId\":\"\"}"; if (msgRecord.TargetUser == 0) { retStr = pushMessageToApp(msgRecord,1); retStr = pushMessageToApp(msgRecord,0); } else { List sUserList = msgPushUserDal.GetPushSingleUserList(msgRecord.TargetUser, msgRecord.TargetList); if (sUserList.Count > 0) { List iosCidList = new List(); List proUserList = new List(); List devUserList = new List(); foreach (PushSingleUser suser in sUserList) { if (suser.GTType == 0) { iosCidList.Add(suser.GTClientID); } Global.WriteLog(String.Format("PushSingleUser suser.GTClientID:{0} suser.APPState:{1}", suser.GTClientID, suser.APPState), "-1", Global.LogsDongleDomainList); //如果要推送的用户少于等于2个时,使用单推,直接推送 if (sUserList.Count <= 2) { if ((msgRecord.Platform.ToLower().Contains("ios") && suser.GTType == 0) || (msgRecord.Platform.ToLower().Contains("android") && suser.GTType == 1)) { retStr = PushMessageToSingle(msgRecord, suser.GTClientID, suser.UserID, suser.Badge, suser.APPState,suser.LYFZAPPID); } // retStr = "{\"result\":\"ok\",\"contentId\":\"\"}"; } if ((msgRecord.Platform.ToLower().Contains("ios") && suser.GTType == 0) || (msgRecord.Platform.ToLower().Contains("android") && suser.GTType == 1)) { if (suser.APPState == 1) { proUserList.Add(suser); } else { devUserList.Add(suser); } } } //如果要推送的用户大于2个时,使用批量单推 if (sUserList.Count > 2) { if (proUserList.Count > 0) { retStr = PushMessageToSingleBatch(msgRecord, proUserList, 1, proUserList[0].LYFZAPPID); } if (retStr.ToLower().Contains("result\":\"ok\",")) { if (devUserList.Count > 0) { retStr = PushMessageToSingleBatch(msgRecord, devUserList, 0, proUserList[0].LYFZAPPID); } } // retStr = "{\"result\":\"ok\",\"contentId\":\"\"}"; } try { if (retStr.ToLower().Contains("result\":\"ok\",")) { msgPushUserDal.AddBadgeNumber(iosCidList.ToArray()); } } catch { } iosCidList.Clear(); } else { retStr = "{\"result\":\"-900000\",\"contentId\":\"\"}"; } sUserList.Clear(); sUserList = null; } return retStr; } catch (Exception ex) { return "{\"result\":\"" + ex.Message + "\",\"contentId\":\"\"}"; } } /// /// 单推消息 /// /// /// /// /// private static string PushMessageToSingle(LYFZ.WanYuKeFuData.Model.GTMsgRecord msgRecord, string _ClientID,string _userid,int _Badge=0,int appState=1,string lyfzAppid="") { try { LYFZ.WanYuKeFuData.Model.APPConfig configModel = Global.GetAPPConfig(lyfzAppid,appState); if (configModel == null) { return "{\"result\":\"没有找到推送APP服务配置信息\",\"contentId\":\"\"}"; } string currentAPPID = configModel.GTAppID; string currentAPPKEY = configModel.GTAppKey; IGtPush push = new IGtPush(configModel.HostUrl, currentAPPKEY, configModel.GTMasterSecret); Global.WriteLog(String.Format("Pro HOST:{0}, APPKEY:{1}, MASTERSECRET:{2} currentAPPID:{3}", configModel.HostUrl, currentAPPKEY, configModel.GTMasterSecret, currentAPPID), "-1", Global.LogsDongleDomainList); /*if (appState == 1) { currentAPPID = APPID; currentAPPKEY = APPKEY; push = new IGtPush(HOST, APPKEY, MASTERSECRET); Global.WriteLog(String.Format("Pro HOST:{0}, APPKEY:{1}, MASTERSECRET:{2} currentAPPID:{3}", HOST, APPKEY, MASTERSECRET, currentAPPID), "-1", Global.LogsDongleDomainList); } else { currentAPPID = Dev_APPID; currentAPPKEY = Dev_APPKEY; push = new IGtPush(Dev_HOST, Dev_APPKEY, Dev_MASTERSECRET); Global.WriteLog(String.Format("Dev HOST:{0}, APPKEY:{1}, MASTERSECRET:{2} currentAPPID:{3}", Dev_HOST, Dev_APPKEY, Dev_MASTERSECRET, currentAPPID), "-1", Global.LogsDongleDomainList); }*/ Global.WriteLog(String.Format("IGtPush:{0} ", push.ToString()), "-1", Global.LogsDongleDomainList); //消息模版:TransmissionTemplate:透传模板 TransmissionTemplate template = TransmissionTemplateMsg(msgRecord.Title, msgRecord.Description, msgRecord.MsgData, _userid, msgRecord.ID.ToString(), currentAPPID, currentAPPKEY, _Badge: _Badge + 1); // NotificationTemplate template = NotificationTemplateMsg(msgRecord.Title, msgRecord.Description, msgRecord.MsgData, _Badge: _Badge+1); // 单推消息模型 SingleMessage message = new SingleMessage(); message.IsOffline = true; // 用户当前不在线时,是否离线存储,可选 message.OfflineExpireTime = 1000 * 3600 * 12; // 离线有效时间,单位为毫秒,可选 message.Data = template; //判断是否客户端是否wifi环境下推送,2为4G/3G/2G,1为在WIFI环境下,0为不限制环境 message.PushNetWorkType = 0; com.igetui.api.openservice.igetui.Target target = new com.igetui.api.openservice.igetui.Target(); target.appId = currentAPPID; target.clientId = _ClientID; //target.alias = ALIAS;//别名 String pushResult = ""; try { pushResult = push.pushMessageToSingle(message, target); // System.Console.WriteLine("----------------服务端返回结果:" + pushResult); } catch (RequestException e) { String requestId = e.RequestId; //发送失败后的重发 pushResult = push.pushMessageToSingle(message, target, requestId); //System.Console.WriteLine("----------------服务端返回结果:" + pushResult); } //{"result":"ok","contentId":"OSA-0930_h5nst6murS6Qc5OAPVwnz2"} Global.WriteLog(String.Format("pushResult:{0} ", pushResult), "-1", Global.LogsDongleDomainList); return pushResult; } catch (Exception ex){ Global.WriteLog(String.Format("Exception:{0} ", ex.Message), "-1", Global.LogsDongleDomainList); return "{\"result\":\""+ex.Message+"\",\"contentId\":\"\"}"; } } /// /// 批量单推消息 /// /// /// /// public static string PushMessageToSingleBatch(LYFZ.WanYuKeFuData.Model.GTMsgRecord msgRecord, List _ClientIDList,int appState=1,string lyfzAppid="") { try { LYFZ.WanYuKeFuData.Model.APPConfig configModel = Global.GetAPPConfig(lyfzAppid, appState); if (configModel == null) { return "{\"result\":\"没有找到推送APP服务配置信息\",\"contentId\":\"\"}"; } string currentAPPID = configModel.GTAppID; string currentAPPKEY = configModel.GTAppKey; IGtPush push = new IGtPush(configModel.HostUrl, currentAPPKEY, configModel.GTMasterSecret); IBatch batch = new BatchImpl(currentAPPKEY, push); Global.WriteLog(String.Format("Pro HOST:{0}, APPKEY:{1}, MASTERSECRET:{2} currentAPPID:{3}", configModel.HostUrl, currentAPPKEY, configModel.GTMasterSecret, currentAPPID), "-1", Global.LogsDongleDomainList); /* if (appState == 1) { currentAPPID = APPID; currentAPPKEY = APPKEY; push = new IGtPush(HOST, APPKEY, MASTERSECRET); batch = new BatchImpl(APPKEY, push); Global.WriteLog(String.Format("Pro HOST:{0}, APPKEY:{1}, MASTERSECRET:{2} currentAPPID:{3}", HOST, APPKEY, MASTERSECRET, currentAPPID), "-1", Global.LogsDongleDomainList); } else { currentAPPID = Dev_APPID; currentAPPKEY = Dev_APPKEY; push = new IGtPush(Dev_HOST, Dev_APPKEY, Dev_MASTERSECRET); batch = new BatchImpl(Dev_APPKEY, push); Global.WriteLog(String.Format("Dev HOST:{0}, APPKEY:{1}, MASTERSECRET:{2} currentAPPID:{3}", Dev_HOST, Dev_APPKEY, Dev_MASTERSECRET, currentAPPID), "-1", Global.LogsDongleDomainList); }*/ int targetCount = 0; foreach (PushSingleUser cidItem in _ClientIDList) { if (cidItem.APPState == appState) { //IOS,Android if ((msgRecord.Platform.ToLower().Contains("ios") && cidItem.GTType == 0) || (msgRecord.Platform.ToLower().Contains("android") && cidItem.GTType == 1)) { //消息模版:TransmissionTemplate:透传模板 TransmissionTemplate template = TransmissionTemplateMsg(msgRecord.Title, msgRecord.Description, msgRecord.MsgData, cidItem.UserID, msgRecord.ID.ToString(), currentAPPID,currentAPPKEY, _Badge: cidItem.Badge + 1); // NotificationTemplate template = NotificationTemplateMsg(msgRecord.Title, msgRecord.Description, msgRecord.MsgData, _Badge: cidItem.Badge+1); // 单推消息模型 SingleMessage messageTrans = new SingleMessage(); messageTrans.IsOffline = true; // 用户当前不在线时,是否离线存储,可选 messageTrans.OfflineExpireTime = 1000 * 3600 * 12; // 离线有效时间,单位为毫秒,可选 messageTrans.Data = template; //判断是否客户端是否wifi环境下推送,2为4G/3G/2G,1为在WIFI环境下,0为不限制环境 messageTrans.PushNetWorkType = 0; com.igetui.api.openservice.igetui.Target targetTrans = new com.igetui.api.openservice.igetui.Target(); targetTrans.appId = currentAPPID; targetTrans.clientId = cidItem.GTClientID; targetCount++; batch.add(messageTrans, targetTrans); } } } //{"result":"ok","info":{"0":{"result":"ok","taskId":"OSS-0930_if8pCKldBb9H0sGxnZTss4","cid":"50cbe6d6082e32a0c829f55fb13c9a5c","status":"successed_offline"},"1":{"result":"ok","taskId":"OSS-0930_BNvR2QGEbl8GJ7AN463dh5","cid":"92254c4b43b01d4e99fa5a77cde4ed36","status":"successed_offline"}}} if (targetCount > 0) { try { return batch.submit(); } catch //(Exception e) { return batch.retry(); } } else { return "{\"result\":\"没有要发送的客户端\",\"contentId\":\"\"}"; } } catch (Exception ex) { return "{\"result\":\"" + ex.Message + " appState:"+ appState.ToString() + "\",\"contentId\":\"\"}"; } } /// /// 向所有用户推送消息 /// /// private static string pushMessageToApp(LYFZ.WanYuKeFuData.Model.GTMsgRecord msgRecord, int appState = 1) { try { IGtPush push = null; string currentAPPID = ""; string currentAPPKEY = ""; if (appState == 1) { currentAPPID = APPID; currentAPPKEY = APPKEY; push = new IGtPush(HOST, APPKEY, MASTERSECRET); } else { currentAPPID = Dev_APPID; currentAPPKEY = Dev_APPKEY; push = new IGtPush(Dev_HOST, Dev_APPKEY, Dev_MASTERSECRET); } // 定义"AppMessage"类型消息对象,设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒) AppMessage message = new AppMessage(); TransmissionTemplate template = TransmissionTemplateMsg(msgRecord.Title, msgRecord.Description, msgRecord.MsgData, "", msgRecord.ID.ToString(), currentAPPID, currentAPPKEY, _Badge: 1); //TransmissionTemplateDemo(); //NotificationTemplate template = NotificationTemplateMsg(msgRecord.Title, msgRecord.Description, msgRecord.MsgData); message.IsOffline = true; // 用户当前不在线时,是否离线存储,可选 message.OfflineExpireTime = 1000 * 3600 * 12; // 离线有效时间,单位为毫秒,可选 message.Data = template; //判断是否客户端是否wifi环境下推送,2:4G/3G/2G,1为在WIFI环境下,0为无限制环境 message.PushNetWorkType = 0; //message.Speed = 1000; List appIdList = new List(); appIdList.Add(currentAPPID); List phoneTypeList = new List(); //通知接收者的手机操作系统类型 phoneTypeList.Add("ANDROID"); phoneTypeList.Add("IOS"); List provinceList = new List(); //通知接收者所在省份 //provinceList.Add("浙江"); //provinceList.Add("上海"); //provinceList.Add("北京"); List tagList = new List(); //tagList.Add("中文"); message.AppIdList = appIdList; message.PhoneTypeList = phoneTypeList; //message.ProvinceList = provinceList; // message.TagList = tagList; String pushResult = push.pushMessageToApp(message); return pushResult; } catch (Exception ex) { return "{\"result\":\"" + ex.Message + "\",\"contentId\":\"\"}"; } // System.Console.WriteLine("-----------------------------------------------"); // System.Console.WriteLine("服务端返回结果:" + pushResult); } /* * * 所有推送接口均支持四个消息模板,依次为透传模板,通知透传模板,通知链接模板,通知弹框下载模板 * 注:IOS离线推送需通过APN进行转发,需填写pushInfo字段,目前仅不支持通知弹框下载功能 * */ static string GetMsgContent(string content) { string retContent = ""; if (content.ToLower().IndexOf("http://") == 0 || content.ToLower().IndexOf("https://") == 0) { retContent = JsonConvert.SerializeObject(new { JumpURL = content }); } else { retContent = content; } return retContent; } /// /// 透传模板动作内容 /// /// /// /// /// /// public static TransmissionTemplate TransmissionTemplateMsg(string _title, string _description, string _Content, string _userid,string _msgid,string tAppid,string tAppkey,int _Badge = 0) { string sendMsgContent = ""; string tempMsgString = GetMsgContent(_Content); dynamic smgObj = new { msgid=_msgid, title = _title, description = _description, content = _Content.IndexOf("{\"") == 0 ? JsonConvert.DeserializeObject(_Content) : tempMsgString.IndexOf("{\"") == 0 ? JsonConvert.DeserializeObject(tempMsgString) : tempMsgString, sendTime=DateTime.Now.ToYYYY_MM_dd_HH_mm_ss(), badge = _Badge }; sendMsgContent = JsonConvert.SerializeObject(smgObj); TransmissionTemplate template = new TransmissionTemplate(); template.AppId = tAppid; template.AppKey = tAppkey; template.TransmissionType = "2"; //应用启动类型,1:强制应用启动 2:等待应用启动 template.TransmissionContent = sendMsgContent; //透传内容 // template.setPushInfo("",8, "加上了安卓消息 我是来至于C#的测试消息", "default", "my C# no","","",""); //APN高级推送 APNPayload apnpayload = new APNPayload(); DictionaryAlertMsg alertMsg = new DictionaryAlertMsg(); alertMsg.Body = _description; //iOS8.2支持字段 alertMsg.Title = _title; apnpayload.AlertMsg = alertMsg; if (_Badge > 0) { apnpayload.Badge = _Badge; } apnpayload.ContentAvailable = 1; //apnpayload.Category = ""; apnpayload.Sound = "default"; apnpayload.addCustomMsg("payload", sendMsgContent); template.setAPNInfo(apnpayload); //设置客户端展示时间 //String begin = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); // String end = DateTime.Now.AddMinutes(10).ToString("yyyy-MM-dd HH:mm:ss"); // template.setDuration(begin, end); return template; } /// /// 通知透传模板动作内容 /// /// 标题 /// 描述 /// 内容 /// 应用启动类型,1:强制应用启动 2:等待应用启动 /// 消息数 IOS 传用 /// 通知栏显示图标 /// /* public static NotificationTemplate NotificationTemplateMsg(string _title, string _description, string _Content, string _TransmissionType="2", int _Badge = 0, string _Logo = "") { NotificationTemplate template = new NotificationTemplate(); template.AppId = APPID; template.AppKey = APPKEY; //通知栏标题 template.Title = _title; //通知栏内容 template.Text = _description; if (_Logo.Contains("https://") || _Logo.Contains("http://")) { //通知栏显示网络图标 template.LogoURL = _Logo.Trim(); } else { //通知栏显示本地图片 template.Logo = _Logo.Trim(); } //通知栏显示本地图片 // template.Logo = _Logo; //通知栏显示网络图标 // template.LogoURL = ""; //应用启动类型,1:强制应用启动 2:等待应用启动 template.TransmissionType = "2"; //透传内容 template.TransmissionContent = _Content; //接收到消息是否响铃,true:响铃 false:不响铃 template.IsRing = true; //接收到消息是否震动,true:震动 false:不震动 template.IsVibrate = true; //接收到消息是否可清除,true:可清除 false:不可清除 template.IsClearable = true; //APN高级推送 APNPayload apnpayload = new APNPayload(); DictionaryAlertMsg alertMsg = new DictionaryAlertMsg(); alertMsg.Body = _description; //iOS8.2支持字段 alertMsg.Title = _title; apnpayload.AlertMsg = alertMsg; if (_Badge > 0) { apnpayload.Badge = _Badge; } apnpayload.ContentAvailable = 1; //apnpayload.Category = ""; apnpayload.Sound = "default"; apnpayload.addCustomMsg("payload", _Content); template.setAPNInfo(apnpayload); //设置通知定时展示时间,结束时间与开始时间相差需大于6分钟,消息推送后,客户端将在指定时间差内展示消息(误差6分钟) //String begin = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); // String end = DateTime.Now.AddMinutes(10).ToString("yyyy-MM-dd HH:mm:ss"); // template.setDuration(begin, end); return template; }*/ } }