AbstractJPushToolUtil.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package com.ruoyi.common.utils.jPush;
  2. import cn.jiguang.common.resp.APIConnectionException;
  3. import cn.jiguang.common.resp.APIRequestException;
  4. import cn.jpush.api.JPushClient;
  5. import cn.jpush.api.push.PushResult;
  6. import cn.jpush.api.push.model.Message;
  7. import cn.jpush.api.push.model.Options;
  8. import cn.jpush.api.push.model.Platform;
  9. import cn.jpush.api.push.model.PushPayload;
  10. import cn.jpush.api.push.model.audience.Audience;
  11. import cn.jpush.api.push.model.notification.AndroidNotification;
  12. import cn.jpush.api.push.model.notification.IosAlert;
  13. import cn.jpush.api.push.model.notification.Notification;
  14. import com.alibaba.fastjson2.JSON;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.stereotype.Component;
  18. import javax.annotation.PostConstruct;
  19. @Component
  20. public abstract class AbstractJPushToolUtil {
  21. private static final Logger log = LoggerFactory.getLogger(AbstractJPushToolUtil.class);
  22. /*@Autowired
  23. private JPushConfig jPush;*/
  24. protected static AbstractJPushToolUtil abstractJPushToolUtils;
  25. @PostConstruct
  26. public void init() {
  27. abstractJPushToolUtils = this;
  28. }
  29. private final static String appKey = "a6413fdbfa71dd4f27f4c9f4";
  30. private final static String masterSecret = "def1b7b57c7924339773e2cb";
  31. private static JPushClient jPushClient = new JPushClient(masterSecret, appKey);
  32. /**
  33. * 发送给指定极光Id
  34. *
  35. * @param registrationId
  36. * @param notificationTitle
  37. * @param msgTitle
  38. * @param msgContent
  39. * @param jPushVO
  40. * @return
  41. */
  42. public static boolean sendToRegistrationId1(String notificationTitle, String
  43. msgTitle, String msgContent, String jPushVO, String... registrationId) {
  44. boolean result = false;
  45. try {
  46. PushPayload pushPayload = buildPushObject_all_registrationId_alertWithTitle
  47. (notificationTitle, msgTitle, msgContent, jPushVO, registrationId);
  48. //TODO
  49. if(pushPayload == null){
  50. return result;
  51. }
  52. log.info("极光推送入参信息pushPayload:{}", JSON.toJSONString(pushPayload));
  53. PushResult pushResult = jPushClient.sendPush(pushPayload);
  54. log.info("极光推送出参信息pushResult:{}", JSON.toJSONString(pushResult));
  55. if (pushResult.getResponseCode() == 200) {
  56. result = true;
  57. }
  58. } catch (APIConnectionException e) {
  59. e.printStackTrace();
  60. } catch (APIRequestException e) {
  61. e.printStackTrace();
  62. }
  63. return result;
  64. }
  65. /**
  66. * 给指定设备id推送
  67. *
  68. * @param notificationTitle 通知标题
  69. * @param msgTitle 消息标题
  70. * @param msgContent 消息内容
  71. * @param jPushVO 附加字段
  72. * @param registrationId 设备id
  73. * @return
  74. */
  75. private static PushPayload buildPushObject_all_registrationId_alertWithTitle(
  76. String notificationTitle, String msgTitle, String msgContent, String jPushVO, String... registrationId) {
  77. return PushPayload.newBuilder()
  78. //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
  79. .setPlatform(Platform.all())
  80. //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
  81. .setAudience(Audience.registrationId(registrationId))
  82. //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
  83. .setNotification(Notification.newBuilder()
  84. //指定当前推送的android通知
  85. .addPlatformNotification(
  86. AndroidNotification.newBuilder()
  87. .setTitle(notificationTitle)
  88. .setAlert(msgContent)
  89. //此字段为透传字段(类型被极光限定,不能传object),不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
  90. .addExtra("data", JSON.toJSONString(jPushVO))
  91. .build())
  92. .build())
  93. //Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
  94. // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
  95. // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
  96. .setMessage(Message.newBuilder()
  97. .setMsgContent(msgContent)
  98. .setTitle(msgTitle)
  99. .addExtra("data", JSON.toJSONString(jPushVO))
  100. .build())
  101. .setOptions(Options.newBuilder()
  102. //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
  103. .setApnsProduction(false)
  104. //此字段是给开发者自己给推送编号,方便推送者分辨推送记录
  105. .setSendno(1)
  106. //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;
  107. .setTimeToLive(86400)
  108. .build())
  109. .build();
  110. }
  111. /**
  112. * 获取IOS的IosAlert
  113. *
  114. * @param notificationTitle
  115. * @param subTitle
  116. * @param msgContent
  117. * @return
  118. */
  119. private static IosAlert queryIosAlert(String notificationTitle, String subTitle, String msgContent) {
  120. return IosAlert.newBuilder().setTitleAndBody(notificationTitle, subTitle, msgContent).build();
  121. }
  122. }