.net平台藉助第三方推送服務在推送Android,IOS訊息(極光推送_V3版本)最新

來源:互聯網
上載者:User

標籤: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 ApiKeyAPIMasterSecret = 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版本)最新

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.