对于做安卓或ios客户端消息推送的第三方服务极光推送,它有一套自己的api,为了简化集成,我们往往需要改造封装。

极光推送官网:https://www.jiguang.cn/push

SDK下载地址:https://docs.jiguang.cn/jpush/resources/

PushExample.java

package cn.jpush.api.examples;import cn.jpush.api.JPushClient;
import cn.jpush.api.common.connection.HttpProxy;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.HashMap;
import java.util.Map;public class PushExample {protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);// demo App defined in resources/jpush-api.confprivate static String appKey = "";// private static String masterSecret = "";// public static String TITLE = "Test from API example";//public static String ALERT = "";public static String MSG_CONTENT = "{'type':'1','userid':'zhangsan','username':'张三','cmd':'','content':''}";public static String REGISTRATION_ID = "4345erhf7";//public static String TAG = "";// 多个以逗号隔开,为空默认发送全部tagpublic static String ALIAS = "";// 别名public static String PROXYURL = "";public static String PROXYPORT = "8080";public static String PROXYSTATE = "0";public static void main(String[] args) {Map<String, Object> map = new HashMap<String, Object>();PushExample push = new PushExample();// push.testSendPush(map);push.testMessage(map);}public Map<String, Object> testSendPush(Map<String, Object> map) {// System.out.println("----------调极光推送api");Map<String, Object> rmap = new HashMap<String, Object>();try {String returnCode = "0001";// 失败if (map != null && !map.isEmpty()) {this.appKey = map.get("appKey") + "";this.masterSecret = map.get("masterSecret") + "";this.TITLE = map.get("title") + "";this.ALERT = map.get("alert") + "";this.MSG_CONTENT = map.get("msgcontent") + "";this.REGISTRATION_ID = map.get("registrationid") + "";this.TAG = map.get("tag") + "";this.ALIAS = map.get("alias") + "";this.PROXYURL = map.get("proxyurl") + "";this.PROXYPORT = map.get("proxyport") + "";this.PROXYSTATE = map.get("proxystate") + "";}PushResult result = null;PushPayload payload = null;System.out.println("----this.PROXYSTATE:" + this.PROXYSTATE);if (this.PROXYSTATE != null && !"".equals(this.PROXYSTATE)&& "1".equals(this.PROXYSTATE)) {// System.out.println("-------------------有代理");HttpProxy proxy = new HttpProxy("proxy.tj.cmcc", 8080);JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3, proxy);if (isStrNull(TAG)) {payload = buildPushObject_all_all_alert();} else {// 安卓-标签发送【多个标签以逗号隔开】payload = buildPushObject_android_tag_alertWithTitle2();}result = jpushClient.sendPush(payload);} else {// System.out.println("-------------------无代理");JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);if (isStrNull(TAG)) {payload = buildPushObject_all_all_alert();} else {// 安卓-标签发送【多个标签以逗号隔开】payload = buildPushObject_android_tag_alertWithTitle2();}result = jpushClient.sendPush(payload);}System.out.println("--------result:" + result);if (result.msg_id > 0) {returnCode = "0000";// 成功}System.out.println("------returnCode:" + returnCode);LOG.info("Got result - " + result);rmap.put("returnCode", returnCode);rmap.put("sendContent", this.ALERT);rmap.put("msgId", result.msg_id);rmap.put("sendno", result.sendno);} catch (APIConnectionException e) {LOG.error("Connection error. Should retry later. ", e);} catch (APIRequestException e) {LOG.error("Error response from JPush server. Should review and fix it. ", e);LOG.info("HTTP Status: " + e.getStatus());LOG.info("Error Code: " + e.getErrorCode());LOG.info("Error Message: " + e.getErrorMessage());LOG.info("Msg ID: " + e.getMsgId());}return rmap;}public Map<String, Object> testMessage(Map<String, Object> map) {// System.out.println("----------调极光推送api");Map<String, Object> rmap = new HashMap<String, Object>();try {String returnCode = "0001";// 失败if (map != null && !map.isEmpty()) {System.out.println("----------->赋值属性");this.appKey = map.get("appKey") + "";this.masterSecret = map.get("masterSecret") + "";this.TITLE = map.get("title") + "";this.ALERT = map.get("alert") + "";this.MSG_CONTENT = map.get("msgcontent") + "";this.REGISTRATION_ID = map.get("registrationid") + "";this.TAG = map.get("tag") + "";this.ALIAS = map.get("alias") + "";this.PROXYURL = map.get("proxyurl") + "";this.PROXYPORT = map.get("proxyport") + "";this.PROXYSTATE = map.get("proxystate") + "";}PushResult result = null;PushPayload payload = null;System.out.println("----this.PROXYSTATE:" + this.PROXYSTATE);if (this.PROXYSTATE != null && !"".equals(this.PROXYSTATE)&& "1".equals(this.PROXYSTATE)) {// System.out.println("-------------------有代理");HttpProxy proxy = new HttpProxy("proxy.tj.cmcc", 8080);JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3, proxy);if (isStrNull(TAG)) {payload = buildPushObject_android_all_message();} else {// 安卓-标签发送【多个标签以逗号隔开】payload = buildPushObject_android_tag_message();}result = jpushClient.sendPush(payload);} else {// System.out.println("-------------------无代理");JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);if (isStrNull(TAG)) {if (isStrNull(ALIAS)) {payload = buildPushObject_android_all_message();} else {payload = buildPushObject_android_alias_message();}} else {// 安卓-标签发送【多个标签以逗号隔开】payload = buildPushObject_android_tag_message();}result = jpushClient.sendPush(payload);}System.out.println("--------result:" + result);if (result.msg_id > 0) {returnCode = "0000";// 成功}System.out.println("------returnCode:" + returnCode);LOG.info("Got result - " + result);rmap.put("returnCode", returnCode);rmap.put("sendContent", this.ALERT);rmap.put("msgId", result.msg_id);rmap.put("sendno", result.sendno);} catch (APIConnectionException e) {LOG.error("Connection error. Should retry later. ", e);} catch (APIRequestException e) {LOG.error("Error response from JPush server. Should review and fix it. ", e);LOG.info("HTTP Status: " + e.getStatus());LOG.info("Error Code: " + e.getErrorCode());LOG.info("Error Message: " + e.getErrorMessage());LOG.info("Msg ID: " + e.getMsgId());}return rmap;}// 全部推送public static PushPayload buildPushObject_all_all_alert() {return PushPayload.alertAll(ALERT);}public static PushPayload buildPushObject_all_alias_alert() {return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias("alias1")).setNotification(Notification.alert(ALERT)).build();}public static PushPayload buildPushObject_android_tag_alertWithTitle() {return PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.tag("liuyqtag")).setNotification(Notification.android(ALERT, TITLE, null)).build();}// 新增方法,以逗号隔开的形式【标签】 【android形式 】public static PushPayload buildPushObject_android_tag_alertWithTitle2() {return PushPayload.newBuilder().setPlatform(Platform.android())// .setPlatform(Platform.all()).setAudience(Audience.tag2(TAG)).setNotification(Notification.android(ALERT, TITLE, null))// .setNotification(Notification.alert(ALERT)).build();}public static PushPayload buildPushObject_android_and_ios() {return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.tag("tag1")).setNotification(Notification.newBuilder().setAlert("alert content").addPlatformNotification(AndroidNotification.newBuilder().setTitle("Android Title").build()).addPlatformNotification(IosNotification.newBuilder().incrBadge(1).addExtra("extra_key", "extra_value").build()).build()).build();}public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {return PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.tag_and("tag1", "liuyqtag", "ss", "dd")).setNotification(Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder().setAlert(ALERT).setBadge(5).setSound("happy").addExtra("from", "JPush").build()).build()).setMessage(Message.content(MSG_CONTENT)).setOptions(Options.newBuilder().setApnsProduction(true).build()).build();}// 新增方法,以逗号隔开的形式 public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage2() {return PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.tag_and2(TAG)).setNotification(Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder().setAlert(ALERT).setBadge(5).setSound("happy").addExtra("from", "JPush").build()).build()).setMessage(Message.content(MSG_CONTENT)).setOptions(Options.newBuilder().setApnsProduction(true).build()).build();}public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.newBuilder().addAudienceTarget(AudienceTarget.tag("tag1", "tag3")).addAudienceTarget(AudienceTarget.alias("alias1", "alias3")).build()).setMessage(Message.newBuilder().setMsgContent(MSG_CONTENT).addExtra("from", "JPush").build()).build();}// 【自定义消息】新增方法,以逗号隔开的形式 【android形式 】public static PushPayload buildPushObject_android_tag_message() {return PushPayload.newBuilder().setPlatform(Platform.android())// .setPlatform(Platform.all()).setAudience(Audience.tag2(TAG)).setMessage(Message.content(MSG_CONTENT)).build();}// 【自定义消息】新增方法,所有 【android形式 】public static PushPayload buildPushObject_android_all_message() {return PushPayload.newBuilder().setPlatform(Platform.android())// .setPlatform(Platform.all()).setAudience(Audience.all()).setMessage(Message.content(MSG_CONTENT)).build();}// 【自定义消息】新增方法,以逗号隔开的形式【别名】 【android形式 】public static PushPayload buildPushObject_android_alias_message() {return PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.alias(ALIAS)).setMessage(Message.content(MSG_CONTENT)).build();}public static boolean isStrNull(String str) {boolean flg = false;if (str == null || "".equals(str)) {flg = true;}return flg;}}

