集成阿里云推送

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
@@ -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();
}
}
}
+33
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>