JGMessageHelper.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.beans.factory.DisposableBean;
  8. import org.springframework.beans.factory.InitializingBean;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import com.goafanti.common.utils.LoggerUtils;
  12. import com.goafanti.message.bo.MessageAdapter;
  13. import com.goafanti.message.bo.MessageBo;
  14. import com.goafanti.message.push.model.Message;
  15. import com.goafanti.message.push.model.Platform;
  16. import com.goafanti.message.push.model.PushPayload;
  17. import com.goafanti.message.push.model.audience.Audience;
  18. import com.goafanti.message.push.model.audience.AudienceTarget;
  19. import com.goafanti.message.queue.MessageRedisQueue;
  20. import cn.jiguang.common.ClientConfig;
  21. import cn.jiguang.common.ServiceHelper;
  22. import cn.jiguang.common.connection.NettyHttpClient;
  23. import cn.jiguang.common.resp.ResponseWrapper;
  24. /**
  25. * 极光推送
  26. * @author Administrator
  27. *
  28. */
  29. public class JGMessageHelper implements InitializingBean, DisposableBean{
  30. @Value(value = "${jiguang.appKey}")
  31. private String appKey;
  32. @Value(value = "${jiguang.masterSecret}")
  33. private String masterSecret;
  34. @Value(value = "${jiguang.pushUrl}")
  35. private String pushUrl;
  36. @Autowired
  37. private MessageRedisQueue jedisQueue;
  38. private static final int UN_AUTH = 401;//未验证
  39. private static final int NOT_SUITABLE = 405;//不支持该请求方法
  40. private static final int EXCEED_THE_LIMIT = 429;//超过频率限制
  41. private static final Logger logger = LoggerFactory.getLogger(JGMessageHelper.class);
  42. private static final int MAX_TRYCOUNT = 3;
  43. private static final String MESSAGE_ID = "message_id";//消息ID
  44. private static final String RESOURCE_ID = "resource_id";//资源ID
  45. private static final String RESOURCE_TYPE = "resource_type";//资源类型
  46. public void send(MessageAdapter messageAdapter){
  47. ClientConfig clientConfig = ClientConfig.getInstance();
  48. String host = (String) clientConfig.get(ClientConfig.PUSH_HOST_NAME);
  49. final NettyHttpClient client = new NettyHttpClient(ServiceHelper.getBasicAuthorization(appKey, masterSecret), null, clientConfig);
  50. try {
  51. URI uri = new URI(host + clientConfig.get(ClientConfig.PUSH_PATH));
  52. PushPayload payload = messageAdapter.getPushPayload();
  53. client.sendRequest(HttpMethod.POST, payload.toString(), uri, new NettyHttpClient.BaseCallback() {
  54. @Override
  55. public void onSucceed(ResponseWrapper responseWrapper) {
  56. logger.info("Got result: " + responseWrapper.responseContent);
  57. if(responseWrapper.responseCode == UN_AUTH){
  58. LoggerUtils.debug(logger, "Request Not Authorized");
  59. }else if(responseWrapper.responseCode == NOT_SUITABLE){
  60. LoggerUtils.debug(logger, "Request Not Allow");
  61. }else if(responseWrapper.responseCode == EXCEED_THE_LIMIT){
  62. LoggerUtils.debug(logger, "Request Too Frequent");
  63. }
  64. }
  65. });
  66. } catch (Exception e) {
  67. sendLater(messageAdapter);
  68. e.printStackTrace();
  69. }finally {
  70. client.close();
  71. }
  72. }
  73. public void sendLater(MessageAdapter messageAdapter) {
  74. if(messageAdapter.getTryCount() < MAX_TRYCOUNT)
  75. jedisQueue.pushFromTail(messageAdapter.tryCount(messageAdapter.getTryCount()+1));
  76. }
  77. public void sendSystemMessage(MessageBo mb){
  78. MessageAdapter ma = new MessageAdapter();
  79. ma.pushPayload(PushPayload.newBuilder()
  80. .setPlatform(Platform.android_ios())
  81. .setAudience(Audience.newBuilder()
  82. .addAudienceTarget(
  83. AudienceTarget.registrationId(mb.getRegistrationId()))
  84. .build())
  85. .setMessage(Message.newBuilder()
  86. .setTitle(mb.getMessageTitle())
  87. .setMsgContent(mb.getMessageBody())
  88. .addExtra(MESSAGE_ID,mb.getMessageId())
  89. .addExtra(RESOURCE_ID,mb.getResourceId())
  90. .addExtra(RESOURCE_TYPE, mb.getResourceType())
  91. .build())
  92. .build());
  93. send(ma);
  94. }
  95. public void sendSystemMessage(MessageBo... mb){
  96. for(MessageBo bo : mb){
  97. sendSystemMessage(bo);
  98. }
  99. }
  100. public void sendSystemMessage(List<MessageBo> mb){
  101. for(MessageBo bo : mb){
  102. sendSystemMessage(bo);
  103. }
  104. }
  105. @Override
  106. public void destroy() throws Exception {
  107. LoggerUtils.debug(logger, "消息系统关闭");
  108. }
  109. @Override
  110. public void afterPropertiesSet() throws Exception {
  111. }
  112. public static void main(String[] args) {
  113. PushPayload pp = PushPayload.newBuilder()
  114. .setPlatform(Platform.android_ios())
  115. .setAudience(Audience.newBuilder()
  116. .addAudienceTarget(
  117. AudienceTarget.registrationId("极光id"))
  118. .build())
  119. .setMessage(Message.newBuilder()
  120. .setTitle("消息标题")
  121. .setMsgContent("消息内容")
  122. .addExtra(MESSAGE_ID,"消息id")
  123. .addExtra(RESOURCE_ID,"资源id")
  124. .addExtra(RESOURCE_TYPE, "资源类型")
  125. .build())
  126. .build();
  127. System.out.println(pp.toString());
  128. }
  129. }