public PushResponse Process(PushRequest pRequest) { PushResponse response = new PushResponse(); response.ResultCode = 0; try { MessageChannelDAO channelDao = new MessageChannelDAO(new BasicUserInfo()); MessageDAO dao = new MessageDAO(new BasicUserInfo()); var entity = channelDao.GetByID(pRequest.ChannelID); if (entity == null) { throw new Exception(string.Format("未找到相应的ChannelID为:{0}的Channel信息", pRequest.ChannelID)); } MessageEntity mEntity = new MessageEntity(); mEntity.MessageID = Guid.NewGuid(); mEntity.ClientID = pRequest.ClientID; mEntity.ChannelID = pRequest.ChannelID; mEntity.AppCode = pRequest.AppCode; mEntity.UserID = pRequest.UserID; var notification = pRequest.Request.DeserializeJSONTo <JdSoft.Apple.Apns.Notifications.Notification>(); mEntity.MessageParameters = pRequest.GetJson(); switch (pRequest.Type) { case 1: //直接发送 var IOSChannel = entity.GetIOSChannel(); var result = IOSNotificationService.CreateInstance(IOSChannel.SandBox ?? true, IOSChannel.P12, IOSChannel.P12PWD).SendNotification(notification); if (result) { mEntity.SendCount = 1; mEntity.Status = 2; dao.Create(mEntity); Loggers.Debug(new DebugLogInfo() { Message = "【发送到推送服务器成功】:" + mEntity.MessageParameters }); } else { mEntity.Status = 1; mEntity.SendCount = 1; mEntity.FaultReason = "发送到推送服务器失败"; dao.Create(mEntity); Loggers.Debug(new DebugLogInfo() { Message = "【发送到推送服务器失败】:" + mEntity.MessageParameters }); throw new Exception("【发送到推送服务器失败】"); } break; case 2: //存放到数据库 mEntity.SendCount = 0; mEntity.Status = 0; dao.Create(mEntity); Loggers.Debug(new DebugLogInfo() { Message = "【保存数据库成功】:" + mEntity.MessageParameters }); break; default: throw new Exception("未定义的请求类型:" + pRequest.Type); } } catch (Exception ex) { response.ResultCode = 100; response.Message = ex.Message; Loggers.Exception(new ExceptionLogInfo(ex)); } return(response); }
void bw_DoWork1(object sender, DoWorkEventArgs e) { while (true) { try { MessageDAO dao = new MessageDAO(new BasicUserInfo()); MessageChannelDAO channelDao = new MessageChannelDAO(new BasicUserInfo()); var entitys = dao.GetValidMessage(); foreach (var item in entitys) { try { var entity = channelDao.GetByID(item.ChannelID); if (entity == null) { throw new Exception(string.Format("未找到相应的ChannelID为:{0}的Channel信息", item.ChannelID)); } switch (entity.MobilePlatform) { case 1: var request = item.GetRequest(); var baiduResponse = BaiduCloudPush.PushMessage(entity.GetBaiDuChannel(), request); if (baiduResponse.IsSuccess) { item.SendCount = (item.SendCount ?? 0) + 1; item.Status = 2; Loggers.Debug(new DebugLogInfo() { Message = "【发送成功】:" + item.MessageContent }); } else { item.SendCount = (item.SendCount ?? 0) + 1; item.Status = 1; item.FaultReason = baiduResponse.ErrorMessage; Loggers.Debug(new DebugLogInfo() { Message = "【发送失败】:" + item.MessageContent }); } break; case 2: var IOSChannel = entity.GetIOSChannel(); var notification = item.GetIOSNotification(); var IOSResponse = IOSNotificationService.CreateInstance(IOSChannel.SandBox ?? true, IOSChannel.P12, IOSChannel.P12PWD).SendNotification(notification); if (IOSResponse) { item.SendCount = (item.SendCount ?? 0) + 1; item.Status = 2; Loggers.Debug(new DebugLogInfo() { Message = "【发送到推送服务器成功】:" + item.MessageContent }); } else { item.SendCount = (item.SendCount ?? 0) + 1; item.Status = 1; item.FaultReason = "发送到推送服务器失败"; Loggers.Debug(new DebugLogInfo() { Message = "【发送到推送服务器成功】:" + item.MessageContent }); } break; default: throw new Exception("错误的平台类型"); } dao.Update(item); } catch (Exception ee) { item.SendCount = (item.SendCount ?? 0) + 1; item.FaultReason = ee.Message; item.Status = 1; dao.Update(item); Loggers.Exception(new ExceptionLogInfo(ee)); throw ee; } } Thread.Sleep(TimeSpan.FromSeconds(Interval)); } catch (Exception ex) { Loggers.Exception(new ExceptionLogInfo(ex)); } } }