JGMessageHelper.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package com.goafanti.message;
  2. import java.net.URI;
  3. import java.util.List;
  4. import io.netty.handler.codec.http.HttpMethod;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.beans.factory.DisposableBean;
  9. import org.springframework.beans.factory.InitializingBean;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import com.goafanti.common.model.JtMessageProducer;
  13. import com.goafanti.common.utils.LoggerUtils;
  14. import com.goafanti.message.bo.MessageAdapter;
  15. import com.goafanti.message.bo.MessageBo;
  16. import com.goafanti.message.push.model.Platform;
  17. import com.goafanti.message.push.model.PushPayload;
  18. import com.goafanti.message.push.model.audience.Audience;
  19. import com.goafanti.message.push.model.audience.AudienceTarget;
  20. import com.goafanti.message.push.model.notification.AndroidNotification;
  21. import com.goafanti.message.push.model.notification.IosNotification;
  22. import com.goafanti.message.push.model.notification.Notification;
  23. import com.goafanti.message.queue.MessageRedisQueue;
  24. import cn.jiguang.common.ClientConfig;
  25. import cn.jiguang.common.ServiceHelper;
  26. import cn.jiguang.common.connection.NettyHttpClient;
  27. import cn.jiguang.common.resp.ResponseWrapper;
  28. /**
  29. * 极光推送
  30. * @author Administrator
  31. *
  32. */
  33. public class JGMessageHelper implements InitializingBean, DisposableBean{
  34. @Value(value = "${jiguang.appKey}")
  35. private String appKey;
  36. @Value(value = "${jiguang.masterSecret}")
  37. private String masterSecret;
  38. @Value(value = "${jiguang.pushUrl}")
  39. private String pushUrl;
  40. @Autowired
  41. private MessageRedisQueue jedisQueue;
  42. private static final int UN_AUTH = 401;//未验证
  43. private static final int NOT_SUITABLE = 405;//不支持该请求方法
  44. private static final int EXCEED_THE_LIMIT = 429;//超过频率限制
  45. private static final Logger logger = LoggerFactory.getLogger(JGMessageHelper.class);
  46. private static final int MAX_TRYCOUNT = 3;
  47. private static final String MESSAGE_ID = "message_id";//消息ID
  48. private static final String MESSAGE_TITLE = "message_title";//消息ID
  49. private static final String MESSAGE_BODY = "message_body";//消息ID
  50. private static final String RESOURCE_ID = "resource_id";//资源ID
  51. private static final String RESOURCE_TYPE = "resource_type";//资源类型
  52. public void send(MessageAdapter messageAdapter){
  53. ClientConfig clientConfig = ClientConfig.getInstance();
  54. String host = (String) clientConfig.get(ClientConfig.PUSH_HOST_NAME);
  55. final NettyHttpClient client = new NettyHttpClient(ServiceHelper.getBasicAuthorization(appKey, masterSecret), null, clientConfig);
  56. try {
  57. URI uri = new URI(host + clientConfig.get(ClientConfig.PUSH_PATH));
  58. PushPayload payload = messageAdapter.getPushPayload();
  59. logger.info(payload.toJSON().toString());
  60. client.sendRequest(HttpMethod.POST, payload.toString(), uri, new NettyHttpClient.BaseCallback() {
  61. @Override
  62. public void onSucceed(ResponseWrapper responseWrapper) {
  63. logger.info("Got result Content: " + responseWrapper.responseContent + "Code:" +responseWrapper.responseCode);
  64. if(responseWrapper.responseCode == UN_AUTH){
  65. LoggerUtils.debug(logger, "Request Not Authorized");
  66. }else if(responseWrapper.responseCode == NOT_SUITABLE){
  67. LoggerUtils.debug(logger, "Request Not Allow");
  68. }else if(responseWrapper.responseCode == EXCEED_THE_LIMIT){
  69. LoggerUtils.debug(logger, "Request Too Frequent");
  70. }
  71. }
  72. });
  73. } catch (Exception e) {
  74. sendLater(messageAdapter);
  75. e.printStackTrace();
  76. }finally {
  77. client.close();
  78. }
  79. }
  80. public void sendLater(MessageAdapter messageAdapter) {
  81. if(messageAdapter.getTryCount() < MAX_TRYCOUNT)
  82. jedisQueue.pushFromTail(messageAdapter.tryCount(messageAdapter.getTryCount()+1));
  83. }
  84. public void sendSystemMessage(MessageBo mb){
  85. MessageAdapter ma = new MessageAdapter();
  86. if(StringUtils.isNotBlank(mb.getRegistrationId())){
  87. ma.pushPayload(PushPayload.newBuilder()
  88. .setPlatform(Platform.android_ios())
  89. .setAudience(Audience.newBuilder()
  90. .addAudienceTarget(
  91. AudienceTarget.registrationId(mb.getRegistrationId()))
  92. .build())
  93. .setNotification(Notification.newBuilder()
  94. .setAlert(mb.getMessageTitle())
  95. .addPlatformNotification(AndroidNotification.newBuilder()
  96. .addExtra(MESSAGE_ID, mb.getMessageId())
  97. .addExtra(RESOURCE_ID, mb.getResourceId() == null ? "" : mb.getResourceId())
  98. .addExtra(RESOURCE_TYPE, mb.getResourceType()== null ? "": mb.getResourceType())
  99. .build())
  100. .addPlatformNotification(IosNotification.newBuilder()
  101. .incrBadge(1)
  102. .setContentAvailable(true)
  103. .addExtra(MESSAGE_ID, mb.getMessageId())
  104. .addExtra(RESOURCE_ID, mb.getResourceId() == null ? "" : mb.getResourceId())
  105. .addExtra(RESOURCE_TYPE, mb.getResourceType()== null ? "": mb.getResourceType())
  106. .build())
  107. .build())
  108. .build());
  109. send(ma);
  110. }
  111. }
  112. public void sendPushMessage(JtMessageProducer mp){
  113. MessageAdapter ma = new MessageAdapter();
  114. if(mp !=null&&StringUtils.isNotBlank(mp.getId())){
  115. ma.pushPayload(PushPayload.newBuilder()
  116. .setPlatform(Platform.android_ios())
  117. .setAudience(Audience.all())
  118. .setNotification(Notification.newBuilder()
  119. .setAlert("")
  120. .addPlatformNotification(AndroidNotification.newBuilder()
  121. .addExtra(MESSAGE_ID, mp.getId())
  122. .addExtra(MESSAGE_TITLE, mp.getTitle())
  123. .addExtra(MESSAGE_BODY, mp.getBody())
  124. .addExtra(RESOURCE_ID, mp.getResourceId() == null ? "" : mp.getResourceId())
  125. .addExtra(RESOURCE_TYPE, mp.getResourceType() == null ? "":mp.getResourceType().toString()).build())
  126. .addPlatformNotification(IosNotification.newBuilder()
  127. .incrBadge(1)
  128. .setContentAvailable(true)
  129. .addExtra(MESSAGE_ID, mp.getId())
  130. .addExtra(MESSAGE_TITLE, mp.getTitle())
  131. .addExtra(MESSAGE_BODY, mp.getBody())
  132. .addExtra(RESOURCE_ID, mp.getResourceId() == null ? "" : mp.getResourceId())
  133. .addExtra(RESOURCE_TYPE, mp.getResourceType()== null ? "": mp.getResourceType().toString()).build())
  134. .build())
  135. .build());
  136. send(ma);
  137. }
  138. }
  139. public void sendSystemMessage(MessageBo... mb){
  140. for(MessageBo bo : mb){
  141. sendSystemMessage(bo);
  142. }
  143. }
  144. public void sendSystemMessage(List<MessageBo> mb){
  145. for(MessageBo bo : mb){
  146. sendSystemMessage(bo);
  147. }
  148. }
  149. @Override
  150. public void destroy() throws Exception {
  151. LoggerUtils.debug(logger, "消息系统关闭");
  152. }
  153. @Override
  154. public void afterPropertiesSet() throws Exception {
  155. }
  156. public static void main(String[] args) {
  157. JGMessageHelper jgMessageHelper = new JGMessageHelper();
  158. MessageAdapter ma = new MessageAdapter().pushPayload(
  159. PushPayload.newBuilder()
  160. .setPlatform(Platform.android_ios())
  161. .setAudience(Audience.all())
  162. .setNotification(Notification.newBuilder()
  163. .setAlert("消息标题")
  164. .addPlatformNotification(AndroidNotification.newBuilder()
  165. .addExtra(MESSAGE_ID,"消息id")
  166. .addExtra(RESOURCE_ID,"资源id")
  167. .addExtra(RESOURCE_TYPE, "资源类型")
  168. .build())
  169. .addPlatformNotification(IosNotification.newBuilder()
  170. .incrBadge(1)
  171. .setContentAvailable(true)
  172. .addExtra(MESSAGE_ID,"消息id")
  173. .addExtra(RESOURCE_ID,"资源id")
  174. .addExtra(RESOURCE_TYPE, "资源类型")
  175. .build())
  176. .build())
  177. .build());
  178. jgMessageHelper.send(ma);
  179. }
  180. }