C#推送安卓—极光推送

版权声明:本文为博主原创文章,需要转载尽管转载。 https://blog.csdn.net/z5976749/article/details/50020267

先到极光推送官网注册一个帐号:

https://www.jpush.cn/common/products

登录后创建应用输入应用名和包名(推送根据包名),创建成功后把appkey和secret复制到

        private readonly string ApiKey = ConfigurationManager.AppSettings["ApiKey"].ToString(); 
        private readonly string APIMasterSecret = ConfigurationManager.AppSettings["APIMasterSecret"].ToString(); 
推送代码:

receiver_type指定推送方式。3是别名,4是广播。

官方案例:http://docs.jpush.io/resources/#sdk_1

/// <summary>
        /// Android极光推送
        /// </summary>
        /// <param name="RegistrationID">设备号</param>
        public string PushAndroid(string RegistrationID, string value)
        {
            try
            {
                Random ran = new Random();
                int sendno = ran.Next(1, 2100000000);//随机生成的一个编号
                string app_key = ApiKey;
                string masterSecret = APIMasterSecret;
                int receiver_type = 3;//接收者类型。2、指定的 tag。3、指定的 alias。4、广播:对 app_key 下的所有用户推送消息。5、根据 RegistrationID 进行推送。当前只是 Android SDK r1.6.0 版本支持
                string receiver_value = RegistrationID;

                int msg_type = 1;//1、通知2、自定义消息(只有 Android 支持)
                string msg_content = "{\"n_builder_id\":\"00\",\"n_title\":\"" + "xxx" + "\",\"n_content\":\"" + value + "\"}";//消息内容
                string platform = "android";//目标用户终端手机的平台类型,如: android, ios 多个请使用逗号分隔。
                string verification_code = GetMD5Str(sendno.ToString(), receiver_type.ToString(), receiver_value, masterSecret);//验证串,用于校验发送的合法性。MD5
                string postData = "sendno=" + sendno;
                postData += ("&app_key=" + app_key);
                postData += ("&masterSecret=" + masterSecret);
                postData += ("&receiver_type=" + receiver_type);
                postData += ("&receiver_value=" + receiver_value);
                postData += ("&msg_type=" + msg_type);
                postData += ("&msg_content=" + msg_content);
                postData += ("&platform=" + platform);
                postData += ("&verification_code=" + verification_code);

                //byte[] data = encoding.GetBytes(postData);
                byte[] data = Encoding.UTF8.GetBytes(postData);
                string resCode = GetPostRequest(data);//调用极光的接口获取返回值
                return resCode;
                //JpushMsg msg = Newtonsoft.Json.JsonConvert.DeserializeObject<JpushMsg>(resCode);//定义一个JpushMsg类,包含返回值信息,将返回的json格式字符串转成JpushMsg对象           
            }
            catch (Exception ex)
            {
                return "{\"errmsg\":\"Wrong\"}";
            }
        }

/// <summary>
        /// MD5字符串
        /// </summary>
        /// <param name="paras">参数数组</param>
        /// <returns>MD5字符串</returns>
        public string GetMD5Str(params string[] paras)
        {
            string str = "";
            for (int i = 0; i < paras.Length; i++)
            {
                str += paras[i];
            }
            byte[] buffer = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(str));
            string md5Str = string.Empty;
            for (int i = 0; i < buffer.Length; i++)
            {
                md5Str = md5Str + buffer[i].ToString("X2");
            }
            return md5Str;
        }

        /// <summary>
        /// Post方式请求获取返回值
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string GetPostRequest(byte[] data)
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://api.jpush.cn:8800/v2/push");

            myRequest.Method = "POST";//极光http请求方式为post
            myRequest.ContentType = "application/x-www-form-urlencoded";//按照极光的要求
            myRequest.ContentLength = data.Length;
            Stream newStream = myRequest.GetRequestStream();

            // Send the data.
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            // Get response
            var response = (HttpWebResponse)myRequest.GetResponse();
            using (var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")))
            {
                string result = reader.ReadToEnd();
                reader.Close();
                response.Close();
                return result;
            }
        }


猜你喜欢

转载自blog.csdn.net/z5976749/article/details/50020267