.net平台借助第三方推送服务在推送Android,IOS消息(极光推送_V3版本)最新
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了.net平台借助第三方推送服务在推送Android,IOS消息(极光推送_V3版本)最新,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2855字,纯文字阅读大概需要5分钟。
内容图文
最近刚从极光推送官网上看到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 = ""; privatereadonlystring 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;//对应推送 -ustring 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 responsevar response = (HttpWebResponse)myRequest.GetResponse(); string staCode= response.StatusCode.ToString();//返回状态码:200 OKusing (var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"))) { string result = reader.ReadToEnd(); reader.Close(); response.Close(); return staCode; } }
原文:http://www.cnblogs.com/pingming/p/4165027.html
内容总结
以上是互联网集市为您收集整理的.net平台借助第三方推送服务在推送Android,IOS消息(极光推送_V3版本)最新全部内容,希望文章能够帮你解决.net平台借助第三方推送服务在推送Android,IOS消息(极光推送_V3版本)最新所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。