最近做的. net 项目(Windows Service)需要向 Android 手机发送推送消息,真是有点困难,没有搞过就不停的搜文档,最后看到了一个开源项目 PushSharp,可以在. net 平台推送 IOS,Android,Windows Phone 等设备消息,大喜,然后先做了 IOS 的,成功了,但是做 Android 的时候遇到了问题,一直推送不成功,程序执行了,但是推送一直出不来,后来费劲的在网上搜,没有找到,最后放弃使用这种推送 Android,另寻出路,随后找到了一种 C2DM 云端推送功能,但是问题又出现了,(1)C2DM 内置于 Android 的 2.2 系统上,无法兼容老的 1.6 到 2.1 系统;(2)C2DM 需要依赖于 Google 官方提供的 C2DM 服务器,由于国内的网络环境,这个服务经常不可用,如果想要很好的使用,我们的 App Server 必须也在国外,这个恐怕不是每个开发者都能够实现的;(3)不像在 iPhone 中,他们把硬件系统集成在一块了。所以对于我们开发者来说,如果要在我们的应用程序中使用 C2DM 的推送功能,因为对于不同的这种硬件厂商平台,比如摩托罗拉、华为、中兴做一个手机,他们可能会把 Google 的这种服务去掉,尤其像在国内就很多这种,把 Google 这种原生的服务去掉。买了一些像什么山寨机或者是华为这种国产机,可能 Google 的服务就没有了。而像在国外出的那些可能会内置。没办法了,最后转到了第三方推送服务平台,极光推送,下面将介绍怎么使用极光推送。
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>
- /// Android极光推送
- /// </summary>
- /// <param name="RegistrationID">设备号</param>
- public void PushAndroid(string RegistrationID)
- {
- try
- {
- Random ran = new Random();
- int sendno = ran.Next(1, 2100000000);//随机生成的一个编号
- string app_key = ApiKey;
- string masterSecret = APIMasterSecret;
- int receiver_type = 5;//接收者类型。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 支持)
- string msg_content = "{\"n_builder_id\":\"00\",\"n_title\":\"" + Title + "\",\"n_content\":\"" + Content + "\"}";//消息内容
- string platform = "android";//目标用户终端手机的平台类型,如: android, ios 多个请使用逗号分隔。
- string verification_code = GetMD5Str(sendno.ToString(), receiver_type.ToString(), receiver_value,masterSecret);//验证串,用于校验发送的合法性。MD5
- string postData = "sendno=" + sendno;
- postData += ("&app_key=" + app_key);
- postData += ("&masterSecret=" + masterSecret);
- postData += ("&receiver_type=" + receiver_type);
- postData += ("&receiver_value=" + receiver_value);
- postData += ("&msg_type=" + msg_type);
- postData += ("&msg_content=" + msg_content);
- postData += ("&platform=" + platform);
- postData += ("&verification_code=" + verification_code);
- //byte[] data = encoding.GetBytes(postData);
- byte[] data = Encoding.UTF8.GetBytes(postData);
- string resCode = GetPostRequest(data);//调用极光的接口获取返回值
- JpushMsg msg = Newtonsoft.Json.JsonConvert.DeserializeObject<JpushMsg>(resCode);//定义一个JpushMsg类,包含返回值信息,将返回的json格式字符串转成JpushMsg对象
- }
- catch (Exception ex)
- {
- }
- }
4,MD5 加密验证字符串,用于调用接口的时候,极光将做验证使用
- /// <summary>
- /// MD5字符串
- /// </summary>
- /// <param name="paras">参数数组</param>
- /// <returns>MD5字符串</returns>
- public string GetMD5Str(params string [] paras)
- {
- string str = "";
- for(int i=0;i<paras.Length;i++)
- {
- str += paras[i];
- }
- byte[] buffer = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(str));
- string md5Str = string.Empty;
- for (int i = 0; i < buffer.Length; i++)
- {
- md5Str = md5Str + buffer[i].ToString("X2");
- }
- return md5Str;
- }
5,http Post 方式调用极光的推送服务
- /// <summary>
- /// Post方式请求获取返回值
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public string GetPostRequest(byte[] data)
- {
- HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://api.jpush.cn:8800/v2/push");
- myRequest.Method = "POST";//极光http请求方式为post
- myRequest.ContentType = "application/x-www-form-urlencoded";//按照极光的要求
- myRequest.ContentLength = data.Length;
- Stream newStream = myRequest.GetRequestStream();
- // Send the data.
- newStream.Write(data, 0, data.Length);
- newStream.Close();
- // Get response
- var response = (HttpWebResponse)myRequest.GetResponse();
- using (var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")))
- {
- string result = reader.ReadToEnd();
- reader.Close();
- response.Close();
- return result;
- }
- }
6,定义一个类,接收返回值
- public class JpushMsg {
- private string sendno; //编号
- public string Sendno {
- get {
- return sendno;
- }
- set {
- sendno = value;
- }
- }
- private string msg_id; //信息编号
- public string Msg_id {
- get {
- return msg_id;
- }
- set {
- msg_id = value;
- }
- }
- private string errcode; //返回码
- public string Errcode {
- get {
- return errcode;
- }
- set {
- errcode = value;
- }
- }
- private string errmsg; //错误信息
- public string Errmsg {
- get {
- return errmsg;
- }
- set {
- errmsg = value;
- }
- }
- }
来源: http://www.bubuko.com/infodetail-1983193.html