极光推送源码api封装改造
来源:互联网 发布:mac pdf转word网页版 编辑:程序博客网 时间:2024/06/15 17:15
对于做安卓或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.conf private 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 = "";// 多个以逗号隔开,为空默认发送全部tag public 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; }}
阅读全文
0 0
- 极光推送源码api封装改造
- 极光推送的API
- 极光推送api
- 极光推送集成与封装
- JPush极光推送服务器端API
- 极光推送开发第二弹——极光推送API
- java远程调用JPush极光推送api
- JPush极光推送Java服务器端API
- JPush极光推送Java服务器端API
- java远程调用JPush极光推送api
- Andorid极光推送API全角和
- JPush极光推送Java服务器端API
- JPush极光推送Java服务器端API
- 极光推送Java SDK源码学习
- AndroidStudio 集成极光推送(带源码)
- java服务器端调用JPush极光推送api推送通知消息
- java服务器端调用JPush极光推送api推送通知消息
- 极光推送
- js数组方法大全(转载)
- 页面载入进入后台,回来刷新,且只刷新一次,避免无限刷新
- SLAMTEC rplidar a1的使用
- jvm调优
- 将列表导出为excel表格
- 极光推送源码api封装改造
- TensorFlow发布Eager,便于Debug!
- Android组件学习笔记(简单音乐播放器,service与intent)
- ADO 数据库编程
- Java核心技术-反射
- Java时间处理类SimpleDateFormat的parse和format
- leetcode: 62. Unique Paths
- window7下部署zookeeper、启动dubbo-admin
- leetcode: 63. Unique Paths II