Beispiel #1
0
        public HttpResponseMessage Push(PushParams pushParams)
        {
            try
            {
                if (pushParams == null || pushParams.Targets == null || pushParams.Targets.Count == 0 || pushParams.Message == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "推送参数为空。"));
                }

                //按apptype的类型分组
                Dictionary <int, List <PushTarget> > dicAppType = (from item in pushParams.Targets
                                                                   group item by item.AppType into gtype
                                                                   orderby gtype.Key
                                                                   select new
                {
                    Key = gtype.Key,
                    Value = gtype.ToList()
                }).ToDictionary(a => a.Key, a => a.Value);
                foreach (int type in dicAppType.Keys)
                {
                    AppPushChannelInfo channelInfo = null;
                    if (EnvProvider.Default.DicAppPushInfo.TryGetValue((AppType)type, out channelInfo))
                    {
                        if (channelInfo == null)
                        {
                            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("App类型:{0}对应的推送配置信息为空。", type.ToString())));
                        }
                        IChannelProvider pushChannel = GetProviderByType(channelInfo.Channel);
                        if (pushChannel == null)
                        {
                            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "未能找到需要的推送通道。"));
                        }
                        PushParams thisPush = new PushParams()
                        {
                            Message = pushParams.Message,
                            Targets = dicAppType[type]
                        };
                        if (thisPush.Targets == null || thisPush.Targets.Count == 0)
                        {
                            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("App类型:{0}对应的推送目标为空。", type.ToString())));
                        }
                        PushResult result = pushChannel.Push(thisPush);
                        if (result == null || result.IsCallPushError)
                        {
                            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("向App类型:{0}对应的推送目标推送时结果为空。", type.ToString())));
                        }
                        if (result.IsCallPushError)
                        {
                            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("向App类型:{0}对应的推送目标推送时出现错误,推送结果:{1}。", type.ToString(), result.ResultMessage)));
                        }
                    }
                    else
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("未找到App类型:{0}对应的推送配置信息。", type.ToString())));
                    }
                }
                return(Request.CreateResponse <PushResult>(HttpStatusCode.OK, new PushResult()
                {
                    IsCallPushError = false,
                    ResultMessage = "调用推送接口成功。"
                }));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
            }
        }