.net 版本 极光推送 后台接口

HttpWebResponseUtility类

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.IO;
using System.IO.Compression;
using System.Text.RegularExpressions; /// <summary>
/// HttpWebResponseUtility 的摘要说明
/// </summary>
public class HttpWebResponseUtility
{
/// <summary>
/// 创建POST方式的HTTP请求
/// </summary>
/// <param name="url">请求的URL</param>
/// <param name="parameters">随同请求POST的参数名称及参数值字典</param>
/// <param name="timeout">请求的超时时间</param>
/// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
/// <param name="requestEncoding">发送HTTP请求时所用的编码</param>
/// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
/// <returns></returns>
public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int? timeout, string userAgent, Encoding requestEncoding, CookieCollection cookies)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
if (requestEncoding == null)
{
throw new ArgumentNullException("requestEncoding");
}
HttpWebRequest request = null;
//如果是发送HTTPS请求
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
}
else
{
request = WebRequest.Create(url) as HttpWebRequest;
}
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
//如果需要POST数据
if (!(parameters == null || parameters.Count == ))
{
StringBuilder buffer = new StringBuilder();
int i = ;
foreach (string key in parameters.Keys)
{
if (i > )
{
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
}
i++;
}
byte[] data = Encoding.UTF8.GetBytes(buffer.ToString());
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, , data.Length);
}
}
return request.GetResponse() as HttpWebResponse;
}
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受
}
}

webservice类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using System.Net;
using System.IO;
using System.Text;
using System.IO.Compression; /// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService { public WebService () { //如果使用设计的组件,请取消注释以下行
//InitializeComponent();
} [WebMethod]
public string HelloWorld() {
return "Hello World";
} public static string MD5Encrypt(string strSource)
{
return MD5Encrypt(strSource, );
} /// <summary>
/// </summary>
/// <param name="strSource">待加密字串</param>
/// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param>
/// <returns>加密后的字串</returns>
public static string MD5Encrypt(string strSource, int length)
{
byte[] bytes = Encoding.ASCII.GetBytes(strSource);
byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes);
StringBuilder sb = new StringBuilder();
switch (length)
{
case :
for (int i = ; i < ; i++)
sb.Append(hashValue[i].ToString("x2"));
break;
case :
for (int i = ; i < ; i++)
{
sb.Append(hashValue[i].ToString("x2"));
}
break;
default:
for (int i = ; i < hashValue.Length; i++)
{
sb.Append(hashValue[i].ToString("x2"));
}
break;
}
return sb.ToString();
}
[WebMethod(Description = "极光发送信息")]
public string doSend()
{
IDictionary<string, string> parameters = new Dictionary<string, string>();
string html = string.Empty;
int sendno = ;
string receiverValue = "!!!!!!!!!";//这个是一个别名
int receiverType = ;
string appkeys = "ca9e556875921e4f5d9abdc6";//appkey
String input = sendno.ToString() + receiverType + "" + "fa618664993ec1b9707cdef4";//market key
string verificationCode = MD5Encrypt(input);
string content = "{\"n_title\":\"" + "测试测试" + "\",\"n_content\":\"" + "推送新消息" + "\",\"n_builder_id\":\"1\",\"n_extras\":{\"nid\":\"" + + "\"}}"; //发送的文本
string loginUrl = "http://api.jpush.cn:8800/sendmsg/v2/sendmsg";
parameters.Add("sendno", sendno.ToString());
parameters.Add("app_key", appkeys);
parameters.Add("receiver_type", receiverType.ToString());
parameters.Add("verification_code", verificationCode); //MD5
parameters.Add("msg_type", "");
parameters.Add("msg_content", content); //内容
parameters.Add("platform", "android,ios"); HttpWebResponse response = HttpWebResponseUtility.CreatePostHttpResponse(loginUrl, parameters, null, null, Encoding.UTF8, null); if (response != null)
{
// 得到返回的数据流
Stream receiveStream = response.GetResponseStream();
// 如果有压缩,则进行解压
if (response.ContentEncoding.ToLower().Contains("gzip"))
{
receiveStream = new GZipStream(receiveStream, CompressionMode.Decompress); }
// 得到返回的字符串
html = new StreamReader(receiveStream).ReadToEnd();
}
return html;
}
}

然后调用方法即可。

至于服务器接收代码,因为直接使用的极光上下载的3分钟demo所以直接使用的demo中的接收方法。不用修改任何代码手机端即可接收信息。

如果希望接收到消息之后能打开到对应的页面,则修改TestActivy.java代码如下:【代码未优化~无用的没删除】

package com.example.jpushdemo;

import org.apache.cordova.DroidGap;
import org.json.JSONArray;
import org.json.JSONObject; import cn.jpush.android.api.JPushInterface;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; public class TestActivity extends DroidGap { @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("用户自定义打开的Activity");
Intent intent = getIntent();
String path = "";
if (null != intent) {
Bundle bundle = getIntent().getExtras();
String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
String content = bundle.getString(JPushInterface.EXTRA_ALERT);
String extreaterjson = bundle.getString(JPushInterface.EXTRA_EXTRA);
int index = extreaterjson.indexOf(":");
String result = extreaterjson.substring(index+2, extreaterjson.length()-2);
tv.setText("Title : " + title + " " + "Content : " + content+" 附加字段:"+result);
path="file:///android_asset/www/blog-single.html?nid="+result;
}
addContentView(tv, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); super.loadUrl(path);
} }

