ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:52.82KB ,
资源ID:6508941      下载积分:10 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/6508941.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(极光推送V3版本,V2版本代码.docx)为本站上传会员【xrp****65】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

极光推送V3版本,V2版本代码.docx

1、使用极光推送()向安卓手机推送消息【服务端向客户端主送推送】,C#语言 在VisualStudio2010中新建网站JPushAndroid。添加引用json帮助类库Newtonsoft.Json.dll。 在web.config增加appkey和mastersecret,可以在极光官网申请。web.config源码:

2、"b232c57153afc71ed74ae3da"/> 添加类JPushV3,会弹出保存类在App_code文件夹下,确定。 JpushV3代码如下: using System; using System.

3、Collections.Generic; using System.Linq; using System.Web; using System.Net; using System.Text; using System.IO; using System.Configuration; using System.Collections; /* * 参考文档: 选择里面的:服务器端 API,Push-API-V3 * * 极光推送的网站的网址是: * 旧版本V2 * 最新版本V3 * * 其中服务端的接口以及示例代码都在这里: */

4、 ///

/// 极光推送的最新版:PUSH-API-V3 /// 参考地址 /// POST /// public class JPushV3 { /// /// 应用标识:极光推送的用户名 /// private readonly string AppKey = ConfigurationManager.AppSettings["AppKey"]; /// /// 极光推送的密码 ///

5、ary> private readonly string MasterSecret = ConfigurationManager.AppSettings["MasterSecret"]; ///

/// 极光推送请求的url地址 /// private readonly string RequestUrl = " /// /// 查询推送结果请求的Url地址 /// private readonly string Rec

6、eivedUrl = " ///

/// 发送推送请求到JPush,使用HttpWebRequest /// /// 传入POST或GET /// 固定地址 /// 用户名AppKey和密码MasterSecret形成的Base64字符串 /// 请求的json参数,一般

7、由Platform(平台)、Audience(设备对象标识)、Notification(通知)、Message(自定义消息)、Options(推送可选项)组成 /// public string SendRequest(String method, String url, String auth, String reqParams) { string resultJson = ""; HttpWebRequest myReq = null;

8、 HttpWebResponse response = null; try { myReq = (HttpWebRequest)WebRequest.Create(url); myReq.Method = method; myReq.ContentType = "application/json"; if (!String.IsNullOrEmpty(auth)) { myReq.Header

9、s.Add("Authorization", "Basic " + auth); } if (method == "POST") { byte[] bs = UTF8Encoding.UTF8.GetBytes(reqParams); myReq.ContentLength = bs.Length; using (Stream reqStream = myReq.GetRequestStream())

10、 { reqStream.Write(bs, 0, bs.Length); reqStream.Close(); } } response = (HttpWebResponse)myReq.GetResponse(); HttpStatusCode statusCode = response.StatusCode; if (Equ

11、als(response.StatusCode, HttpStatusCode.OK)) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8)) { resultJson = reader.ReadToEnd(); try

12、{ object json = Newtonsoft.Json.JsonConvert.DeserializeObject(resultJson); } catch { resultJson = string.Format("{{\"error\": {{\"message\": \"{0}\", \"code\": 10086}}}}", "响应的结果不是正确的jso

13、n格式"); } } } } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) { HttpStatusCode errorCode = ((HttpWebResponse)ex.Response).StatusCode;

14、 string statusDescription = ((HttpWebResponse)ex.Response).StatusDescription; using (StreamReader sr = new StreamReader(((HttpWebResponse)ex.Response).GetResponseStream(), System.Text.Encoding.UTF8)) { resultJson = sr.ReadToEnd()

15、 //{"errcode":404,"errmsg":"request api doesn't exist"} Dictionary dict = JsonToDictionary(resultJson); string errCode = "10086"; string errMsg = "发送推送的请求地址不存在或无法连接"; if (dict.Co

16、ntainsKey("errcode")) { errCode = dict["errcode"].ToString(); } if (dict.ContainsKey("errmsg")) { errMsg = dict["errmsg"].ToString(); }

17、 resultJson = string.Format("{{\"error\": {{\"message\": \"{0}\", \"code\": {1}}}}}", errMsg, errCode); } } else { //这里一定是error作为键名(自定义错误号10086),和极光推送失败时的json格式保持一致 如 {"error": {"message": "Missing parameter", "cod

18、e": 1002}} resultJson = string.Format("{{\"error\": {{\"message\": \"{0}\", \"code\": 10086}}}}", ex.Message.Replace("\"", " ").Replace("'", " ")); } } catch (System.Exception ex) { resultJson = string.Format("{{\"error\": {{\"mes

19、sage\": \"{0}\", \"code\": 10086}}}}", ex.Message.Replace("\"", " ").Replace("'", " ")); } finally { if (response != null) { response.Close(); } if (myReq != null) { myReq.Ab

20、ort(); } } return resultJson; } ///

