极光推送

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace OperaterAPI
{
   public  class Jpush
    {

        //string RegistrationID = "";
        private string ApiKey = "";
        private string APIMasterSecret = "";


        delegate void D(object obj);


        /// <summary>
        /// Android极光推送
        /// </summary>
        /// <param name="RegistrationID">设备号</param>
        public string PushAndroid(string RegistrationID,string Title, string Content)
        {
           
            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,ios";//目标用户终端手机的平台类型,如: 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);
            postData += ("&apns_production=" + 1);

            //byte[] data = encoding.GetBytes(postData);
            byte[] data = Encoding.UTF8.GetBytes(postData);
            string resCode = GetPostRequest(data);//调用极光的接口获取返回值
            //label3.Text = "结果:" + resCode;
            return resCode;

        }

        /// <summary>
        /// ios极光推送
        /// </summary>
        /// <param name="RegistrationID">设备号</param>
        public void PushAndroid1(string RegistrationID, string Title, string Content)
        {

            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,ios";//目标用户终端手机的平台类型,如: 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);
            //postData += ("&apns_production=" + 1);
            //byte[] data = encoding.GetBytes(postData);
            byte[] data = Encoding.UTF8.GetBytes(postData);
            string resCode = GetPostRequest(data);//调用极光的接口获取返回值
            //label3.Text = "结果:" + resCode;


        }

        /// <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;
            }
        }

        /// <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;
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值