在pom.xml文件的
cn.jpush.api jpush-client 3.2.17
创建一个包jpush,包内创建
AbstractJPushToolUtil
JPushConfig
JPushToolUtil
JPushVO
#极光推送 jpush.environment=false jpush.appKey=54456f5d662dfdsg jpush.masterSecret=vf8g4f84f74v8f4d78g4ff48
@Test public void sendToRegistrationId() { JPushVO pushVO = new JPushVO(); pushVO.setId(1L); pushVO.setUserName("橘子皮"); pushVO.setContent("My name is 橘子皮"); System.err.println( JPushToolUtil.sendToRegistrationId( "通知标题", "内标题", "内容", pushVO, "1104a897924ffbec2ce" )); }
@Slf4j @Component public abstract class AbstractJPushToolUtil { @Autowired private JPushConfig jPush; protected static AbstractJPushToolUtil abstractJPushToolUtils; @PostConstruct public void init() { abstractJPushToolUtils = this; }
/** * 发送给指定极光Id * * @param registrationId * @param notificationTitle * @param msgTitle * @param msgContent * @param jPushVO * @return */ public static boolean sendToRegistrationId1(String notificationTitle, String msgTitle, String msgContent, JPushVO jPushVO, String... registrationId) { boolean result = false; try { PushPayload pushPayload = buildPushObject_all_registrationId_alertWithTitle (notificationTitle, msgTitle, msgContent, jPushVO, registrationId); //TODO log.info("极光推送入参信息pushPayload:{}", JSON.toJSONString(pushPayload)); PushResult pushResult = getJPushClient().sendPush(pushPayload); log.info("极光推送出参信息pushResult:{}", JSON.toJSONString(pushResult)); if (pushResult.getResponseCode() == 200) { result = true; } } catch (APIConnectionException e) { e.printStackTrace(); } catch (APIRequestException e) { e.printStackTrace(); } return result; }
/** * 给指定设备id推送 * * @param notificationTitle 通知标题 * @param msgTitle 消息标题 * @param msgContent 消息内容 * @param jPushVO 附加字段 * @param registrationId 设备id * @return */ private static PushPayload buildPushObject_all_registrationId_alertWithTitle( String notificationTitle, String msgTitle, String msgContent, JPushVO jPushVO, String... registrationId) { return PushPayload.newBuilder() //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台 .setPlatform(Platform.all()) //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id .setAudience(Audience.registrationId(registrationId)) //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发 .setNotification(Notification.newBuilder() //指定当前推送的android通知 .addPlatformNotification( AndroidNotification.newBuilder() .setTitle(notificationTitle) .setAlert(msgContent) //此字段为透传字段(类型被极光限定,不能传object),不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) .addExtra("data", JSON.toJSONString(jPushVO)) .build()) //指定当前推送的iOS通知 .addPlatformNotification( queryIosNotification( queryIosAlert(notificationTitle, msgTitle, msgContent),jPushVO)) .build()) //Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息, // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的 // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别 .setMessage(Message.newBuilder() .setMsgContent(msgContent) .setTitle(msgTitle) .addExtra("data", JSON.toJSONString(jPushVO)) .build()) .setOptions(Options.newBuilder() //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义 .setApnsProduction(false) //此字段是给开发者自己给推送编号,方便推送者分辨推送记录 .setSendno(1) //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天; .setTimeToLive(86400) .build()) .build(); }
/** * 获取IOS的IosAlert * * @param notificationTitle * @param subTitle * @param msgContent * @return */ private static IosAlert queryIosAlert(String notificationTitle, String subTitle, String msgContent) { return IosAlert.newBuilder().setTitleAndBody(notificationTitle, subTitle, msgContent).build(); }
}
import lombok.Data; import lombok.experimental.Accessors; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;
@Accessors(chain = true) @Data @Component public class JPushConfig { /** * 极光官网-个人管理中心-点击查看-secret */ @Value("${jpush.masterSecret}") private String masterSecret; /** * 指定本推送要推送的apns环境,true表示生产,false表示开发 */ @Value("${jpush.environment}") private boolean environment; /** * 极光官网-个人管理中心-appkey */ @Value("${jpush.appKey}") private String appKey; }
@Slf4j @Component public class JPushToolUtil extends AbstractJPushToolUtil {
/** * 发送给指定的极光ID */ public static boolean sendToRegistrationId( String notificationTitle, String msgTitle, String msgContent, JPushVO jPushVO, String... registrationId) { return sendToRegistrationId1(notificationTitle,msgTitle,msgContent,jPushVO,registrationId); }
}
import lombok.experimental.Accessors; /** * 极光拓展字段不能传输object,限制了类型,所以封装更好些 * 此类为极光推送拓展字段封装类(目前字段是暂定,你也可以拓展) * * @author juzi * @date 2022-12-2 */ @Accessors(chain = true) public class JPushVO { private Long id; private String userName; private String content; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public String toString() { return "JPushVO{" + "id=" + id + ", userName='" + userName + '\'' + ", content='" + content + '\'' + '}'; } }