集成阿里云推送

This commit is contained in:
yiyan 2019-04-11 00:38:52 +08:00
parent 6d9441f757
commit 7b2c9f46a0
11 changed files with 302 additions and 23 deletions

View File

@ -34,9 +34,12 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:5.0.8.RELEASE" level="project" />
<orderEntry type="library" name="Maven: com.aliyun:aliyun-java-sdk-push:3.10.1" level="project" />
<orderEntry type="library" name="Maven: com.aliyun:aliyun-java-sdk-core:4.4.0" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.5" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.5.8" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.11" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.11" level="project" />
<orderEntry type="library" name="Maven: javax.xml.bind:jaxb-api:2.1" level="project" />
<orderEntry type="library" name="Maven: javax.xml.stream:stax-api:1.0-2" level="project" />
@ -60,7 +63,6 @@
<orderEntry type="library" name="Maven: org.springframework:spring-jms:5.0.8.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-messaging:5.0.8.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.apache.xbean:xbean-spring:3.16" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:5.0.8.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis:3.4.5" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis-spring:1.3.2" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
@ -75,10 +77,7 @@
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.8.4" level="project" />
<orderEntry type="library" name="Maven: log4j:log4j:1.2.17" level="project" />
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.6" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.2" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.6" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.1" level="project" />
<orderEntry type="library" name="Maven: commons-httpclient:commons-httpclient:3.1" level="project" />
<orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.56" level="project" />
</component>
</module>

24
pom.xml
View File

@ -32,6 +32,19 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.8.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<!-- 阿里云推送 -->
<dependency>
<groupId>com.aliyun</groupId>
@ -201,22 +214,11 @@
<!-- apache 支持包 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<!-- json 解析器 -->
<dependency>

View File

@ -0,0 +1,92 @@
package com.ifish7.mq.push;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.push.model.v20160801.PushRequest;
import com.aliyuncs.push.model.v20160801.PushResponse;
import com.ifish7.mq.business.user.entity.TblPushList;
import com.ifish7.mq.business.user.service.ITblPushListService;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import java.time.LocalDateTime;
/**
* @author: yan.y
* @Description:
* @Date: Created in 21:42 2019/4/10
*/
@Log4j
public class AliyunPushApi {
private DefaultAcsClient client;
public AliyunPushApi(String regionId,String accessKeyId,String accessKeySecret){
client = new DefaultAcsClient(DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret));
}
//推送target类型
private static final String TARGET_TYPE = "DEVICE";
//推送类型
private static final String PUSH_TYPE = "MESSAGE";
@Autowired
private PushRequest pushRequest;
@Autowired
private ITblPushListService pushListService;
public void advancedPush(JSONObject json){
//推送目标
//DEVICE:根据设备推送
//ACCOUNT:根据账号推送
//ALIAS:根据别名推送
//TAG:根据标签推送
//ALL:推送给全部设备
pushRequest.setTarget(TARGET_TYPE);
pushRequest.setAcceptFormat(FormatType.JSON);
//根据Target来设定多个值使用逗号分隔
//此处应该查询出用户所对应的deviceId
pushRequest.setTargetValue(json.getString("deviceId"));
//推送类型
pushRequest.setPushType(PUSH_TYPE);
//推送设备类型
pushRequest.setDeviceType(json.getString("phoneType"));
// 消息的标题
pushRequest.setTitle(json.getString("title"));
// 消息的内容
pushRequest.setBody(json.getString("body"));
// 离线消息是否保存,若保存, 在推送时候用户即使不在线下一次上线则会收到
pushRequest.setStoreOffline(true);
try {
PushResponse response = client.getAcsResponse(pushRequest);
TblPushList pushList = new TblPushList();
pushList.setUserId(json.getInteger("userId"));
pushList.setDeviceId(json.getInteger("deviceId"));
pushList.setShowName(json.getString("showName"));
pushList.setPhoneType(json.getString("phoneType"));
pushList.setPushType(json.getString("pushType"));
pushList.setPushTitle(json.getString("title"));
pushList.setPushContext(json.getString("body"));
pushList.setJpushStatus(response.getMessageId());
pushList.setNeteaseStatus("1");
pushList.setCreateTime(LocalDateTime.now());
pushList.setNumber1(json.getInteger("number1"));
pushList.setNumber2(json.getInteger("number2"));
pushList.setNumber3(json.getInteger("number3"));
pushList.setNumber4(json.getInteger("number4"));
pushList.setNumber5(json.getInteger("number5"));
pushListService.save(pushList);
} catch (ClientException e) {
log.error(e.getMessage(),e);
}
}
}

