Exemplo n.º 1
0
        /// <summary>
        /// 推送消息到移动端App。
        /// </summary>
        /// <param name="billAction">本次消息对应的单据操作种类。不同的app支持的操作种类不一样。</param>
        /// <param name="paramObj"></param>
        public static void PushCore(BillAction billAction, LibMailParam paramObj)
        {
            LibMailParam param = paramObj as LibMailParam;

            try
            {
                List <PushTarget> listTarget = GetPushTarget(billAction, param.PersonId, param.To, param.CC);
                if (listTarget == null || listTarget.Count == 0)
                {
                    return;
                }
                NoticeMsg msg = new NoticeMsg()
                {
                    Message = param.Content,
                    Title   = param.Subject
                };
                PushParams pushParams = new PushParams()
                {
                    Message = msg,
                    Targets = listTarget
                };
                //调用服务接口推送
                LibAppPushService.Push(pushParams);
            }
            catch
            {
                //throw;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 调用推送服务进行推送
        /// </summary>
        /// <param name="pushParams"></param>
        /// <returns></returns>
        public static PushResult Push(PushParams pushParams)
        {
            if (MicroServicesConfig.Instance.AppPush.Enabled == false)
            {
                return(null);
            }
            if (pushParams == null || pushParams.Targets == null || pushParams.Targets.Count == 0 ||
                pushParams.Message == null || string.IsNullOrEmpty(pushParams.Message.Message))
            {
                return(null);
            }
            string url = string.Format("{0}/api/push/push", MicroServicesConfig.Instance.AppPush.BaseUrl);

            //创建HttpClient
            using (var http = new HttpClient())
            {
                //使用FormUrlEncodedContent做HttpContent
                var content  = new StringContent(JsonConvert.SerializeObject(pushParams), Encoding.UTF8, "text/json");
                var response = http.PostAsync(url, content);
                //确保HTTP成功状态值,如果不是正确的返回状态则抛出异常
                //response.Result.EnsureSuccessStatusCode();
                //await异步读取最后的JSON
                var retStr = response.Result.Content.ReadAsStringAsync();
                if (response.Result.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    return(null);
                }
                else
                {
                    return(JsonConvert.DeserializeObject <PushResult>(retStr.Result));
                }
            }
        }
Exemplo n.º 3
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));
            }
        }
Exemplo n.º 4
0
        public PushResult Push(PushParams pushParams)
        {
            if (pushParams == null || pushParams.Message == null || pushParams.Targets == null || pushParams.Targets.Count == 0)
            {
                throw new Exception("推送参数为空。");
            }
            if (pushParams.Targets.First() == null)
            {
                throw new Exception("推送目标为空。");
            }
            //入口参数中的Target都是同一个app类型的推送目标了。
            AppPushChannelInfo channelInfo = null;

            if (EnvProvider.Default.DicAppPushInfo.TryGetValue((AppType)pushParams.Targets.First().AppType, out channelInfo) == false || channelInfo == null)
            {
                throw new Exception(string.Format("App类型:{0}对应的推送通道信息为空。", pushParams.Targets.First().AppType));
            }

            try
            {
                IGtPush              push     = new IGtPush(HOST, channelInfo.AppKey, channelInfo.Secret);
                ListMessage          message  = new ListMessage();
                TransmissionTemplate template = TransmissionTemplateDemo(channelInfo, pushParams.Message);
                message.IsOffline         = true;
                message.OfflineExpireTime = 1000 * 3600 * 12;
                message.Data = template;

                List <Target> targetList = new List <Target>();
                for (int i = 0; i < pushParams.Targets.Count; i++)
                {
                    Target target = new Target();
                    target.appId    = channelInfo.AppId;
                    target.clientId = pushParams.Targets[i].ClientId;
                    targetList.Add(target);
                }
                //com.igetui.api.openservice.igetui.Target target1 = new com.igetui.api.openservice.igetui.Target();
                //target1.appId = APPID;
                //target1.clientId = clientId;

                // 如需要,可以设置多个接收者
                //com.igetui.api.openservice.igetui.Target target2 = new com.igetui.api.openservice.igetui.Target();
                //target2.appId = APPID;
                //target2.clientId = "f70befc00249c7337c15ba253e7cc391";

                //com.igetui.api.openservice.igetui.Target target3 = new com.igetui.api.openservice.igetui.Target();
                //target3.appId = APPID;
                //target3.clientId = "8a4d45c5a7319237db8493d774befb4c";

                //targetList.Add(target1);
                //targetList.Add(target2);
                //targetList.Add(target3);

                String contentId  = push.getContentId(message);
                String pushResult = push.pushMessageToList(contentId, targetList);
                System.Console.WriteLine("-----------------------------------------------");
                System.Console.WriteLine("服务端返回结果:" + pushResult);
                return(new PushResult()
                {
                    IsCallPushError = false,
                    ResultMessage = pushResult
                });
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Exemplo n.º 5
0
 public PushResult Push(PushParams pushParams)
 {
     throw new NotImplementedException();
 }