极光推送源码api封装改造相关推荐

  1. JPush极光推送Java服务器端API

    菜鸟的春天 JPush极光推送Java服务器端API // 对android和ios设备发送 JPushClient jpush = new JPushClient(masterSecret, app ...

  2. 极光推送之java后台封装REST API

    1 什么是推送? 这个看图效果最好请直接看下图: 我们手机经常会收到如上图弹框消息,我们今天说的就是上面的弹窗信息如何推送的.一般情况我们可以通过第三的服务来给自己的app发送推送消息例如:极光推送. ...

  3. 极光推送服务端API(定时推送任务,推送到指定设备,推送到所有设备)

    极光推送常用的几个api方法总结,抽取出了utils类,利用MsgType进行业务类型区别,方便app端收到推送后进行不同处理: 首先引入依赖: <!-- 极光推送 --><depe ...

  4. 【抖音热门情侣必备】微信早安定时消息推送源码

    手把手教你最近很火的 微信公众号测试号推送消息 1. 注册微信公众号测试号 2. 扫描测试号二维码 3. 新增测试模板 4. 下载并打开config修改配置文件 5. 补充配置文件 6. 运行程序 7 ...

  5. 极光推送 java api_JPush极光推送Java服务器端API

    方法名称参数列表(必须)方法说明 setEnableSSL boolean enableSSL (true为使用ssl, 默认为不使用ssl) 是否启动ssl安全连接 sendNotification ...

  6. 给女朋友做公众号天气推送源码+教学+自动版本

    正文: 原版pythong,现在,起初,人们认为这仅仅只是个简单且幼稚,只为图一乐的微信消息模板. 后来,什么云服务器,php,html,个人主页,点滴相册,企业微信,各种各样的花式模板,易语言都来了 ...

  7. 极光推送指定用户推送_干货|SpringBoot集成极光推送完整实现代码(建议收藏)...

    工作中经常会遇到服务器向App推送消息的需求,一般企业中选择用极光推送的比较多,在集成极光时发现极光的文档并不完整,网上的文章也很多不能直接使用,这里列出我在工作中集成极光的全部代码,只需要按照如下代 ...

  8. 用JPUSH极光推送实现服务端向安装了APP应用的手机推送消息(C#服务端接口)

    这次公司要我们做一个功能,就是当用户成功注册以后,他登录以后要收到消息,当然这个消息是安装了我们的手机APP应用的手机咯. 极光推送的网站的网址是:https://www.jpush.cn/ 极光推送 ...

  9. iOS推送(利用极光推送)

    本文主要是基于极光推送的SDK封装的一个快速集成极光推送的类的封装(不喜勿喷) (1)首先说一下推送的一些原理: Push的原理: Push 的工作机制可以简单的概括为下图 图中,Provider是指 ...

  10. Android第三方SDK集成 —— 极光推送

    前言: 本文前篇,可以帮助朋友们快速集成极光推送.本文后篇,是我自己项目实践的一些总结和心得,应该对读者们还是很有参考价值的,相信读完这篇文章,你会对极光推送有更加深入的理解,而不仅仅只是会集成而已. ...

最新文章

  1. iOS开发UI篇—UITableview控件基本使用
  2. unity延迟执行下一行代码_Python代码在Linux环境下执行错误异常
  3. MapReduce进阶:多MapReduce的链式模式
  4. [译] Web 爬虫下的 Python 数据分析:中情局全球概况图解
  5. DotNet(C#)自定义运行时窗体设计器 一
  6. Android安装两次才成功,Android应用从市场安装完成打开与桌面打开,被启动两次的问题...
  7. 魅族前副总裁李楠谈“苹果对5G判断”,理解万岁!
  8. arm-linux启动,linux启动流程arm
  9. ASP.NET程序中 抛出Thread was being aborted. 异常(转)
  10. 没了珊瑚虫你用谁?八大QQ主流修改版大比拼
  11. Netflix混沌工程手册Part 1:混沌工程简介
  12. 小松鼠短视频完美开源源码
  13. Excel取消自动行高调整(取消自动换行)
  14. JAVA后台随机生成一个中文名字
  15. 螃蟹保存方法保存时间_活面包蟹怎么保存?面包蟹能保存多久
  16. linux进程signal,Linux Signal 示例
  17. 【转载】走心撩到电音妹:所谓的“迷幻电子”是什么?
  18. 程序无法安装时, 提示无法访问网络位置0 的解决办法
  19. Matlab 函数atan 函数atan2 的区别
  20. 为什么有些人钱花了而赚不到钱呢?

热门文章

  1. C:\Users\xx\.xxxxxx\system\tomcat\xxx\work\Catalina\localhost\ROOT(系统找不到指定文件)
  2. 数学建模线性规划实例及详细解答(MATLAB代码)
  3. 食饵捕食者模matlab,数学建模经典基于MATLAB的三种群食饵_捕食者模型数值解
  4. linux中文变成日文,linux nkf 日文编码转换命令[转载]
  5. 微信小程序搜索关键字高亮和ctrl+f搜索定位实现
  6. wireshark抓包教程详解
  7. 看故事也能长知识,CPU的工作原理原来这么简单!
  8. 计算机无法访问桌面,桌面无法显示_电脑桌面显示:无法访问,你可能没有权限使用网络......
  9. html引入思源黑体
  10. 高通QFIL烧录错误求解