Ejemplo n.º 1
0
        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));
                }
            }
        }