.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_
StringBuilder sbs = new StringBuilder();
sbs.Append("{");
sbs.Append("\"platform\": \"all\",");
sbs.Append("\"audience\": \"all\",");
sbs.Append("\"notification\": {");
sbs.Append("\"android\": {");
sbs.Append("\"alert\": \"" + content + "\",");
sbs.Append("\"title\": \"武林国小\",");
sbs.Append("\"builder_id\": 1,");
sbs.Append("\"extras\": {");
sbs.Append("\"newsid\": 321");
sbs.Append("}");
sbs.Append(" },");
sbs.Append("\"ios\": {");
sbs.Append("\"alert\": \"" + content + "\",");
sbs.Append("\"sound\": \"default\",");
sbs.Append("\"badge\": \"+1\",");
sbs.Append("\"extras\": {");
sbs.Append("\"news_id\": 134");
sbs.Append(" }");
sbs.Append(" }");
sbs.Append("},");
sbs.Append("\"message\": {");
sbs.Append("\"msg_content\": \"" + content + "\",");
sbs.Append("\"content_type\": \"text\",");
sbs.Append("\"title\": \"武林国小\",");
sbs.Append("\"extras\": {");
sbs.Append("\"key\": \"value\"");
sbs.Append("}");
sbs.Append("},");
sbs.Append("\"sms_message\":{");
sbs.Append("\"content\":\"" + content + "\",");
sbs.Append("\"delay_time\":3600");
sbs.Append("},");
sbs.Append("\"options\": {");
sbs.Append("\"time_to_live\": 60,");
sbs.Append("\"apns_production\": true");
sbs.Append("}");
sbs.Append("}");
byte[] data = Encoding.UTF8.GetBytes(sbs.ToString());
//使用 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; } }