/// 通过用户名AppKey和密码获取验证码 /// /// private string GetBase64Auth() { string str = this.AppKey + ":" + this.MasterSecret; byte[] bytes = Enco

21、ding.Default.GetBytes(str); return Convert.ToBase64String(bytes); } ///

/// 发送推送请求到JPush /// /// POST或GET /// 请求的json参数,一般由Platform(平台)、Audience(设备对象标识)、Notification(通知)、Message(自定义消息)、

22、Options(推送可选项)组成 /// public string SendRequest(String method, String reqParams) { string auth = GetBase64Auth(); return SendRequest(method, this.RequestUrl, auth, reqParams); } ///

/// 发送Post请求 ///

23、y> /// 请求的json参数,一般由Platform(平台)、Audience(设备对象标识)、Notification(通知)、Message(自定义消息)、Options(推送可选项)组成 /// public string SendPostRequest(String reqParams) { string auth = GetBase64Auth(); return SendRequest("POST",

24、 this.RequestUrl, auth, reqParams); } ///

/// 发送Get请求 /// /// 请求的json参数,一般由Platform(平台)、Audience(设备对象标识)、Notification(通知)、Message(自定义消息)、Options(推送可选项)组成 /// public string SendGetRequest(Str

25、ing reqParams) { string auth = GetBase64Auth(); return SendRequest("GET", this.RequestUrl, auth, reqParams); } /* * 生成唯一的sendNo的方法: 取序列号 * 查看返回结果的方法 */ ///

/// 查询推送的结果 /// /// 生成的json

26、信息唯一id /// public string GetReceivedResult(String msg_ids) { string url = this.ReceivedUrl + "?msg_ids=" + msg_ids; String auth = GetBase64Auth(); return SendRequest("GET", url, auth, null); ; } /* * 1.正确时返回结果{"

27、sendno":"123456","msg_id":"1799597405"} * 或者 {"sendno":"0","msg_id":"351403900"} * 2.入参json完全正确,但找不到要到达的设备。错误时:返回 * {"msg_id": 3125719446, "error": {"message": "cannot find user by this audience", "code": 1011}} * 3.传入空字符串 或者 非json格式,或者没有必须的选项:{"error": {"message": "Missing

28、 parameter", "code": 1002}} * 传入的键(键区分大小写)、值不符合要求 {"error": {"message": "Audience value must be JSON Array format!", "code": 1003}} */ ///

/// 将返回的json转换为Hashtable对象 /// /// /// pu

29、blic Hashtable JsonToHashtable(string jsonString) { /* * 正确时返回结果{"sendno":"123456","msg_id":"1799597405"} * {"sendno":"0","msg_id":"351403900"} * 入参json完全正确,但找不到要到达的设备。错误时:返回 {"msg_id": 3125719446, "error": {"message": "cannot find user by this audience"

30、 "code": 1011}} * 传入空字符串 或者 非json格式,或者没有必须的选项:{"error": {"message": "Missing parameter", "code": 1002}} * 传入的键值不符合要求 {"error": {"message": "Audience value must be JSON Array format!", "code": 1003}} 键区分大小写 */ Hashtable ht = new Hashtable(); object jso

31、n = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString); //返回的结果一定是一个json对象 Newtonsoft.Json.Linq.JObject jsonObject = json as Newtonsoft.Json.Linq.JObject; if (jsonObject == null) { return ht; } foreach (Newtonsoft.Json.Linq.J

32、Property jProperty in jsonObject.Properties()) { Newtonsoft.Json.Linq.JToken jToken = jProperty.Value; string value = ""; if (jToken != null) { value = jToken.ToString(); } ht.Add(jProperty.Name,

33、 value); } return ht; } ///

