wanghui před 8 roky
rodič
revize
d6de1fab8e

+ 44 - 25
src/main/java/com/goafanti/message/JGMessageHelper.java

@@ -16,11 +16,13 @@ import org.springframework.beans.factory.annotation.Value;
 import com.goafanti.common.utils.LoggerUtils;
 import com.goafanti.message.bo.MessageAdapter;
 import com.goafanti.message.bo.MessageBo;
-import com.goafanti.message.push.model.Message;
 import com.goafanti.message.push.model.Platform;
 import com.goafanti.message.push.model.PushPayload;
 import com.goafanti.message.push.model.audience.Audience;
 import com.goafanti.message.push.model.audience.AudienceTarget;
+import com.goafanti.message.push.model.notification.AndroidNotification;
+import com.goafanti.message.push.model.notification.IosNotification;
+import com.goafanti.message.push.model.notification.Notification;
 import com.goafanti.message.queue.MessageRedisQueue;
 
 import cn.jiguang.common.ClientConfig;
@@ -35,9 +37,9 @@ import cn.jiguang.common.resp.ResponseWrapper;
  */
 public class JGMessageHelper implements InitializingBean, DisposableBean{
 	@Value(value = "${jiguang.appKey}")
-	private String appKey; 
+	private String appKey = "36944c5628d34ff997f56dcd"; 
 	@Value(value = "${jiguang.masterSecret}")
-	private String masterSecret;
+	private String masterSecret = "cceff6a22d91c06786bfdcd3";
 	@Value(value = "${jiguang.pushUrl}")
 	private String pushUrl;
 	@Autowired
@@ -52,17 +54,16 @@ public class JGMessageHelper implements InitializingBean, DisposableBean{
 	private static final String RESOURCE_TYPE = "resource_type";//资源类型
 	public void send(MessageAdapter messageAdapter){
 		ClientConfig clientConfig = ClientConfig.getInstance();
-	    String host = (String) clientConfig.get(ClientConfig.PUSH_HOST_NAME);
+	   String host = (String) clientConfig.get(ClientConfig.PUSH_HOST_NAME);
         final NettyHttpClient client = new NettyHttpClient(ServiceHelper.getBasicAuthorization(appKey, masterSecret), null, clientConfig);
         try {
             URI uri = new URI(host + clientConfig.get(ClientConfig.PUSH_PATH));
             PushPayload payload = messageAdapter.getPushPayload();
-           if(payload == null) System.out.println("*******************shit*********************");
-            System.out.println(payload.toJSON());
+            logger.info(payload.toJSON().toString());
             client.sendRequest(HttpMethod.POST, payload.toString(), uri, new NettyHttpClient.BaseCallback() {
                 @Override
                 public void onSucceed(ResponseWrapper responseWrapper) {
-                    logger.info("Got result: " + responseWrapper.responseContent);
+                    logger.info("Got result Content: " + responseWrapper.responseContent + "Code:" +responseWrapper.responseCode);
                     if(responseWrapper.responseCode == UN_AUTH){
                     	LoggerUtils.debug(logger, "Request Not Authorized");
                     }else if(responseWrapper.responseCode == NOT_SUITABLE){
@@ -94,13 +95,21 @@ public class JGMessageHelper implements InitializingBean, DisposableBean{
 							.addAudienceTarget(
 									AudienceTarget.registrationId(mb.getRegistrationId()))
 							.build())
-					.setMessage(Message.newBuilder()
-							.setTitle(mb.getMessageTitle())
-							.setMsgContent(mb.getMessageBody())
-							.addExtra(MESSAGE_ID,mb.getMessageId())
-							.addExtra(RESOURCE_ID,mb.getResourceId() == null ? "" : mb.getResourceId())
-							.addExtra(RESOURCE_TYPE, mb.getResourceType()== null ? "": mb.getResourceType())
-							.build())
+					.setNotification(Notification.newBuilder()
+			        		.setAlert(mb.getMessageTitle())
+			        		.addPlatformNotification(AndroidNotification.newBuilder()
+			        				.addExtra(MESSAGE_ID, mb.getMessageId())
+			        				.addExtra(RESOURCE_ID, mb.getResourceId() == null ? "" : mb.getResourceId())
+			        				.addExtra(RESOURCE_TYPE, mb.getResourceType()== null ? "": mb.getResourceType())
+			        				.build())
+			        		.addPlatformNotification(IosNotification.newBuilder()
+			        				.incrBadge(1)
+			        				.setContentAvailable(true)
+			        				.addExtra(MESSAGE_ID, mb.getMessageId())
+			        				.addExtra(RESOURCE_ID, mb.getResourceId() == null ? "" : mb.getResourceId())
+			        				.addExtra(RESOURCE_TYPE, mb.getResourceType()== null ? "": mb.getResourceType())
+			        				.build())
+			        		.build())
 					.build());
 			send(ma);
 		}
@@ -129,20 +138,30 @@ public class JGMessageHelper implements InitializingBean, DisposableBean{
 	}
 
 	public static void main(String[] args) {
-		PushPayload pp = PushPayload.newBuilder()
+		JGMessageHelper jgMessageHelper = new JGMessageHelper();
+		MessageAdapter ma = new MessageAdapter().pushPayload( 
+				PushPayload.newBuilder()
 		.setPlatform(Platform.android_ios())
 		.setAudience(Audience.newBuilder()
 				.addAudienceTarget(
-						AudienceTarget.registrationId("极光id"))
-				.build())
-		.setMessage(Message.newBuilder()
-				.setTitle("消息标题")
-				.setMsgContent("消息内容")
-				.addExtra(MESSAGE_ID,"消息id")
-				.addExtra(RESOURCE_ID,"资源id")
-				.addExtra(RESOURCE_TYPE, "资源类型")
+						AudienceTarget.registrationId("101d8559094270e0d78"))
 				.build())
-		.build();
-		System.out.println(pp.toString());
+		.setNotification(Notification.newBuilder()
+        		.setAlert("消息标题")
+        		.addPlatformNotification(AndroidNotification.newBuilder()
+        				.addExtra(MESSAGE_ID,"消息id")
+        				.addExtra(RESOURCE_ID,"资源id")
+        				.addExtra(RESOURCE_TYPE, "资源类型")
+        				.build())
+        		.addPlatformNotification(IosNotification.newBuilder()
+        				.incrBadge(1)
+        				.setContentAvailable(true)
+        				.addExtra(MESSAGE_ID,"消息id")
+        				.addExtra(RESOURCE_ID,"资源id")
+        				.addExtra(RESOURCE_TYPE, "资源类型")
+        				.build())
+        		.build())
+		.build());
+		jgMessageHelper.send(ma);
 	}
 }