/// <summary>
        /// 获取设备标签
        /// </summary>
        /// <param name="registrationId"></param>
        /// <returns></returns>
        public string[] getDeviceTags(string registrationId)
        {
            if (string.IsNullOrEmpty(registrationId))
            {
                throw new ApiException(Commons.HTTP_STATUS_400, -1, "registrationId is null");
            }
            string path = MobPushConfig.deviceUrl + "/tags/" + registrationId;

            try
            {
                MobResult mr = Commons.WebClientGet(path);
                if (mr == null || mr.res == null)
                {
                    return(null);
                }
                string res = mr.res.ToString();
                Tags   t   = JsonExtension.FromJSON <Tags>(res);
                if (t == null)
                {
                    return(null);
                }
                return(t.tags);
            }
            catch (ApiException api)
            {
                throw api;
            }
        }
        /// <summary>
        /// 清空设备标签 -- (仅200表示成功)
        /// </summary>
        /// <param name="registrationId"></param>
        /// <returns></returns>
        public int cleanDeviceTags(string registrationId)
        {
            if (string.IsNullOrEmpty(registrationId))
            {
                throw new ApiException(Commons.HTTP_STATUS_400, -1, "registrationId is null");
            }
            String path = MobPushConfig.deviceUrl + "/tags";
            Tags   t    = new Tags(registrationId, 3);

            try
            {
                MobResult mr = Commons.WebClientPost(path, JsonExtension.ToJSON(t));
                return(mr.status);
            }
            catch (ApiException api)
            {
                throw api;
            }
        }
 /// <summary>
 /// 获取推送任务统计信息
 /// </summary>
 /// <param name="path"></param>
 /// <returns></returns>
 private PushStats pullStats(String path)
 {
     try
     {
         MobResult result = Commons.WebClientGet(path);
         if (result != null)
         {
             if (result.res == null)
             {
                 return(null);
             }
             PushStats stats = JsonExtension.FromJSON <PushStats>(result.res.ToJSON());
             return(stats);
         }
         return(null);
     }
     catch (ApiException api)
     {
         throw api;
     }
 }
 /// <summary>
 /// 获取推送详情基础
 /// </summary>
 /// <param name="path"></param>
 /// <returns></returns>
 private PushWork pullPush(string path)
 {
     try
     {
         MobResult result = Commons.WebClientGet(path);
         if (result != null)
         {
             if (result.res == null)
             {
                 return(null);
             }
             PushWork work = JsonExtension.FromJSON <PushWork>(result.res.ToJSON());
             return(work);
         }
         return(null);
     }
     catch (ApiException api)
     {
         throw api;
     }
 }
        /// <summary>
        /// 绑定设备别名,如果存在则覆盖原有别名 -- (仅200表示成功)
        /// </summary>
        /// <param name="alias"></param>
        /// <param name="registrationId"></param>
        /// <returns></returns>
        public int setDeviceAlias(string alias, string registrationId)
        {
            if (string.IsNullOrEmpty(registrationId))
            {
                throw new ApiException(Commons.HTTP_STATUS_400, -1, "registrationId is null");
            }
            if (string.IsNullOrEmpty(alias))
            {
                throw new ApiException(Commons.HTTP_STATUS_400, -1, "alias is null");
            }
            String path = MobPushConfig.deviceUrl + "/alias";
            Alias  a    = new Alias(registrationId, alias);

            try {
                MobResult mr = Commons.WebClientPost(path, JsonExtension.ToJSON(a));
                return(mr.status);
            }
            catch (ApiException api)
            {
                throw api;
            }
        }
