package com.ruoyi.common.utils.jPush; 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.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.IosAlert; import cn.jpush.api.push.model.notification.Notification; import com.alibaba.fastjson2.JSON; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @Component public abstract class AbstractJPushToolUtil { private static final Logger log = LoggerFactory.getLogger(AbstractJPushToolUtil.class); /*@Autowired private JPushConfig jPush;*/ protected static AbstractJPushToolUtil abstractJPushToolUtils; @PostConstruct public void init() { abstractJPushToolUtils = this; } private final static String appKey = "a6413fdbfa71dd4f27f4c9f4"; private final static String masterSecret = "def1b7b57c7924339773e2cb"; private static JPushClient jPushClient = new JPushClient(masterSecret, appKey); /** * 发送给指定极光Id * * @param registrationId * @param notificationTitle * @param msgTitle * @param msgContent * @param jPushVO * @return */ public static boolean sendToRegistrationId1(String notificationTitle, String msgTitle, String msgContent, String jPushVO, String... registrationId) { boolean result = false; try { PushPayload pushPayload = buildPushObject_all_registrationId_alertWithTitle (notificationTitle, msgTitle, msgContent, jPushVO, registrationId); //TODO if(pushPayload == null){ return result; } log.info("极光推送入参信息pushPayload:{}", JSON.toJSONString(pushPayload)); PushResult pushResult = jPushClient.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, String 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()) .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(); } }