码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-12-15 16:50:12      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   ar   color   os   使用   

最近刚从极光推送官网上看到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;
            }
        }

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

标签:android   style   blog   http   io   ar   color   os   使用   

原文地址:http://www.cnblogs.com/pingming/p/4165027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!