参考资料:http://hi.baidu.com/taobao458/item/4ec606f34f169156c9f33781

Phonegap 极光推送api 服务器端推送代码的更多相关文章

  1. **极光推送PHP服务器端推送移动设备消息(Jpush V2 api)

    jpush.php  这是推送方法  用到curl发送请求 <?php /** * 极光推送php 服务器端 * @author yalong sun * @Email <syl_ad@1 ...

  2. 圆柱模板行业B2B站点打造MIP推送+熊掌号推送+历史普通推送插件

    最近因为做聚合页面http://zhimo.yuanzhumuban.cc/hotkey/list-951.html  内部站点关键词拥有5万的行业词库,所以这么多搜索词库,如何让百度第一时间抓取呢? ...

  3. JPush API client library for C Sharp(极光推送API)

    概述 这是 JPush REST API 的 C# 版本封装开发包,是由极光推送官方提供的,一般支持最新的 API 功能. 对应的 REST API 文档:http://docs.jpush.io/s ...

  4. [转]PhoneGap使用PushPlugin插件实现消息推送

    本文转自:http://my.oschina.net/u/1270482/blog/217661 http://devgirl.org/2013/07/17/tutorial-implement-pu ...

  5. SignalR 聊天室实例详解(服务器端推送版)

    翻译自:http://www.codeproject.com/Articles/562023/Asp-Net-SignalR-Chat-Room  (在这里可以下载到实例的源码) Asp.Net Si ...

  6. Qt通过极光推送向app推送消息

    简介 最近在做个项目,当客服端收到防盗的消息通知时向手机app推送一个消息,告知有防盗报警.这么小的功能没必要自己写个推送端,极光推送免费而且推送的成功率高,已经能满足我们的需求了. 极光推送的文档大 ...

  7. iOS推送生成服务器端p12文件

    生成服务器端推送p12文件 所需文件:A.开发证书  aps_production.cer B.本地导出的私钥   : aps_production.p12 C.生成证书时用到的请求文件:Push.c ...

  8. 拥抱HTTP2.0时代 - HTTP2.0实现服务器端推送Push功能

    在当今的移动互联开发趋势中,nghttp2是一个很值得大家去关注的一个开源项目. 我们在nghttpx模块中实现了HTTP/2服务器推送功能,并且在我们的nghttp2.org网站中启用了该推送功能. ...

  9. 百度收录检测并主动推送API(实时 mip推送通用)

    简要描述: 百度收录检测并主动推送API(实时) 请求URL: api.bigjiji.com/baiduCheck_123456 调用方式: img标签 参数: 参数名 必选 类型 说明 site ...

随机推荐

  1. 关于Unity3D中的版本管理 .

    关于Unity3D中的版本管理 使用Unity3D也有一段时间了,由于团队一直使用SVN进行版本管理,现总结一下: (1) Unity3D的二进制资源必须加锁进行版本控制,因为它没办法merge: ( ...

  2. iOS环形控制器、环形按钮

    这两天接手了一个外包的UI,有一个环形的控制器,需求改啊改的:“安卓已经实现了……”,最讨厌这句了,最后做了一版,对方终于满意了,删掉其他的繁琐部分,留下控制器部分,大家看看,有更好的想法欢迎分享. ...

  3. 见过NTP服务,没见过网络流量到200M左右的NTP服务

    XXX,看来可能是NTP.CONF的文件配置错误所致了. 附上一段查看网络流量的SHELL.(好像只针对ETH0,如果要看其它的,还需要修改) #!/bin/bash typeset in_old d ...

  4. Cognos请求流程——<转>

    访问Cognos8 匿名访问 用户通过浏览器发起Cognos访问请求,请求被送至Cognos Gateway Gateway接收请求并发送给一个dispatcher dispatcher发现请求没有附 ...

  5. js和jsp

    1.js中的字符串赋值可以用' '或者" ". alert('1111'); alert("1111"); document.getElementById('d ...

  6. linux系统开机过程描述

    本文描述linux系统开机过程,属于个人理解范畴,如果文中表述有误请大家批评指正! 计算机开机之后,首先要加载BIOS(基本输入输出系统)信息,BIOS包含了很多重要的信息,包括CPU信息,设备启动顺 ...

  7. (step4.3.8)hdu 2181(哈密顿绕行世界问题——DFS)

    题目大意:通俗点讲就是,输出所有从m城市出发,便利所有城市之后又能回到m城市的序列...... 解题思路:DFS 1)用map[][]来存储城市之间的连通情况.用used[]存储某个城市的使用情况(即 ...

  8. java实战之数组工具集

    java是一门面向对象的语言,我们也提到过,面向对象的一个优点就在于能够提高代码的复用性,前面我们详细讲过数组的查找,排序,等等,为了提高代码的复用性,我们何不自己写一个数组的工具集,来综合下前面所学 ...

  9. 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings

    E. Little Elephant and Strings time limit per test 3 seconds memory limit per test 256 megabytes inp ...

  10. 后缀自动机(SAM):SPOJ Longest Common Substring II

    Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...