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 com.zmkj.base.util.StringUtils; import com.zmkj.core.enums.JPushEnum; import com.zmkj.core.enums.MessageEnum; import com.zmkj.core.zm.ZmCode; import com.zmkj.core.zm.ZmResult; 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: jackpot * @Date: 2019/2/01 11:23 * @Description: 极光消息推送 */ @Component @Slf4j public class JPushMessage { @Value("${your appKey}") private String jpushAppKey; @Value("${your masterSecret}") private String jpushMasterSecret; @Value("${your apnsProd}") 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 ZmResult 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 ZmResult.newInstance(ZmCode.ERR, e.getMessage()); } } /** * 指定用户极光定时推送 * * @param message * @param to * @param token * @return */ public ZmResult 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 ZmResult.newInstance(ZmCode.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 ( to true) .build ()); return payload.build (); } / ** * full internet user specified push * time is not empty - the timing of the push * * @param In Flag (. 1: the Android, 2: the IOS,. 3: All ) * @param Message * @return * / public ZmResult pushFlag (In Flag Integer, String title, String Message, the LocalDateTime Time) { the try { String to = JPushEnum.MESSAGE_SYSTEM.getCode (); PushPayload payload = MessageEnum.ALL_PLATFORM.getCode () ? .equals (Flag) pushAllNotify (the Message, to): . MessageEnum.IOS.getCode () the equals (Flag) ? PushIosNotify (the Message, to): pushAndroidNotify (the Message, to); // time is empty, compared with the normal push IF (StringUtils.isEmpty (Time)) { PushResult the Result = jpushClient .sendPush (payload); log.info (JSON.toJSONString (Result)); } // time was not timed push the else { ScheduleResult Result = jpushClient.createSingleSchedule (title, dtf.format (time), payload); log.info (JSON.toJSONString (Result)); } return null ; } the catch (Exception e) { log.error(e.getMessage(), e); return ZmResult.newInstance(ZmCode.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 ZmResult 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 ZmResult.newInstance(ZmCode.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 ( to true ); return payload; } / ** * full internet user specifies passthrough * * @param In Flag (. 1: the Android, 2: the IOS,. 3 : all platforms) * @param Message * @return * / public ZmResult transmitFlag (In Flag String, String title, String Message) { the try { PushPayload payload = null ; String to = JPushEnum.MESSAGE_SYSTEM.getCode(); //全平台 if (flag.equals("3")) { payload = sendAllPush(title, message, to); } //ios else if (flag.equals("1")) { payload = sendIosPush(title, message, to); } //安卓 else if (flag.equals("2")) { payload = 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 ZmResult.newInstance(ZmCode.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; } }