View File

@ -1,5 +1,9 @@
package com.ifish7.mq.queues.event;
import com.alibaba.fastjson.JSONObject;
import com.ifish7.mq.push.AliyunPushApi;
import com.ifish7.mq.utils.AppBeans;
/**
* @author: yan.y
* @Description: 消息推送事件
@ -8,4 +12,12 @@ package com.ifish7.mq.queues.event;
*/
public class PushNotifcationEvent {
/**
* 设备通知增强版
* @param eventBody 事件体
*/
private void deviceNotifcationPlus(JSONObject eventBody){
AliyunPushApi aliyunPushApi = AppBeans.getBean(AliyunPushApi.class);
aliyunPushApi.advancedPush(eventBody);
}
}

View File

@ -2,19 +2,17 @@ package com.ifish7.mq.queues.event.support;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.log4j.Log4j;
import java.lang.reflect.Method;
/**
* @author: yan.y
* @Description: 事件总线处理
* @Description: 数据事件总线处理
* @Date: Created in 22:15 2019/4/9
*/
@Log4j
@Data
public class EventSupport {
public class DataEventSupport {
//事件处理类型
protected String eventProcess;

View File

@ -0,0 +1,36 @@
package com.ifish7.mq.queues.event.support;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j;
import java.lang.reflect.Method;
/**
* @author: yan.y
* @Description: 推送事件总线处理
* @Date: Created in 23:24 2019/4/10
*/
@Log4j
public class PushEventSupport {
//事件处理类型
protected String eventProcess;
//事件名称
protected String eventName;
//事件体
protected JSONObject eventBody;
//事件处理
public void process() {
try {
Class<?> eventClazz = Class.forName(this.eventName);
Method method = eventClazz.getDeclaredMethod(this.eventProcess,Object.class);
method.setAccessible(true);
method.invoke(eventClazz.newInstance(),this.eventBody);
} catch (Exception e) {
log.info(e.getMessage(),e);
}
}
}

View File

@ -1,8 +1,7 @@
package com.ifish7.mq.queues.listener;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.ifish7.mq.queues.event.support.EventSupport;
import com.ifish7.mq.queues.event.support.DataEventSupport;
import lombok.extern.log4j.Log4j;
import javax.jms.JMSException;
@ -24,8 +23,8 @@ public class IfishDataQueueMessageListener implements MessageListener{
TextMessage tm = (TextMessage) message;
try {
log.info("Ifish7 - Data : " + tm.getText());
EventSupport eventSupport = JSON.parseObject(tm.getText(), EventSupport.class);
eventSupport.process();
DataEventSupport dataEventSupport = JSON.parseObject(tm.getText(), DataEventSupport.class);
dataEventSupport.process();
} catch (JMSException e) {
log.error(e);
}

View File

@ -0,0 +1,26 @@
accessKeyId= LTAIfZaosFH5IWlD
accessKeySecret = dvlE8eFii31BcGb8HzkGz3eSaJ6Y94
appKey = 23252055
#\u76EE\u524D\u8BE5\u503C\u56FA\u5B9A\uFF0C\u4E0D\u7528\u52A8
regionId = cn-hangzhou
protocol = HTTPS
method = POST
storeOffline = true
version = 2016-08-01
# \u53D1\u9001\u7684deviceId, \u5217\u8868\u7528\u9017\u53F7\u5206\u9694 \u6BD4\u5982: 111 \u6216\u662F 111,222
#deviceIds =
#deviceId =
# \u53D1\u9001\u7684account, \u5217\u8868\u7528\u9017\u53F7\u5206\u9694 \u6BD4\u5982: account1 \u6216\u662F account1,account2
#accounts =
#account =
# \u53D1\u9001\u7684alias, \u5217\u8868\u7528\u9017\u53F7\u5206\u9694 \u6BD4\u5982: alias1,alias2
#aliases =
#alias =
# \u53D1\u9001\u7684tag, tag\u8868\u8FBE\u5F0F\u53C2\u8003\u6587\u6863:https://help.aliyun.com/document_detail/48055.html?
#tag =
#tagExpression =

View File

@ -14,6 +14,7 @@
<value>classpath:mq.properties</value>
<value>classpath:jdbc.properties</value>
<value>classpath:log4j.properties</value>
<value>classpath:push.properties</value>
</list>
</property>
</bean>
@ -23,4 +24,20 @@
<!-- 自动扫描 -->
<context:component-scan base-package="com.ifish7.mq.*" />
<bean id="app" class="com.ifish7.mq.utils.AppBeans" />
<bean id="push" class="com.aliyuncs.push.model.v20160801.PushRequest" scope="prototype">
<property name="appKey" value="${appKey}" />
<property name="sysProtocol" value="${protocol}" />
<property name="sysMethod" value="${method}" />
<property name="storeOffline" value="${storeOffline}" />
<property name="sysRegionId" value="${regionId}"/>
<property name="sysVersion" value="${version}" />
</bean>
<bean id="api" class="com.ifish7.mq.push.AliyunPushApi" scope="prototype" >
<constructor-arg name="regionId" value="${regionId}" />
<constructor-arg name="accessKeyId" value="${accessKeyId}" />
<constructor-arg name="accessKeySecret" value="${accessKeySecret}" />
</bean>
</beans>

View File

@ -0,0 +1,65 @@
package com.ifish7.mq.push;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.push.model.v20160801.PushRequest;
import com.aliyuncs.push.model.v20160801.PushResponse;
import lombok.extern.log4j.Log4j;
import org.junit.Test;
/**
* @author: yan.y
* @Description:
* @Date: Created in 23:42 2019/4/10
*/
//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations="classpath:spring-main.xml")
@Log4j
public class AliyunPushApiTest {
@Test
public void testAdvancedPush(){
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIfZaosFH5IWlD", "dvlE8eFii31BcGb8HzkGz3eSaJ6Y94");
DefaultAcsClient client = new DefaultAcsClient(profile);
PushRequest pushRequest = new PushRequest();
pushRequest.setAppKey(23252055l);
pushRequest.setSysProtocol(ProtocolType.HTTPS);
pushRequest.setSysMethod(MethodType.POST);
pushRequest.setStoreOffline(true);
pushRequest.setSysRegionId("cn-hangzhou");
pushRequest.setSysVersion("2016-08-01");
//推送目标
//DEVICE:根据设备推送
//ACCOUNT:根据账号推送
//ALIAS:根据别名推送
//TAG:根据标签推送
//ALL:推送给全部设备
pushRequest.setTarget("DEVICE");
pushRequest.setAcceptFormat(FormatType.JSON);
//根据Target来设定多个值使用逗号分隔
//此处应该查询出用户所对应的deviceId
pushRequest.setTargetValue("18aujhs872ysgd6tx7329oaliygx7432");
//推送类型
pushRequest.setPushType("MESSAGE");
//推送设备类型
pushRequest.setDeviceType("ANDROID");
// 消息的标题
pushRequest.setTitle("温度报警");
// 消息的内容
pushRequest.setBody("[温度报警]你的水族箱\"鱼缸1234\"在2019-04-10 23:40:00,温度达到29℃,已高于28℃,请及时查看!");
try {
PushResponse response = client.getAcsResponse(pushRequest);
log.info(response);
} catch (ClientException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" >
<list>
<value>classpath:push.properties</value>
</list>
</property>
</bean>
<bean id="app" class="com.ifish7.mq.utils.AppBeans" />
<bean id="push" class="com.aliyuncs.push.model.v20160801.PushRequest" scope="prototype">
<property name="appKey" value="${appKey}" />
<property name="sysProtocol" value="${protocol}" />
<property name="sysMethod" value="${method}" />
<property name="storeOffline" value="${storeOffline}" />
<property name="sysRegionId" value="${regionId}"/>
<property name="sysVersion" value="${version}" />
</bean>
<bean id="api" class="com.ifish7.mq.push.AliyunPushApi" scope="prototype" >
<constructor-arg name="regionId" value="${regionId}" />
<constructor-arg name="accessKeyId" value="${accessKeyId}" />
<constructor-arg name="accessKeySecret" value="${accessKeySecret}" />
</bean>
</beans>