JPushUtils.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package com.platform.yijia.utils;
  2. import cn.jiguang.common.ClientConfig;
  3. import cn.jpush.api.JPushClient;
  4. import cn.jpush.api.push.PushResult;
  5. import cn.jpush.api.push.model.Message;
  6. import cn.jpush.api.push.model.Options;
  7. import cn.jpush.api.push.model.Platform;
  8. import cn.jpush.api.push.model.PushPayload;
  9. import cn.jpush.api.push.model.audience.Audience;
  10. import cn.jpush.api.push.model.notification.AndroidNotification;
  11. import cn.jpush.api.push.model.notification.IosNotification;
  12. import cn.jpush.api.push.model.notification.Notification;
  13. import com.alibaba.fastjson.JSON;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.stereotype.Component;
  18. import java.time.format.DateTimeFormatter;
  19. /***
  20. * <Title> JPushUtils </Title>
  21. * <Description> 极光推送工具类 </Description>
  22. * @author JK
  23. * @date 2020年12月29日
  24. */
  25. //@Component
  26. public class JPushUtils {
  27. private static Logger logger =(Logger) LoggerFactory.getLogger(JPushUtils.class); //日志
  28. //@Value("${jpush.app.key}")
  29. private String jpushAppKey; //appKey d065738741a4ba836a47769b
  30. //@Value("${jpush.master.secret}")
  31. private String jPushMasterSecret; //masterSecret dec5b9ddf8e0834d3bfbf469
  32. //@Value("${jpush.apns.prod}")
  33. private boolean apnsProd; //apnsProd 是否生产环境
  34. private JPushClient jPushClient = new JPushClient(jPushMasterSecret, jpushAppKey, null, ClientConfig.getInstance()); //创建JPushClient对象
  35. //private final static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //格式化时间
  36. /*
  37. * 推送给设备标识参数的用户
  38. * @param registrationId 设备标识 JPushInterface.init(this); registrationID = JPushInterface.getRegistrationID(this);
  39. * @param notificationTitle 通知内容标题 发送的内容(会显示APP端的通知栏)
  40. * @param msgTitle 消息内容标题
  41. * @param msgContent 消息内容 消息对象的JSON形式
  42. * @param extrasParam 扩展字段
  43. * @return false推送失败,true推送成功
  44. */
  45. public boolean sendToRegistrationId(String registrationId, String notificationTitle, String msgTitle, String msgContent, String extrasParam){
  46. boolean flag = false;
  47. try {
  48. PushPayload.Builder payload = PushPayload.newBuilder()
  49. .setPlatform(Platform.android_ios()) //指定要推送的平台 Platform.android_ios()安卓和iOS用户推送
  50. .setAudience(Audience.registrationId(registrationId)) //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
  51. .setNotification(Notification.newBuilder()
  52. .addPlatformNotification(AndroidNotification.newBuilder() //android推送通知
  53. .setTitle(notificationTitle).setAlert(notificationTitle)
  54. .addExtra("androidNotification extras key", extrasParam)
  55. .build()
  56. ).addPlatformNotification(IosNotification.newBuilder() //IOS推送通知
  57. .setAlert(notificationTitle)
  58. .incrBadge(1)
  59. .setSound("sound.caf")
  60. .addExtra("iosNotification extras key", extrasParam)
  61. .build()
  62. ).build()
  63. ).setMessage(Message.newBuilder()
  64. .setTitle(msgTitle)
  65. .setMsgContent(msgContent)
  66. .addExtra("message extras key", extrasParam)
  67. .build()
  68. );
  69. if(apnsProd)
  70. payload.setOptions(Options.newBuilder().setApnsProduction(true).build()); //setApnsProduction的值指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
  71. //开始推送
  72. PushResult pushResult = jPushClient.sendPush(payload.build());
  73. if(pushResult.getResponseCode() == 200){
  74. flag = true; //表示成功
  75. logger.info(JSON.toJSONString(pushResult));
  76. }
  77. } catch (Exception e) {
  78. logger.error(e.getMessage(), e);
  79. }
  80. return flag;
  81. }
  82. /*
  83. * 推送给所有用户
  84. * @param notificationTitle 通知内容标题 发送的内容(会显示APP端的通知栏)
  85. * @param msgTitle 消息内容标题
  86. * @param msgContent 消息内容 消息对象的JSON形式
  87. * @param extrasParam 扩展字段
  88. * @return false推送失败,true推送成功
  89. */
  90. public boolean sendToAll(String notificationTitle, String msgTitle, String msgContent, String extrasParam){
  91. boolean flag = false;
  92. try {
  93. PushPayload.Builder payload = PushPayload.newBuilder()
  94. .setPlatform(Platform.android_ios()) //指定要推送的平台 Platform.android_ios()安卓和iOS用户推送
  95. .setAudience(Audience.all()) //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
  96. .setNotification(Notification.newBuilder()
  97. .addPlatformNotification(AndroidNotification.newBuilder() //android推送通知
  98. .setTitle(notificationTitle).setAlert(notificationTitle)
  99. .addExtra("androidNotification extras key", extrasParam)
  100. .build()
  101. ).addPlatformNotification(IosNotification.newBuilder() //IOS推送通知
  102. .setAlert(notificationTitle)
  103. .incrBadge(1)
  104. .setSound("sound.caf")
  105. .addExtra("iosNotification extras key", extrasParam)
  106. .build()
  107. ).build()
  108. ).setMessage(Message.newBuilder()
  109. .setTitle(msgTitle)
  110. .setMsgContent(msgContent)
  111. .addExtra("message extras key", extrasParam)
  112. .build()
  113. );
  114. if(apnsProd)
  115. payload.setOptions(Options.newBuilder().setApnsProduction(true).build()); //setApnsProduction的值指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
  116. //开始推送
  117. PushResult pushResult = jPushClient.sendPush(payload.build());
  118. if(pushResult.getResponseCode() == 200){
  119. flag = true; //表示成功
  120. logger.info(JSON.toJSONString(pushResult));
  121. }
  122. } catch (Exception e) {
  123. logger.error(e.getMessage(), e);
  124. }
  125. return flag;
  126. }
  127. /*
  128. * IOS平台推送
  129. * @param notificationTitle 通知内容标题 发送的内容(会显示APP端的通知栏)
  130. * @param msgTitle 消息内容标题
  131. * @param msgContent 消息内容 消息对象的JSON形式
  132. * @param extrasParam 扩展字段
  133. * @return false推送失败,true推送成功
  134. */
  135. public boolean sendToIOS(String notificationTitle, String msgTitle, String msgContent, String extrasParam){
  136. boolean flag = false;
  137. try {
  138. PushPayload.Builder payload = PushPayload.newBuilder()
  139. .setPlatform(Platform.ios()) // 指定要推送的平台 Platform.ios()为IOS用户推送
  140. .setAudience(Audience.all()) // 指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
  141. .setNotification(Notification.newBuilder()
  142. .addPlatformNotification(IosNotification.newBuilder() // jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
  143. .setAlert(notificationTitle) // 传一个IosAlert对象,指定apns title、title、subtitle等
  144. .incrBadge(1) // 此项是指定此推送的badge自动加1
  145. .setSound("sound.caf") //传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
  146. .addExtra("iosNotification extras key", extrasParam) // 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
  147. .build()
  148. ).build()
  149. ).setMessage(Message.newBuilder()
  150. .setTitle(msgTitle)
  151. .setMsgContent(msgContent)
  152. .addExtra("message extras key", extrasParam)
  153. .build()
  154. );
  155. if(apnsProd)
  156. payload.setOptions(Options.newBuilder().setApnsProduction(true).build()); //setApnsProduction的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
  157. PushResult pushResult = jPushClient.sendPush(payload.build()); //开始推送
  158. if(pushResult.getResponseCode() == 200){
  159. flag = true; //表示成功
  160. logger.info(JSON.toJSONString(pushResult));
  161. }
  162. } catch (Exception e) {
  163. logger.error(e.getMessage(), e);
  164. }
  165. return flag;
  166. }
  167. /*
  168. * Android平台推送
  169. * @param notificationTitle 通知内容标题 发送的内容(会显示APP端的通知栏)
  170. * @param msgTitle 消息内容标题
  171. * @param msgContent 消息内容 消息对象的JSON形式
  172. * @param extrasParam 扩展字段
  173. * @return false推送失败,true推送成功
  174. */
  175. public boolean sendToAndroid(String notificationTitle, String msgTitle, String msgContent, String extrasParam){
  176. boolean flag = false;
  177. try {
  178. PushPayload.Builder payload = PushPayload.newBuilder()
  179. .setPlatform(Platform.android()) //指定要推送的平台 Platform.android()安卓用户推送
  180. .setAudience(Audience.all()) //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
  181. .setNotification(Notification.newBuilder() //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
  182. .addPlatformNotification(AndroidNotification.newBuilder() //指定当前推送的android通知
  183. .setTitle(notificationTitle).setAlert(notificationTitle)
  184. .addExtra("androidNotification extras key", extrasParam) //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
  185. .build()
  186. ).build()
  187. ).setMessage(Message.newBuilder()
  188. .setTitle(msgTitle)
  189. .setMsgContent(msgContent)
  190. .addExtra("message extras key", extrasParam)
  191. .build()
  192. );
  193. if(apnsProd)
  194. payload.setOptions(Options.newBuilder().setApnsProduction(true).build()); //setApnsProduction的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
  195. //开始推送
  196. PushResult pushResult = jPushClient.sendPush(payload.build());
  197. if(pushResult.getResponseCode() == 200){
  198. flag = true; //表示成功
  199. logger.info(JSON.toJSONString(pushResult));
  200. }
  201. } catch (Exception e) {
  202. logger.error(e.getMessage(), e);
  203. }
  204. return flag;
  205. }
  206. }