极光推送V3版本,V2版本代码 下载本文

内容发布更新时间 : 2024/11/16 21:26:14星期一 下面是文章的全部内容请认真阅读。

使用极光推送(www.jpush.cn)向安卓手机推送消息【服务端向客

户端主送推送】,C#语言

在VisualStudio2010中新建网站JPushAndroid。添加引用json帮助类库Newtonsoft.Json.dll。 在web.config增加appkey和mastersecret,可以在极光官网web.config源码:

www.jpush.cn申请。

添加类JPushV3,会弹出保存类在App_code文件夹下,确定。

JpushV3代码如下:

using System;

using System.Collections.Generic; using System.Linq; using System.Web; using System.Net; using System.Text; using System.IO;

using System.Configuration; using System.Collections; /*

* 参考文档:http://docs.jpush.cn/display/dev/Index 选择里面的:服务器端 API,Push-API-V3 *

* 极光推送的网站的网址是:https://www.jpush.cn/

* 旧版本V2 http://docs.jpush.cn/display/dev/Push+API+v2 * 最新版本V3 http://docs.jpush.cn/display/dev/Push-API-v3

*

* 其中服务端的接口以及示例代码都在这里:http://docs.jpush.cn/display/dev/Server-SDKs */

///

/// 极光推送的最新版:PUSH-API-V3

/// 参考地址 http://docs.jpush.cn/display/dev/Push-API-v3 /// POST https://api.jpush.cn/v3/push ///

public class JPushV3 {

///

/// 应用标识:极光推送的用户名 ///

private readonly string AppKey = ConfigurationManager.AppSettings[\]; ///

/// 极光推送的密码 ///

private readonly string MasterSecret = ConfigurationManager.AppSettings[\]; ///

/// 极光推送请求的url地址 ///

private readonly string RequestUrl = \; ///

/// 查询推送结果请求的Url地址 ///

private readonly string ReceivedUrl = \;

///

/// 发送推送请求到JPush,使用HttpWebRequest ///

/// ///

///

/// ///

public string SendRequest(String method, String url, String auth, String reqParams) {

string resultJson = \;

HttpWebRequest myReq = null; HttpWebResponse response = null; try

{

myReq = (HttpWebRequest)WebRequest.Create(url); myReq.Method = method;

myReq.ContentType = \; if (!String.IsNullOrEmpty(auth)) {

myReq.Headers.Add(\, \ + auth); }

if (method == \) {

byte[] bs = UTF8Encoding.UTF8.GetBytes(reqParams); myReq.ContentLength = bs.Length;

using (Stream reqStream = myReq.GetRequestStream()) {

reqStream.Write(bs, 0, bs.Length); reqStream.Close(); } }

response = (HttpWebResponse)myReq.GetResponse(); HttpStatusCode statusCode = response.StatusCode;

if (Equals(response.StatusCode, HttpStatusCode.OK)) {

using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8)) {

resultJson = reader.ReadToEnd(); try {

object json = Newtonsoft.Json.JsonConvert.DeserializeObject(resultJson); } catch {

resultJson = string.Format(\10086}}}}\, \响应的结果不是正确的json格式\); }

} } }

catch (WebException ex) {

if (ex.Status == WebExceptionStatus.ProtocolError) {

HttpStatusCode errorCode = ((HttpWebResponse)ex.Response).StatusCode;