/// 根据json返回的结果判断是否推送成功 /// /// 响应的json /// 错误信息 /// 错误号 /// publi

34、c bool IsSuccess(string jsonString, out string errorMessage, out string errorCode) { Hashtable ht = JsonToHashtable(jsonString); errorMessage = ""; errorCode = ""; foreach (string key in ht.Keys) { //如果存在error键,说明推送出错 if (k

35、ey == "error") { string errJson = ht[key].ToString(); Hashtable htError = JsonToHashtable(errJson); errorMessage = htError["message"].ToString(); errorCode = htError["code"].ToString(); return false;

36、 } } return true; } ///

/// 根据返回的响应json来判断推送是否成功,成功时记录sendno与msg_id。 /// 失败时记录错误信息errorMessage、错误号errCode等 /// /// 响应的json /// 错误信息 ///

37、am name="errorCode">错误号 /// 用户自定义的推送编号(从序列号中获取),不设置则为0,成功后返回该编号 /// 极光服务器处理后返回的信息编号 /// public bool IsSuccess(string jsonString, out string errorMessage, out string errorCode, out string sendno,

38、 out string msg_id) { bool result = IsSuccess(jsonString, out errorMessage, out errorCode); Hashtable ht = JsonToHashtable(jsonString); sendno = ""; msg_id = ""; if (result) //推送成功时,只有键sendno、msg_id { sendno = ht["sendno"].ToSt

39、ring(); msg_id = ht["msg_id"].ToString(); } else //如果失败时存在msg_id键,则记录msg_id的值 { if (ht.ContainsKey("msg_id")) { msg_id = ht["msg_id"].ToString(); } } return result; } ///

40、ummary> /// 将返回的json转换为字典Dictionary对象 ///

/// /// public Dictionary JsonToDictionary(string jsonString) { Dictionary ht = new Dictionary();

41、 object json = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString); //返回的结果一定是一个json对象 Newtonsoft.Json.Linq.JObject jsonObject = json as Newtonsoft.Json.Linq.JObject; if (jsonObject == null) { return ht; } foreach (Newtonsoft.J

42、son.Linq.JProperty jProperty in jsonObject.Properties()) { Newtonsoft.Json.Linq.JToken jToken = jProperty.Value; string value = ""; if (jToken != null) { value = jToken.ToString(); } ht.Add(jPrope

43、rty.Name, value); } return ht; } } ☆JPushV2类代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using System.Text; using System.Net; using System.IO; using System.Security.Cryptography; /* *

44、参考文档: 选择里面的:服务器端 API,Push-API-V3 * * 极光推送的网站的网址是: * 旧版本V2 * 最新版本V3 * * 其中服务端的接口以及示例代码都在这里: */ ///

/// 这个版本是Push-API-v2版本,只能用到2015-12-31,建议直接使用最新版v3 /// post处理地址 :8800/v2/push /// public class JPushV2 { /// /// Android ApiKey

45、 ///

private readonly string AppKey = ConfigurationManager.AppSettings["AppKey"]; /// /// Android密码 /// private readonly string APIMasterSecret = ConfigurationManager.AppSettings["MasterSecret"]; /// /// Android极光推送

46、开始推送方法 ///

/// 设备号 RV1D41L5F1T public void PushAndroid(string RegistrationID) { try { Random ran = new Random(); int sendno = ran.Next(1, 2100000000);//随机生成的一个编号 string app_k

47、ey = AppKey; string masterSecret = APIMasterSecret; int receiver_type = 4;//接收者类型。2、指定的 tag。3、指定的 alias。4、广播:对 app_key 下的所有用户推送消息。5、根据 RegistrationID 进行推送。当前只是 Android SDK r1.6.0 版本支持 string receiver_value = RegistrationID; /* int msg_type = 1;//1、通知 2、自定义消息(只有 Android 支持) //通过json来推送消息内容。title-标题 content-推送的内容 string Title = "要推送的标题"; string Content = "要推送的内容"; string msg_content = "{\"n_builder_id\":\"00\",\"n_title\":\"" + Title + "\",\"n_content\":\"" + Content

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服