string JPushURI = "https://api.jpush.cn/v3/push";
       string app_key = "808a3e149a54f82dbdb06f47";
       string masterSecret = "ced2ca08b8b789585dfa9222";
       string msg = "zhanghuiying";

第一种写法

public static string JPush(string msg)

{
            string result = "";
            HttpWebRequest rq = (HttpWebRequest)HttpWebRequest.Create(JPushURI);
            rq.ContentType = "application/json";
            rq.Method = "POST";
            #region 封装header与消息体
            //Header
            byte[] buffer = Encoding.Default.GetBytes(app_key + ":" + masterSecret );
            string base64 = Convert.ToBase64String(buffer);
            rq.Headers.Set(HttpRequestHeader.Authorization, base64);
            rq.Headers.Set("Authorization", base64);
            //消息体
            Dictionary<string, object> data = new Dictionary<string, object>();
            ArrayList arr = new ArrayList();
            arr.Add("android");
            data.Add("platform", arr);//推送分平台["android", "ios"]
            data.Add("audience", "all");//推送分用户"tag或者alias":["tag","alias"]
            Dictionary<string, object> notification = new Dictionary<string, object>();
            Dictionary<string, object> androidalert = new Dictionary<string, object>();
            androidalert.Add("alert", msg);//分平台传
            notification.Add("android", androidalert);
            data.Add("notification", notification);
            Dictionary<string, object> message = new Dictionary<string, object>();
            message.Add("msg_content", "固定消息(非广播)内容体");
            data.Add("message", message);
            JavaScriptSerializer js = new JavaScriptSerializer();
            string json = js.Serialize(data);
            #endregion
            try
            {
                using (Stream rqs = rq.GetRequestStream())
                {
                    byte[] bytejson = Encoding.Default.GetBytes(json);
                    rqs.Write(bytejson, 0, bytejson.Length);
                    using (HttpWebResponse rp = (HttpWebResponse)rq.GetResponse())
                    {
                        using (Stream rps = rp.GetResponseStream())
                        {
                            StreamReader sr = new StreamReader(rps);
                            result = sr.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception e) { Console.WriteLine("异常:" + e.Message); }
            return result;

}

第二种写法

protected void Button1_Click(object sender, EventArgs e)
        {
            string result = "";
            try
            {
                //消息体  
                Dictionary<string, object> data = new Dictionary<string, object>();
                ArrayList arr = new ArrayList();
                arr.Add("android");
                arr.Add("ios");
                data.Add("platform", arr);
                ArrayList arr1 = new ArrayList();
                arr1.Add("JZ1098");
                Dictionary<string, object> alias = new Dictionary<string, object>();
                alias.Add("alias", arr1);
                data.Add("audience", alias);
                Dictionary<string, object> androidNotificationParam = new Dictionary<string, object>();
                //androidNotificationParam.Add("url", "0");
                //androidNotificationParam.Add("id", "0");
                //androidNotificationParam.Add("messageTitle", "0");
                //androidNotificationParam.Add("ModiflyTime", "0");
                androidNotificationParam.Add("", "{}");
                Dictionary<string, object> androidNotificationExtras = new Dictionary<string, object>();
                androidNotificationExtras.Add("option", "0");
                androidNotificationExtras.Add("param", androidNotificationParam);
                Dictionary<string, object> androidNotificationVal = new Dictionary<string, object>();
                androidNotificationVal.Add("val", androidNotificationExtras);
                Dictionary<string, object> androidNotification = new Dictionary<string, object>();
                androidNotification.Add("extras", androidNotificationVal);
                Dictionary<string, object> notification = new Dictionary<string, object>();
                notification.Add("alert", msg);
                notification.Add("android", androidNotification);
                data.Add("notification", notification);
                Dictionary<string, object> options = new Dictionary<string, object>();
                options.Add("apns_production", false);
                data.Add("options", options);
                JavaScriptSerializer js = new JavaScriptSerializer();
                string json = js.Serialize(data);
                try
                {
                    result = RequestWebClient(JPushURI, json, null, app_key, masterSecret);
                    if (!string.IsNullOrWhiteSpace(result))
                    {
                        System.Web.UI.ScriptManager.RegisterStartupScript(Page, Page.GetType(), "msg", "alert(\"推送消息成功!\");", true);//return result;
                    }
                }
                catch (Exception ex)
                {
                    System.Web.UI.ScriptManager.RegisterStartupScript(Page, Page.GetType(), "msg", "alert(\"推送消息失败!\");", true);
                }
            }
            catch (Exception ex)
            {
                System.Web.UI.ScriptManager.RegisterStartupScript(Page, Page.GetType(), "msg", "alert(\"推送消息失败!\");", true);
            }
            System.Web.UI.ScriptManager.RegisterStartupScript(Page, Page.GetType(), "msg", "alert(\"推送消息成功!\");", true);//return result;
        }

public static string RequestWebClient(string uri, string paramStr, Encoding encoding, string username, string password)
        {
            if (encoding == null)
                encoding = Encoding.UTF8;
            string result = string.Empty;
            WebClient wc = new WebClient();
            // 采取POST方式必须加的Header
            wc.Headers.Add("Content-Type", "application/json");
            byte[] postData = encoding.GetBytes(paramStr);
            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                wc.Credentials = GetCredentialCache(uri, username, password);
                wc.Headers.Add("Authorization", GetAuthorization(username, password));
            }

byte[] responseData = wc.UploadData(uri, "POST", postData); // 得到返回字符流
            return encoding.GetString(responseData);// 解码                  
        }
        public static CredentialCache GetCredentialCache(string uri, string username, string password)
        {
            string authorization = string.Format("{0}:{1}", username, password);
            CredentialCache credCache = new CredentialCache();
            credCache.Add(new Uri(uri), "Basic", new NetworkCredential(username, password));
            return credCache;
        }
        public static string GetAuthorization(string username, string password)
        {
            string authorization = string.Format("{0}:{1}", username, password);
            return "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(authorization));
        }

极光推送REST API相关推荐

  1. 极光推送REST API与Java后台对接

    极光推送官网的web推送页面 因为是对接它的api,所以我参照这这个样式实现了一个,效果如下: 定时任务推送界面,可定制.实现了推送一次和每日定时推送,如果再扩展的话有每周.每月的功能,只是没有这个业 ...

  2. 极光推送REST API 实例(分等级推送,使用别名推送)

    在一个推送功能中,给不同等级的用户推送不同的消息 /// <summary>     /// 取得发送人员名单     /// </summary>     /// <p ...

  3. JPush(极光推送)实战总结

    关于JPush 极光推送是国内的服务厂商提供的一站式push服务(同时支持iOS.android),后面也加入了即时通讯的能力供app使用.致力于打造简单.可靠.价格有竞争力的服务(简单功能全免费,高 ...

  4. 极光推送--服务器端

        极光推送,由名字可知是一款推送软件.由于android自带的推送在大陆被墙掉了,所以在国内用第三方推送的用户特别多,比较有名的有极光推送.百度云推送.推送的主要用途是用来推送消息给客户端,比如 ...

  5. 极光推送java服务端-通知(2)

    1.下载SDK REST API为极光推送开发API 2.下载好后,maven导入后可以参考示例 官方demo 4.小demo // 设置好账号的app_key和masterSecret是必须的pri ...

  6. 极光推送后台php接口,极光推送Jpush(v2)接口 服务端PHP版本的REST API推送类

    在许多的手机App开发中推送是一个必须的应用.高大上的企业都会自己投入成本搭建自己的推送服务器,而小微企业则会选择一些服务商,使用他们的服务,减轻自己的运营和维护的成本.Jpush(极光推送)是目前比 ...

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

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

  8. Phonegap 极光推送api 服务器端推送代码

    .net 版本 极光推送 后台接口 HttpWebResponseUtility类 using System; using System.Collections.Generic; using Syst ...

  9. 极光推送 api ios参数问题

    这是首个app项目,推送用的是极光推送jpush 由于用官方文档出现接收多条的问题,在网上找到一套封装好的,非常感觉这位开发者 //推送.指定人error_reporting(E_ALL^E_NOTI ...

最新文章

  1. git push 推送大文件失败的处理办法
  2. 基于 APIGateway 打造生产级别的 Knative 服务
  3. java newdirectorystream_Path、Paths和DirectoryStreamT
  4. vSphere Client 编辑虚拟机属性的问题
  5. 协方差意味着什么_“零”到底意味着什么?
  6. kubernetes mysql pxc_K8S使用operator部署和管理Percona - PXC集群
  7. 论坛的搭建以及优化方案
  8. 运行Pangolin时提示错误
  9. mysql unsigend_创建表 查询数据
  10. 能源消耗总量计算公式_七、能源统计(21)
  11. 关注点分离(Separation of concerns, SoC)
  12. 物联网全景动态图谱2.0|PaaS物联网平台汇总
  13. 计算历史区间的收益率,用前复权还是后复权?
  14. 为什么图片和PDF合并后的PDF页面大小不一
  15. 算法竞赛进阶指南:0x14:后缀数组
  16. Spring之自动装配
  17. catia草绘标注工具
  18. 软件安装和点亮流水灯
  19. python数据分析方法五种_python数据分析与算法之五 算法
  20. java下雪_java多线程编程实现下雪效果

热门文章

  1. BloomFilte 布隆过滤器原理与实现
  2. 信息学奥赛一本通1178题——成绩排序
  3. java实现动态展示当前时间,在文本框中动态地显示当前时间,有木有人做过,指导下初学者...
  4. RestTemplate使用带泛型的返回体
  5. ijidea搭建springMVC入门程序,配置TomCat
  6. 用c语言程序画圣诞树,【图片】用C语言画一棵“圣诞树”(前方高能,学渣小心避让!)【广西科技大学吧】_百度贴吧...
  7. 一对一视频聊天系统中三种聊天方式的功能应用代码详解
  8. 华为手机如何升级鸿蒙系统_升级快讯:又一批华为手机可以升级到“鸿蒙系统”了!...
  9. 计算机简单故障排除教案,计算机故障检测与排除教案,计算机故障排除方法
  10. 全球计算机出货量排名,2017年全球PC出货量分析:全年出货量达2.6亿台