示例#1
0
        /// <summary>
        /// 推送消息到给定的一组设备(批量单播)
        /// </summary>
        /// <param name="opts">
        ///channel_ids      string  是   一组channel_id(最多为一万个)组成的json数组字符串 对应一批设备
        ///msg_type         number  否   取值如下:0:消息;1:通知。默认为0 消息类型
        ///msg              string  是   详情见消息/通知数据格式 消息内容,json格式
        ///msg_expires      number  否   1~86400,默认为1天(86400秒) 消息过期时间,单位为秒
        ///topic_id         string  是   字母、数字及下划线组成,长度限制为1~128 分类主题名称
        /// </param>
        /// <returns></returns>
        public string PushBatchDevice(PushOptions opts)
        {
            string postMethod = "push/batch_device";
            var    postStr    = GetPushParameter(opts, postMethod);
            string result     = requestUrl(postStr, postMethod);

            return(result);
        }
示例#2
0
        /// <summary>
        /// 创建标签组
        /// </summary>
        /// <param name="opts">
        /// tag     string  是   1~128字节,但不能为‘default’ 标签名称
        /// </param>
        /// <returns></returns>
        public string AppCreateTag(PushOptions opts)
        {
            string postMethod = "app/create_tag";
            var    postStr    = GetPushParameter(opts, postMethod);
            string result     = requestUrl(postStr, postMethod);

            return(result);
        }
示例#3
0
        //public string PushMessage(PushOptions opts)
        //{

        //    var postStr = GetPushParameter(opts);
        //    byte[] data = Encoding.UTF8.GetBytes(postStr);//编码,尤其是汉字,事先要看下抓取网页的编码方式
        //    WebClient webClient = new WebClient();
        //    try
        //    {
        //        webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可
        //        webClient.Headers.Add("User-Agent", "BCCS_SDK/3.0 (Windows 7 Ultimate ) C#/4.0 (Baidu Push SDK for PHP v4.0) .net framework/4.0 ZEND/2.6.0");

        //        byte[] responseData = webClient.UploadData(this.url, "POST", data);//得到返回字符流
        //        string srcString = Encoding.UTF8.GetString(responseData);//解码
        //        return "Post:" + postStr + "\r\n\r\n" + "Response:" + srcString;
        //    }
        //    catch (WebException ex)
        //    {
        //        Stream stream = ex.Response.GetResponseStream();
        //        string m = ex.Response.Headers.ToString();
        //        byte[] buf = new byte[256];
        //        stream.Read(buf, 0, 256);
        //        stream.Close();
        //        int count = 0;
        //        foreach (var b in buf)
        //        {
        //            if (b > 0)
        //            {
        //                count++;
        //            }
        //            else
        //            {
        //                break;
        //            }
        //        }
        //        return " Post:" + postStr + ex.Message + "\r\n\r\n" + Encoding.UTF8.GetString(buf, 0, count);
        //    }
        //}

        /// <summary>
        /// 获取参数字符串
        /// </summary>
        /// <param name="opts"></param>
        /// <returns></returns>
        private string GetPushParameter(PushOptions opts, string method)
        {
            string url = string.Format("{0}{1}", this.url, method);
            Dictionary <string, string> dic = new Dictionary <string, string>();
            //将键值对按照key的升级排列
            var props = typeof(PushOptions).GetProperties().OrderBy(p => p.Name);

            foreach (var p in props)
            {
                if (p.GetValue(opts, null) != null)
                {
                    dic.Add(p.Name, p.GetValue(opts, null).ToString());
                }
            }
            //生成sign时,不能包含sign标签,所有移除
            dic.Remove("sign");
            var preData = new StringBuilder();

            foreach (var l in dic)
            {
                preData.Append(l.Key);
                preData.Append("=");
                preData.Append(l.Value);
            }
            //按要求拼接字符串,并urlencode编码
            var str          = HttpUtility.UrlEncode(this.httpMehtod.ToUpper() + url + preData.ToString() + this.secret_key, System.Text.Encoding.UTF8);
            var strSignUpper = new StringBuilder();
            int perIndex     = 0;

            //字符串中百分号后面两位转换为大写
            for (int i = 0; i < str.Length; i++)
            {
                var c = str[i].ToString();
                if (str[i] == '%')
                {
                    perIndex = i;
                }
                if (i - perIndex == 1 || i - perIndex == 2)
                {
                    c = c.ToUpper();
                }
                strSignUpper.Append(c);
            }
            strSignUpper = strSignUpper.Replace("(", "%28").Replace(")", "%29");
            var sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSignUpper.ToString(), "MD5").ToLower();

            //加入生成好的sign键值对
            dic.Add("sign", sign);
            var strb = new StringBuilder();

            //int tagIndex = 0;
            foreach (var l in dic)
            {
                strb.Append(l.Key);
                strb.Append("=");
                strb.Append(l.Value);
                strb.Append("&");
            }
            var postStr = strb.ToString().EndsWith("&") ? strb.ToString().Remove(strb.ToString().LastIndexOf('&')) : strb.ToString();

            return(postStr);
        }
示例#4
0
        protected void BtnSend_Click(object sender, EventArgs e)
        {
            try
            {
                string secretKey = PubSql.SecretKey;
                string apiKey    = PubSql.ApiKey;


                CodeBehind.BaiduPush Bpush = new CodeBehind.BaiduPush("POST", secretKey);
                String   messages          = "";
                String   method            = "push_msg";
                TimeSpan ts          = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
                uint     device_type = 3;
                uint     unixTime    = (uint)ts.TotalSeconds;

                uint   message_type;
                string messageksy = "xxxxxx";
                if (RbMessage.Checked)
                {
                    message_type = 0;
                    messages     = TBMessage.Text;
                }
                else
                {
                    message_type = 1;

                    if (RBIOSPRO.Checked == true || RBIOSDEV.Checked == true)
                    {
                        device_type = 4;
                        IOSNotification notification = new IOSNotification();
                        notification.title       = TBTitle.Text;
                        notification.description = TBDescription.Text;
                        messages = notification.getJsonString();
                    }
                    else
                    {
                        BaiduPushNotification notification = new BaiduPushNotification();
                        notification.title       = TBTitle.Text;
                        notification.description = TBDescription.Text;
                        messages = notification.getJsonString();
                    }
                }


                PushOptions pOpts;
                if (RBUnicast.Checked)
                {
                    pOpts = new PushOptions(method, apiKey, TBUserId.Text, TBChannelID.Text, device_type, messages,
                                            messageksy, unixTime);
                }
                else if (RBMulticast.Checked)
                {
                    pOpts = new PushOptions(method, apiKey, TBTag.Text, device_type, messages, messageksy, unixTime);
                }
                else
                {
                    pOpts = new PushOptions(method, apiKey, device_type, messages, messageksy, unixTime);
                }

                pOpts.message_type = message_type;
                if (RBIOSPRO.Checked == true)
                {
                    pOpts.deploy_status = 2;
                }
                else if (RBIOSDEV.Checked == true)
                {
                    pOpts.deploy_status = 1;
                }


                string response = Bpush.PushMessage(pOpts);

                TextBoxResponse.Text = response;
            }
            catch (Exception ex)
            {
                TextBoxResponse.Text = "Exception caught sending update: " + ex.ToString();
            }
        }