.net平台借助第三方推送服务在推送Android,IOS消息(极光推送_V3版本)最新

最近刚从极光推送官网上看到V2版本要停用,不得不有重新写V3版本的。这里用到了 HTTP Basic Authentication

http://www.cnblogs.com/pingming/p/4165057.html

1、首先需要将你的app在极光官网上进行注册,获取一个ApiKey,一个APIMasterSecret(密码),将这两个值保存在配置文件(app/web.config)中,具体手机开发端需要做什么操作我们.net平台不管

<appSettings>
    <add key="ApiKey" value="**********"/>
    <add key="APIMasterSecret" value="*******"/>
 </appSettings>

2、读取配置中的值

private readonly string ApiKey = "";
private readonly string APIMasterSecret = "";
ApiKey = ConfigurationManager.AppSettings["ApiKey"].ToString();//Android ApiKey
APIMasterSecret = ConfigurationManager.AppSettings["APIMasterSecret"].ToString();//Android密码

 

3、开始推送方法

/// <summary>
        /// 极光推送V3版本
        /// </summary>
        /// <param name="content"></param>
        public string PushV3(string content)
        {
            string app_key = ApiKey;
            string masterSecret = APIMasterSecret;

            string u_ = app_key + ":" + masterSecret;//对应推送 -u

            string base64_ = EncodeBase64(u_);//编码 u_

            string postData = "";
            postData += "{";
            postData += "\"platform\": \"all\",";
            postData += "\"audience\" : \"all\"";
            postData += ",";
            postData += "\"notification\" : {";

            postData += "\"android\" : {";
            postData += "\"alert\" : \"" + content + "\",";
            postData += "\"title\":\"Send to Android\",";
            postData += "\"builder_id\":1,";
            postData += "\"extras\" : { \"newsid\" : 321}";

            postData += "}, ";
            postData += "\"ios\" : {";
            postData += "\"alert\" : \"" + content + "\",";
            postData += "\"sound\":\"default\",";
            postData += "\"badge\":\"+1\",";
            postData += "\"extras\" : { \"newsid\" : 321}";
            postData += "}";
            postData += "},";
            postData += "\"options\" : {";
            postData += "\"time_to_live\" : 60,\"apns_production\":false";
            postData += "}";
            postData += "}";


            byte[] data = Encoding.UTF8.GetBytes(postData);

            //使用 HTTP  Basic Authentication 的方式做访问授权
//http Post方式调用极光的推送服务
 
            Uri url = new Uri("https://api.jpush.cn/v3/push");
            CredentialCache mycache = new CredentialCache();
            mycache.Add(url, "Basic", new NetworkCredential(app_key, masterSecret));

            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);//

            myRequest.Method = "POST";//极光http请求方式为post
            myRequest.ContentType = "application/json";////按照极光的要求
            myRequest.ContentLength = data.Length;
            myRequest.Credentials = mycache;
            myRequest.KeepAlive = true;
            myRequest.Headers.Add("Authorization", "Basic "+base64_);//http头添加
            Stream newStream = myRequest.GetRequestStream();

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

            // Get response
            var response = (HttpWebResponse)myRequest.GetResponse();
            string staCode= response.StatusCode.ToString();//返回状态码:200 OK
            using (var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")))
            {
                string result = reader.ReadToEnd();
                reader.Close();
                response.Close();
                return staCode;
            }
        }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。