极光推送工具类
以下是本人开发期间整理出来的极光推送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; } }
作者:JackpotHan
欢迎任何形式的转载,但请务必注明出处。
限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示