Example #6
0
        /// <summary>
        /// 获取地理位置列表 -- 子级列表
        /// 如果查询最上级则传入null即可
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public List <Area> getArea(string parentId)
        {
            if (string.IsNullOrEmpty(parentId))
            {
                parentId = "0";
            }
            string path = MobPushConfig.baseUrl + "/area/" + parentId;

            try
            {
                MobResult mr = Commons.WebClientGet(path);
                if (mr == null || mr.res == null)
                {
                    return(new List <Area>());
                }
                string      res  = mr.res.ToString();
                List <Area> list = JsonExtension.DeserializeJsonToList <Area>(res);
                return(list);
            }
            catch (ApiException api)
            {
                throw api;
            }
        }
        /// <summary>
        /// HTTP POST 请求封装,未知异常则responseCode = 400,消息错误码 = -1
        /// </summary>
        /// <param name="url"></param>
        /// <param name="json"></param>
        /// <returns></returns>
        public static MobResult WebClientPost(string url, string json)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ApiException(HTTP_STATUS_400, -1, "appkey or appSecret is null,Please go to MobPushConfig to set them");
            }
            HttpWebResponse webRespon    = null;
            StreamReader    streamReader = null;

            try
            {
                string sign = GetMD5String(json, MobPushConfig.appSecret);
                WebHeaderCollection header = new WebHeaderCollection();
                header.Add("key", MobPushConfig.appkey);
                header.Add("sign", sign);
                byte[]         data           = Encoding.UTF8.GetBytes(json);
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                httpWebRequest.Method        = "POST";
                httpWebRequest.ContentType   = "application/json; charset=UTF-8";
                httpWebRequest.ContentLength = data.Length;
                httpWebRequest.Headers       = header;
                Stream newStream = httpWebRequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();
                try
                {
                    webRespon = (HttpWebResponse)httpWebRequest.GetResponse();
                }
                catch (WebException ex)
                {
                    webRespon = (HttpWebResponse)ex.Response;
                }
                Stream webStream = webRespon.GetResponseStream();
                if (webStream == null)
                {
                    throw new ApiException(HTTP_STATUS_400, -1, "Network error");
                }
                int statsCode = (int)webRespon.StatusCode;

                streamReader = new StreamReader(webStream, Encoding.UTF8);
                string    responseContent = streamReader.ReadToEnd();
                MobResult r = JsonExtension.FromJSON <MobResult>(responseContent);
                if (HTTP_STATUS_200 != statsCode)
                { // 如果http请求响应非200,则异常
                    throw new ApiException(statsCode, r.status, r.error);
                }
                return(r);
            }
            catch (ApiException apiEx)
            {
                throw apiEx;
            }
            catch (Exception ex)
            {
                throw new ApiException(HTTP_STATUS_400, -1, ex.Message);
            }
            finally
            {
                if (webRespon != null)
                {
                    webRespon.Close();
                }
                if (streamReader != null)
                {
                    streamReader.Close();
                }
            }
        }
        /// <summary>
        /// HTTP GET 请求封装,未知异常则responseCode = 400,消息错误码 = -1
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static MobResult WebClientGet(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ApiException(HTTP_STATUS_400, -1, "appkey or appSecret is null,Please go to MobPushConfig to set them");
            }
            HttpWebResponse webRespon    = null;
            StreamReader    streamReader = null;

            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method      = "GET";
                httpWebRequest.AllowWriteStreamBuffering = false;
                httpWebRequest.Timeout = 15000;
                httpWebRequest.ServicePoint.Expect100Continue = false;

                httpWebRequest.Headers.Add("key", MobPushConfig.appkey);
                string sign = GetMD5String(null, MobPushConfig.appSecret);
                httpWebRequest.Headers.Add("sign", sign);
                try
                {
                    webRespon = (HttpWebResponse)httpWebRequest.GetResponse();
                }
                catch (WebException ex)
                {
                    webRespon = (HttpWebResponse)ex.Response;
                }
                Stream webStream = webRespon.GetResponseStream();
                if (webStream == null)
                {
                    throw new ApiException(HTTP_STATUS_400, -1, "Network error");
                }
                int statsCode = (int)webRespon.StatusCode;

                streamReader = new StreamReader(webStream, Encoding.UTF8);
                string    responseContent = streamReader.ReadToEnd();
                MobResult r = JsonExtension.FromJSON <MobResult>(responseContent);
                if (HTTP_STATUS_200 != statsCode)
                { // 如果http请求响应非200,则异常
                    throw new ApiException(statsCode, r.status, r.error);
                }
                return(r);
            }
            catch (ApiException apiEx)
            {
                throw apiEx;
            }
            catch (Exception ex)
            {
                throw new ApiException(HTTP_STATUS_400, -1, ex.Message);
            }
            finally {
                if (webRespon != null)
                {
                    webRespon.Close();
                }
                if (streamReader != null)
                {
                    streamReader.Close();
                }
            }
        }
        /// <summary>
        /// 发送推送 -- 返回MobPush唯一标识
        /// </summary>
        /// <param name="pushWork"></param>
        /// <returns></returns>
        public string sendPush(PushWork pushWork)
        {
            if (pushWork == null)
            {
                throw new ApiException(Commons.HTTP_STATUS_400, -1, "pushWork is null");
            }
            pushWork.appkey = MobPushConfig.appkey;
            if (string.IsNullOrEmpty(pushWork.content))
            {
                throw new ApiException(Commons.HTTP_STATUS_400, -1, "content is null");
            }
            if (pushWork.target == null)
            {
                throw new ApiException(Commons.HTTP_STATUS_400, -1, "target is null");
            }
            if (pushWork.type == null)
            {
                throw new ApiException(Commons.HTTP_STATUS_400, -1, "type is null");
            }
            if (pushWork.plats == null)
            {
                throw new ApiException(Commons.HTTP_STATUS_400, -1, "plats is null");
            }
            if (pushWork.unlineTime == null)
            {//默认保留时间
                pushWork.unlineTime = 1;
            }
            if (pushWork.plats.Contains(1))
            { // Android默认参数
                if (pushWork.androidstyle == null)
                {
                    pushWork.androidstyle = (int?)AndroidNotifyStyleEnum.normal;
                }
            }
            if (pushWork.plats.Contains(2))
            { // IOS 的默认参数
                if (pushWork.iosBadge == null)
                {
                    pushWork.iosBadge = 1;
                }
                if (string.IsNullOrEmpty(pushWork.iosSound))
                {
                    pushWork.iosSound = "default";
                }
                if (pushWork.iosProduction == null)
                {
                    pushWork.iosProduction = 1;
                }
            }
            string path = MobPushConfig.pushUrl + "/v2/push";

            try
            {
                MobResult mr = Commons.WebClientPost(path, JsonExtension.ToJSON(pushWork));

                pushWork = JsonExtension.FromJSON <PushWork>(mr.res.ToJSON());

                return(pushWork.batchId);
            }
            catch (ApiException api)
            {
                throw api;
            }
        }