Quellcode durchsuchen

消息推送开发

anderx vor 7 Jahren
Ursprung
Commit
6671f7c8b3

+ 29 - 4
src/main/java/com/goafanti/message/JGMessageHelper.java

@@ -13,6 +13,7 @@ import org.springframework.beans.factory.InitializingBean;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 
+import com.goafanti.common.model.JtMessageProducer;
 import com.goafanti.common.utils.LoggerUtils;
 import com.goafanti.message.bo.MessageAdapter;
 import com.goafanti.message.bo.MessageBo;
@@ -50,6 +51,7 @@ public class JGMessageHelper implements InitializingBean, DisposableBean{
 	private static final Logger logger = LoggerFactory.getLogger(JGMessageHelper.class);
 	private static final int MAX_TRYCOUNT = 3;
 	private static final String MESSAGE_ID = "message_id";//消息ID
+	private static final String MESSAGE_BODY = "message_body";//消息ID
 	private static final String RESOURCE_ID =  "resource_id";//资源ID
 	private static final String RESOURCE_TYPE = "resource_type";//资源类型
 	public void send(MessageAdapter messageAdapter){
@@ -115,6 +117,32 @@ public class JGMessageHelper implements InitializingBean, DisposableBean{
 		}
 	}
 	
+	public void sendPushMessage(JtMessageProducer mp){
+		MessageAdapter ma = new MessageAdapter();
+		if(mp !=null&&StringUtils.isNotBlank(mp.getId())){
+			ma.pushPayload(PushPayload.newBuilder()
+					.setPlatform(Platform.android_ios())
+					.setAudience(Audience.all())
+					.setNotification(Notification.newBuilder()
+			        		.setAlert(mp.getTitle())
+			        		.addPlatformNotification(AndroidNotification.newBuilder()
+			        				.addExtra(MESSAGE_ID, mp.getId())
+			        				.addExtra(MESSAGE_BODY, mp.getBody())
+			        				.addExtra(RESOURCE_ID, mp.getResourceId() == null ? "" : mp.getResourceId())
+			        				.addExtra(RESOURCE_TYPE, mp.getResourceType() == null ? "":mp.getResourceType().toString()).build())
+			        		.addPlatformNotification(IosNotification.newBuilder()
+			        				.incrBadge(1)
+			        				.setContentAvailable(true)
+			        				.addExtra(MESSAGE_ID, mp.getId())
+			        				.addExtra(MESSAGE_BODY, mp.getBody())
+			        				.addExtra(RESOURCE_ID, mp.getResourceId() == null ? "" : mp.getResourceId())
+			        				.addExtra(RESOURCE_TYPE, mp.getResourceType()== null ? "": mp.getResourceType().toString()).build())
+			        		.build())
+					.build());
+			send(ma);
+		}
+	}
+	
 	public void sendSystemMessage(MessageBo... mb){
 		for(MessageBo bo : mb){
 			sendSystemMessage(bo);
@@ -142,10 +170,7 @@ public class JGMessageHelper implements InitializingBean, DisposableBean{
 		MessageAdapter ma = new MessageAdapter().pushPayload( 
 				PushPayload.newBuilder()
 		.setPlatform(Platform.android_ios())
-		.setAudience(Audience.newBuilder()
-				.addAudienceTarget(
-						AudienceTarget.registrationId("101d8559094270e0d78"))
-				.build())
+		.setAudience(Audience.all())
 		.setNotification(Notification.newBuilder()
         		.setAlert("消息标题")
         		.addPlatformNotification(AndroidNotification.newBuilder()

+ 3 - 2
src/main/java/com/goafanti/message/controller/AdminMessageController.java

@@ -379,9 +379,10 @@ public class AdminMessageController extends CertifyApiController{
 	@RequestMapping(value="/getMessageList", method = RequestMethod.GET)
 	public Result getMessageList(JtMessageProducer messageProducer,String sendStartTime, String sendEndTime, Integer pageNo,Integer pageSize ){
 		Result res = new Result();
-		if(StringUtils.isBlank(TokenManager.getUserId())){
+		/*if(StringUtils.isBlank(TokenManager.getUserId())){
 			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "登录数据失效"));
-		}
+			return res;
+		}*/
 		if (res.getError().isEmpty()) {
 			//查看消息列表
 			res.setData(jtMessageProducerService.getMessageList(messageProducer, sendStartTime, sendEndTime, pageNo, pageSize ));

+ 12 - 0
src/main/java/com/goafanti/message/service/impl/JtMessageProducerServiceImpl.java

@@ -20,6 +20,7 @@ import com.goafanti.common.model.JtMessageProducer;
 import com.goafanti.core.mybatis.BaseMybatisDao;
 import com.goafanti.core.mybatis.page.Pagination;
 import com.goafanti.core.shiro.token.TokenManager;
+import com.goafanti.message.JGMessageHelper;
 import com.goafanti.message.service.JtMessageProducerService;
 
 @Service
@@ -32,6 +33,8 @@ public class JtMessageProducerServiceImpl  extends BaseMybatisDao<JtMessageProdu
 	private AdminMapper adminMapper;
 	@Autowired
 	private JtMessageConsumerMapper jtMessageConsumerMapper;
+	@Autowired
+	private JGMessageHelper jgMessageHelper;
 	
 
 	@Override
@@ -69,6 +72,15 @@ public class JtMessageProducerServiceImpl  extends BaseMybatisDao<JtMessageProdu
 		messageProducer.setIsSend(true);
 		messageProducer.setIsDraft(false);
 		jtMessageProducerMapper.updateByPrimaryKeySelective(messageProducer);
+		if (messageProducer!=null) {
+			if (messageProducer.getSubject()==1) {
+				//推送消息
+				jgMessageHelper.sendPushMessage(messageProducer);
+				
+			}else if (messageProducer.getSubject()==2) {
+				//短信消息
+			}
+		}
 		return jtMessageConsumerMapper.addBatch(list);
 	}