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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

發(fā)布時間:2023/12/2 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 极光推送服务端API(定时推送任务,推送到指定设备,推送到所有设备) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

極光推送常用的幾個api方法總結,抽取出了utils類,利用MsgType進行業(yè)務類型區(qū)別,方便app端收到推送后進行不同處理

首先引入依賴:

<!-- 極光推送 --><dependency><groupId>cn.jpush.api</groupId><artifactId>jpush-client</artifactId><version>3.3.4</version></dependency><dependency><groupId>cn.jpush.api</groupId><artifactId>jiguang-common</artifactId><version>1.1.1</version></dependency>

?

package com.commons.utils;import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Date; import org.slf4j.Logger; import org.slf4j.LoggerFactory;import com.ecp.commons.exception.APIException; import com.ecp.commons.utils.JsonUtil; import com.google.gson.Gson; import com.google.gson.GsonBuilder;import cn.jiguang.common.resp.APIConnectionException; import cn.jiguang.common.resp.APIRequestException; import cn.jpush.api.JPushClient; import cn.jpush.api.push.PushResult; import cn.jpush.api.push.model.Message; 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.schedule.ScheduleResult;public class JpushUtils {//讀取配置中的appkey和masterSecret
protected static final Logger LOG = LoggerFactory.getLogger(JpushUtils.class);public static final String appKey = com.ecp.commons.common.PropertiesUtil.getProperty("jPush.appKey");public static final String masterSecret = com.ecp.commons.common.PropertiesUtil.getProperty("jPush.masterSecret");/*** * @auth Ren* @date 2018年5月2日* @decripe 定時推送,利用DeviceSN做別名,點對點發(fā)送,同時記錄返回的msg_id* @param obj推送對象,deviceSN設備識別碼,定時的時間date,MsgType推送的業(yè)務類型(APIConstants中定義),* name推送的名稱*/public static ScheduleResult sendSchedulePush(Object obj, String deviceSN, Date date, String MsgType, String name) {JPushClient jPushClient = new JPushClient(masterSecret, appKey);String objStr = ObjectToJson(obj);SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String time = format.format(date);ScheduleResult result = null;PushPayload push = PushPayload.newBuilder().setPlatform(Platform.all()).setMessage(Message.newBuilder().setMsgContent(objStr).addExtras(Collections.singletonMap("MsgType", MsgType)).build()).setAudience(Audience.alias(deviceSN)).build();try {result = jPushClient.createSingleSchedule(name, time, push);LOG.info("Got result - " + result);LOG.info("send objStr - " + objStr);System.out.println(result);System.out.println(objStr);} 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());}return result;}/*** * @auth Ren* @date 2018年5月2日* @decripe 定時推送,推送到所有設備,同時記錄返回的msg_id* @param obj推送對象,定時的時間date,MsgType推送的業(yè)務類型(APIConstants中定義),name推送的名稱*/public static ScheduleResult sendSchedulePushAll(Object obj, Date date, String MsgType, String name) {JPushClient jPushClient = new JPushClient(masterSecret, appKey);String objStr = ObjectToJson(obj);SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String time = format.format(date);ScheduleResult result = null;PushPayload push = PushPayload.newBuilder().setPlatform(Platform.all()).setMessage(Message.newBuilder().setMsgContent(objStr).addExtras(Collections.singletonMap("MsgType", MsgType)).build()).setAudience(Audience.all()).build();try {result = jPushClient.createSingleSchedule(name, time, push);LOG.info("Got result - " + result);LOG.info("send objStr - " + objStr);System.out.println(result);System.out.println(objStr);} 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());}return result;}/*** * @auth Ren* @date 2018年5月2日* @decripe 刪除定時任務* @param scheduleId定時任務的Id*/public static void DeleteSchedule(String scheduleId) {try {JPushClient jPushClient = new JPushClient(masterSecret, appKey);jPushClient.deleteSchedule(scheduleId);} 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());}}/*** * @auth Ren* @date 2018年5月2日* @decripe:把obj對象的json串推送到別名為DeviceSN的設備上,同時記錄返回的msg_id* @param obj推送對象,deviceSN設備識別碼,MsgType推送的業(yè)務類型(APIConstants中定義)*/public static PushResult SendPush(Object obj, String DeviceSN, String MsgType) {JPushClient jPushClient = new JPushClient(masterSecret, appKey);String objStr = ObjectToJson(obj);PushPayload push = PushPayload.newBuilder().setPlatform(Platform.all()).setMessage(Message.newBuilder().setMsgContent(objStr).addExtras(Collections.singletonMap("MsgType", MsgType)).build()).setAudience(Audience.alias(DeviceSN)).build();PushResult result = null;try {result = jPushClient.sendPush(push);LOG.info("Got result - " + result);LOG.info("send objStr - " + objStr);System.out.println(result);System.out.println(objStr);} catch (APIConnectionException e) {LOG.error("Connection error. Should retry later. ", e);LOG.error("Sendno: " + push.getSendno());} 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());LOG.error("Sendno: " + push.getSendno());}if (result == null) {throw new APIException("與設備通話失敗,請聯(lián)系管理員處理!");}return result;}/*** * @auth Ren* @date 2018年5月2日* @decripe 把obj對象的json串推送到所有設備上* @param obj推送對象,MsgType推送的業(yè)務類型(APIConstants中定義)*/public static PushResult SendPushAll(Object obj, String MsgType) {JPushClient jPushClient = new JPushClient(masterSecret, appKey);String objStr = ObjectToJson(obj);PushPayload push = PushPayload.newBuilder().setPlatform(Platform.all()).setMessage(Message.newBuilder().setMsgContent(objStr).addExtras(Collections.singletonMap("MsgType", MsgType)).build()).setAudience(Audience.all()).build();PushResult result = null;try {result = jPushClient.sendPush(push);LOG.info("Got result - " + result);LOG.info("send objStr - " + objStr);System.out.println(result);System.out.println(objStr);} catch (APIConnectionException e) {LOG.error("Connection error. Should retry later. ", e);LOG.error("Sendno: " + push.getSendno());} 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());LOG.error("Sendno: " + push.getSendno());}if (result == null) {throw new APIException("推送失敗,請聯(lián)系管理員處理!");}return result;}public static String ObjectToJson(Object o) {String json = JsonUtil.getJsonString4JavaPOJO(o, "yyyy-MM-dd HH:mm:ss");return json;} }

?

轉載于:https://www.cnblogs.com/self-studyRen/p/9141725.html

總結

以上是生活随笔為你收集整理的极光推送服务端API(定时推送任务,推送到指定设备,推送到所有设备)的全部內容,希望文章能夠幫你解決所遇到的問題。

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