如下是本人開發期間整理出來的極光推送Java後臺相關的工具類,給你們分享一下..java
<!- 極光pom -> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.3.10</version> </dependency>
package com.jackpot.message; import cn.jiguang.common.ClientConfig; import cn.jpush.api.JPushClient; 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.notification.AndroidNotification; import cn.jpush.api.push.model.notification.IosNotification; import cn.jpush.api.push.model.notification.Notification; import cn.jpush.api.schedule.ScheduleResult; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; /** * @Author: hanjt * @Date: 2019/2/01 11:23 * @Description: 極光推送工具類 */ @Component @Slf4j public class JPushUtil { @Value("${jpush.app.key}") private String jpushAppKey; @Value("${jpush.master.secret}") private String jpushMasterSecret; @Value("${jpush.apns.prod}") private boolean apnsProd; private JPushClient jpushClient = new JPushClient(jpushMasterSecret, jpushAppKey, null, ClientConfig.getInstance()); private final static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); /** * 指定用戶極光推送 * * @param message * @param to * @param token * @return */ public Result push(String message, String to, String... token) { try { PushPayload payload = pushAllNotify(message, to, token); PushResult result = jpushClient.sendPush(payload); log.info(JSON.toJSONString(result)); return null; } catch (Exception e) { log.error(e.getMessage(), e); return Result.newInstance(Code.ERR, e.getMessage()); } } /** * 指定用戶極光定時推送 * * @param message * @param to * @param token * @return */ public Result schedulePush(String title, String message, String to, LocalDateTime time, String... token) { try { PushPayload push = pushAllNotify(message, to, token); ScheduleResult result = jpushClient.createSingleSchedule(title, dtf.format(time), push); log.info(JSON.toJSONString(result)); return null; } catch (Exception e) { log.error(e.getMessage(), e); return Result.newInstance(Code.ERR, e.getMessage()); } } /** * 全平臺指定用戶推送 * * @param message * @param to * @param token * @return */ public PushPayload pushAllNotify(String message, String to, String... token) { PushPayload.Builder payload = PushPayload.newBuilder() .setPlatform(Platform.all()); payload.setAudience(StringUtils.isEmpty(token) ? Audience.all() : Audience.alias(token)); payload.setNotification(Notification.newBuilder().setAlert(message) .addPlatformNotification(IosNotification.newBuilder().addExtra("to", to).build()) .addPlatformNotification(AndroidNotification.newBuilder().addExtra("to", to).build()) .build() ); if (apnsProd) payload.setOptions(Options.newBuilder().setApnsProduction(true).build()); return payload.build(); } /** * 指定平臺全用戶推送 * time不爲空--定時推送 * * @param flag(1:Android,2:IOS,3:所有) * @param message * @return */ public Result pushFlag(Integer flag, String title, String message, LocalDateTime time) { try { String to = JPushEnum.MESSAGE_SYSTEM.getCode(); PushPayload payload = MessageEnum.ALL_PLATFORM.getCode().equals(flag) ? pushAllNotify(message, to) : MessageEnum.IOS.getCode().equals(flag) ? pushIosNotify(message, to) : pushAndroidNotify(message, to); //時間爲空則爲正常推送 if (StringUtils.isEmpty(time)) { PushResult result = jpushClient.sendPush(payload); log.info(JSON.toJSONString(result)); } //時間不爲則爲定時推送 else { ScheduleResult result = jpushClient.createSingleSchedule(title, dtf.format(time), payload); log.info(JSON.toJSONString(result)); } return null; } catch (Exception e) { log.error(e.getMessage(), e); return Result.newInstance(Code.ERR, e.getMessage()); } } /** * 安卓全平臺推送 * * @param message * @param to * @return */ private PushPayload pushAndroidNotify(String message, String to) { PushPayload.Builder payload = PushPayload.newBuilder() .setPlatform(Platform.android()) .setAudience(Audience.all()) .setNotification( Notification.newBuilder().addPlatformNotification( AndroidNotification.newBuilder().setAlert(message).addExtra("to", to).build() ).build() ); return payload.build(); } /** * ios全平臺推送 * * @param message * @param to * @return */ private PushPayload pushIosNotify(String message, String to) { PushPayload.Builder payload = PushPayload.newBuilder() .setPlatform(Platform.ios()) .setAudience(Audience.all()) .setNotification( Notification.newBuilder().addPlatformNotification( IosNotification.newBuilder().setAlert(message).addExtra("to", to).build() ).build() ); if (apnsProd) payload.setOptions(Options.newBuilder().setApnsProduction(true).build()); return payload.build(); } /** * 指定用戶極光透傳 * * @param message * @param to * @param token * @return */ public Result transmit(String title, String message, String to, String... token) { try { PushPayload payload = sendAllPush(title, message, to, token); PushResult result = jpushClient.sendPush(payload); log.info(JSON.toJSONString(result)); return null; } catch (Exception e) { log.error(e.getMessage(), e); return Result.newInstance(Code.ERR, e.getMessage()); } } /** * 全平臺指定用戶極光透傳 * * @param title 標題 * @param message 消息內容 * @param to app跳轉表識 * @return */ public PushPayload sendAllPush(String title, String message, String to, String... token) { PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.all()) .setAudience(StringUtils.isEmpty(token) ? Audience.all() : Audience.alias("token")) .setMessage(Message.newBuilder() .setMsgContent(message) .setTitle(title) .addExtra("to", to) .build()) .build(); if (apnsProd) payload.resetOptionsApnsProduction(true); return payload; } /** * 指定平臺全用戶透傳 * * @param flag(1:Android,2:IOS,3:全平臺) * @param message * @return */ public Result transmitFlag(String flag, String title, String message) { try { String to = JPushEnum.MESSAGE_SYSTEM.getCode(); PushPayload payload = MessageEnum.ALL_PLATFORM.getCode().equals(flag) ? sendAllPush(title, message, to) : MessageEnum.IOS.getCode().equals(flag) ? sendIosPush(title, message, to) : sendAndroidPush(title, message, to); PushResult result = jpushClient.sendPush(payload); log.info(JSON.toJSONString(result)); return null; } catch (Exception e) { log.error(e.getMessage(), e); return Result.newInstance(Code.ERR, e.getMessage()); } } /** * 安卓全平臺透傳 * * @param message * @param to * @return */ private PushPayload sendAndroidPush(String title, String message, String to) { PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.android()) .setAudience(Audience.all()) .setMessage(Message.newBuilder() .setMsgContent(message) .setTitle(title) .addExtra("to", to) .build()).build(); return payload; } /** * ios全平臺透傳 * * @param message * @param to * @return */ private PushPayload sendIosPush(String title, String message, String to) { PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.ios()) .setAudience(Audience.all()) .setMessage(Message.newBuilder() .setMsgContent(message) .setTitle(title) .addExtra("to", to) .build()).build(); if (apnsProd) payload.resetOptionsApnsProduction(true); return payload; } }