cdb101befb05bd88d60a9ec397934800affeedd2.svn-base 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.iamberry.app.api.util;
  2. import java.util.HashMap;
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import cn.jpush.api.JPushClient;
  7. import cn.jpush.api.common.resp.APIConnectionException;
  8. import cn.jpush.api.common.resp.APIRequestException;
  9. import cn.jpush.api.push.PushResult;
  10. import cn.jpush.api.push.model.Options;
  11. import cn.jpush.api.push.model.Platform;
  12. import cn.jpush.api.push.model.PushPayload;
  13. import cn.jpush.api.push.model.PushPayload.Builder;
  14. import cn.jpush.api.push.model.audience.Audience;
  15. import cn.jpush.api.push.model.notification.AndroidNotification;
  16. import cn.jpush.api.push.model.notification.IosNotification;
  17. import cn.jpush.api.push.model.notification.Notification;
  18. import com.iamberry.app.core.dto.JpushMessageDTO;
  19. import com.iamberry.app.core.entity.Message;
  20. public class Jdpush {
  21. protected static final Logger LOG = LoggerFactory.getLogger(Jdpush.class);
  22. public static final String JpushAppkey ="4aed8013f682a80294510d00";
  23. public static final String JpushSecret ="b7228c86effa5468c04983f5";
  24. public static final boolean APNOPTION = true; //True 表示推送生产环境,False 表示要推送开发环境
  25. public static final String PREFIX = "iamberry_"; //别名,前缀
  26. public static final HashMap<String, Platform> Platforms = new HashMap<String, Platform>(); //平台
  27. public static final HashMap<Integer, String> allResultCode = new HashMap<Integer, String>(); //结果集
  28. /*
  29. platform 必填 推送平台设置
  30. audience 必填 推送设备指定
  31. notification 可选 通知内容体。是被推送到客户端的内容。与 message 一起二者必须有其一,可以二者并存
  32. message 可选 消息内容体。是被推送到客户端的内容。与 notification 一起二者必须有其一,可以二者并存
  33. sms_message 可选 短信渠道补充送达内容体
  34. options 可选 推送参数
  35. */
  36. static{
  37. Platforms.put("all", Platform.all()); //all
  38. Platforms.put("android", Platform.android()); //android
  39. Platforms.put("ios", Platform.ios()); //ios
  40. allResultCode.put(200, "发送成功!");allResultCode.put(400, "错误的请求!");
  41. allResultCode.put(401, "未验证");allResultCode.put(403, "被拒绝");
  42. allResultCode.put(404, "无法找到");allResultCode.put(504, "代理超时");
  43. }
  44. public static String SendPush(PushPayload payload) {
  45. JPushClient jpushClient = new JPushClient(JpushSecret,JpushAppkey); //推送客户端
  46. //生成推送的内容,
  47. PushResult result = null;
  48. try {
  49. System.out.println(payload.toString());
  50. result = jpushClient.sendPush(payload);
  51. LOG.info("Got result - " + result);
  52. LOG.info("Got result code " + result.getResponseCode());
  53. String responseString = allResultCode.get(result.getResponseCode());
  54. if(StringUtils.isNotBlank(responseString)){
  55. return responseString;
  56. }
  57. } catch (APIConnectionException e) {
  58. LOG.error("Connection error. Should retry later. ", e);
  59. } catch (APIRequestException e) {
  60. LOG.error("Error response from JPush server. Should review and fix it. ", e);
  61. LOG.info("HTTP Status: " + e.getStatus());
  62. LOG.info("Error Code: " + e.getErrorCode());
  63. LOG.info("Error Message: " + e.getErrorMessage());
  64. LOG.info("Msg ID: " + e.getMsgId());
  65. }
  66. return String.valueOf(result.getResponseCode());
  67. }
  68. /**
  69. *手动发送消息到-----> 某平台,某消息,某通知
  70. * @param jpushmessage 参数类
  71. * @return 消息封装类
  72. */
  73. public static String sendmessage_jpushmessage(JpushMessageDTO jpushmessage) {
  74. PushPayload pushpayload = PushPayload.newBuilder()
  75. .setPlatform(Platforms.get(jpushmessage.getPlatform()))
  76. .setAudience(Audience.alias(PREFIX+jpushmessage.getTag()))
  77. .setNotification(Notification.alert(jpushmessage.getContent()) )//通知消息是 content
  78. .setOptions(Options.newBuilder()
  79. .setApnsProduction(APNOPTION) //True 表示推送生产环境,False 表示要推送开发环境
  80. .build())
  81. .build();
  82. return SendPush(pushpayload);
  83. }
  84. /**
  85. * 程序触发,手动触发
  86. * 若别名存在-->则表示对某个人发送,不存在,则表示对所有人
  87. * @return 调用结果
  88. */
  89. public static String buildPushObject_all_notifi(Message messsage) {
  90. Builder builder = PushPayload.newBuilder(); //所有平台
  91. //备注中,all,ios,android,其他表示 all,
  92. if(Platforms.containsKey(messsage.getRemark())){
  93. builder.setPlatform(Platforms.get(messsage.getRemark()));
  94. }else{
  95. builder.setPlatform(Platform.all());
  96. }
  97. if(StringUtils.isNoneEmpty(Long.toString(messsage.getUser()))){
  98. builder.setAudience(Audience.alias(PREFIX+messsage.getUser())); //某个别名
  99. }
  100. builder.setNotification(Notification.newBuilder()
  101. .setAlert(messsage.getContent()) //通知消息是alert content
  102. .addPlatformNotification(AndroidNotification.newBuilder()
  103. .setTitle(messsage.getTitle())
  104. .addExtra("forword", messsage.getForword()) //是否在内部打开1内部2外部
  105. .addExtra("send_type", messsage.getSend_type())//推送的类型1文本 2url 3 内部页面
  106. .addExtra("url", messsage.getUrl())//消息URL
  107. .build())
  108. .addPlatformNotification(IosNotification.newBuilder()
  109. .addExtra("forword", messsage.getForword()) //是否在内部打开1内部2外部
  110. .addExtra("send_type", messsage.getSend_type())//推送的类型1文本 2url 3 内部页面
  111. .addExtra("url", messsage.getUrl())//消息URL
  112. .build())
  113. .build());
  114. //设置是开发环境还是,生产环境
  115. builder.setOptions(Options.newBuilder()
  116. .setApnsProduction(APNOPTION)
  117. .build());
  118. return SendPush(builder.build());
  119. }
  120. }