日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

极光推送源码api封装改造

發(fā)布時間:2023/12/10 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 极光推送源码api封装改造 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

對于做安卓或ios客戶端消息推送的第三方服務(wù)極光推送,它有一套自己的api,為了簡化集成,我們往往需要改造封裝。

極光推送官網(wǎng):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 = "";// 多個以逗號隔開,為空默認發(fā)送全部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("----------調(diào)極光推送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 {// 安卓-標簽發(fā)送【多個標簽以逗號隔開】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 {// 安卓-標簽發(fā)送【多個標簽以逗號隔開】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("----------調(diào)極光推送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 {// 安卓-標簽發(fā)送【多個標簽以逗號隔開】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 {// 安卓-標簽發(fā)送【多個標簽以逗號隔開】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;}}

總結(jié)

以上是生活随笔為你收集整理的极光推送源码api封装改造的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。