Commit 03c5bfd0 by fz

1.阿里云推送通知

1 parent 3501a16d
Showing 36 changed files with 801 additions and 75 deletions
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<packaging>war</packaging> <packaging>war</packaging>
<artifactId>youMeng-platform-service</artifactId> <artifactId>push-platform-service</artifactId>
<dependencies> <dependencies>
<dependency> <dependency>
...@@ -45,6 +45,18 @@ ...@@ -45,6 +45,18 @@
<artifactId>rest-admin-swagger2</artifactId> <artifactId>rest-admin-swagger2</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</dependency> </dependency>
<!--阿里移动推送-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-push</artifactId>
<version>3.13.6</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>[4.3.2,5.0.0)</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.glsx.modules.notify.config;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AliBeanConfig {
@Value("${aliyun.accessKeyId}")
private String accessKeyId;
@Value("${aliyun.accessSecret}")
private String accessSecret;
@Bean
public IAcsClient client(){
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
IAcsClient client = new DefaultAcsClient(profile);
return client;
}
}
package com.glsx.modules.notify.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class PushNotifySwagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.glsx.modules.notify.controller"))
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title("终端推送平台api")
.description("向(安卓,iOS)终端推送消息/通知")
.version("1.0.0")
.build());
}
}
package com.glsx.modules.notify.controller; package com.glsx.modules.notify.controller;
import com.glsx.modules.notify.service.DeviceService; import com.glsx.modules.notify.model.BaseRequest;
import com.glsx.modules.notify.model.param.NotifyReqParam;
import com.glsx.modules.notify.service.PushService;
import com.glsx.plat.core.web.R; import com.glsx.plat.core.web.R;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;
/** /**
* @author fengzhi * @author fengzhi
* @date 2021/4/8 14:14 * @date 2021/4/8 14:14
*/ */
@Controller @Controller
@RequestMapping("/youmeng/notify") @RequestMapping("/push")
@Api(value = "设备管理", tags = {"设备管理"}) @Api(value = "终端推送API", tags = {"通知或者消息推送"})
public class DeviceController { public class PushController {
@Autowired @Autowired
private DeviceService deviceService; private PushService pushService;
/** /**
* 获取用户下的设备列表 * 推送通知给终端
* * pushNotify
* @return R * @return R
*/ */
@RequestMapping(value = "/{uid}/devices", method = RequestMethod.GET) @ApiOperation(value="授权管理")
@ResponseBody @PostMapping("notify")
@ApiOperation(value = "获取用户下的设备列表") public R pushNotify(@RequestBody NotifyReqParam request){
public R getAllDevicesByUser(@PathVariable String uid) { BaseRequest baseRequest = new BaseRequest();
return deviceService.getAllDevicesByUser("1"); BeanUtils.copyProperties(request,baseRequest);
return pushService.pushNotify(baseRequest);
} }
} }
package com.glsx.modules.notify.factory;
/**
* @author fengzhi
* @date 2021/3/9 17:05
* @description 触发类型信息接口
*/
public interface Constants {
Integer getCode();
String getValue();
String getBeanName();
}
package com.glsx.modules.notify.factory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author fengzhi
* @date 2021/4/9 17:05
* @description 触发类型接口工厂类
*/
@Service
public class PushFactory {
@Autowired
Map<String, PushStrategy> strategys = new ConcurrentHashMap<>(3);
public PushStrategy getStrategy(Integer code) throws Exception{
PushStrategy strategy = strategys.get(PushType.getBeanNameByCode(code));
if(strategy == null) {
throw new RuntimeException("PushFactory=====no strategy defined");
}
return strategy;
}
}
package com.glsx.modules.notify.factory;
import com.glsx.modules.notify.model.BaseRequest;
import com.glsx.plat.core.web.R;
/**
* @author fengzhi
* @date 2021/3/9 17:05
* @description 平台统一对外接口
*/
public interface PushStrategy {
R pushNotify(BaseRequest request);
}
package com.glsx.modules.notify.factory;
/**
* @author fengzhi
* @date 2021/4/9 09:49
* @description 触发类型枚举
*/
public enum PushType implements Constants {
ALIYUN {
@Override
public Integer getCode() {
return 1;
}
@Override
public String getValue() {
return "阿里云移动推送";
}
@Override
public String getBeanName(){
return "aliYunStrategy";
}
},
YOUMENG {
@Override
public Integer getCode() {
return 2;
}
@Override
public String getValue() {
return "友盟推送";
}
@Override
public String getBeanName(){
return "youMengStrategy";
}
},;
public static String getValueByCode(Integer code) {
for (PushType platForm : PushType.values()) {
if (platForm.getCode().equals(code)) {
return platForm.getValue();
}
}
return null;
}
public static String getBeanNameByCode(Integer code) {
for (PushType platForm : PushType.values()) {
if (platForm.getCode().equals(code)) {
return platForm.getBeanName();
}
}
return null;
}
}
package com.glsx.modules.notify.factory.strategy;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.push.model.v20160801.PushRequest;
import com.aliyuncs.push.model.v20160801.PushResponse;
import com.glsx.modules.notify.factory.PushStrategy;
import com.glsx.modules.notify.model.BaseRequest;
import com.glsx.plat.core.web.R;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
//aliyun移动推送策略
@Slf4j
@Component
public class AliYunStrategy extends BaseStrategy implements PushStrategy {
@Value("${aliyun.android.appKey}")
private String appKey_android;
@Value("${aliyun.ios.appKey}")
private String appKey_ios;
@Value("${aliyun.android.NotificationChannel}")
private String androidNotificationChannel;
@Value("${aliyun.ios.ApnsEnv}")
private String apnsEnv;
@Autowired
private IAcsClient client;
@Override
public R pushNotify(BaseRequest request) {
String deviceType = request.getDeviceType();
if (StringUtils.isEmpty(deviceType)) {
return R.error("终端类型DeviceType不能为空");
}
//设置推送模式为通知
request.setPushType("NOTICE");
//设置按照别名推送
request.setTarget("ALIAS");
PushResponse pushResponse = null;
//iOS:iOS设备
//ANDROID:Android设备
if (deviceType.equals("ANDROID")) {
pushResponse = pushAndroidNotify(request);
}
if (deviceType.equals("iOS")) {
pushResponse = pushIOSNotify(request);
}
if (null == pushResponse)
return R.error("push请求失败");
Map<String, Object> map = new HashMap();
map.put("RequestId", pushResponse.getRequestId());
map.put("MessageId", pushResponse.getMessageId());
return R.ok().data(map);
}
public PushResponse pushAndroidNotify(BaseRequest request) {
PushRequest pushRequest = new PushRequest();
pushRequest.setAppKey(Long.valueOf(appKey_android));
buildBaseRequest(pushRequest, request);
//安卓默认设置
//离线保存,ExpireTime默认72小时
pushRequest.setStoreOffline(true);
//设置NotificationChannel参数,兼容Android 8.0以上设备通知接收不到。
pushRequest.setAndroidNotificationChannel(androidNotificationChannel);
return sendRequest(pushRequest);
}
public PushResponse pushIOSNotify(BaseRequest request) {
PushRequest pushRequest = new PushRequest();
pushRequest.setAppKey(Long.valueOf(appKey_ios));
buildBaseRequest(pushRequest, request);
//IOS默认设置
pushRequest.setIOSApnsEnv(apnsEnv);
return sendRequest(pushRequest);
}
public PushResponse sendRequest(PushRequest request) {
PushResponse response = null;
try {
response = client.getAcsResponse(request);
log.info(new Gson().toJson(response));
} catch (ServerException e) {
e.printStackTrace();
log.error("aliPush===ServerException:" + e.getMessage());
} catch (ClientException e) {
log.error("aliPush===ErrCode:" + e.getErrCode() + "===ErrMsg:" + e.getErrMsg() + "===RequestId:" + e.getRequestId());
}
return response;
}
protected void buildBaseRequest(PushRequest request, BaseRequest baseRequest) {
request.setRegionId("cn-hangzhou");
request.setPushType(baseRequest.getPushType());
request.setDeviceType(baseRequest.getDeviceType());
request.setTarget(baseRequest.getTarget());
request.setTargetValue(baseRequest.getTargetValue());
request.setBody(baseRequest.getBody());
request.setTitle(baseRequest.getTitle());
}
}
package com.glsx.modules.notify.factory.strategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class BaseStrategy {
}
package com.glsx.modules.notify.model;
import lombok.Data;
@Data
public class AndroidNotifyRequest extends HighLevelRequest {
/**
* 设置vivo通知类型的classification,需要在vivo平台申请,详见:申请链接
*/
private String AndroidNotificationVivoChannel;
/**
* 设置Huawei通知类型的classification,需要在Huawei平台申请,详见:申请链接
*/
private String AndroidNotificationHuaweiChannel;
/**
* 标识每条消息在通知显示时的唯一标识,
* 不同的通知栏消息可以相同的NotifyId,
* 实现新的通知栏消息覆盖老的,当前支持小米和华为通道
*/
private Integer AndroidNotificationNotifyId;
/**
* 通知的提醒方式。可取值:
*
* VIBRATE:振动(默认值)
* SOUND:声音
* BOTH:声音和振动
* NONE:静音
*/
private String AndroidNotifyType;
/**
* 点击通知后动作。可取值:
*
* APPLICATION:打开应用(默认值)
* ACTIVITY:打开应用AndroidActivity
* URL:打开URL
* NONE:无跳转
*/
private String AndroidOpenType;
/**
* 设定通知打开的activity。
*
* 仅当AndroidOpenType=”Activity”时传入,如:com.alibaba.cloudpushdemo.bizactivity
*/
private String AndroidActivity;
/**
* Android通知声音(保留参数,当前暂不起作用)。
*/
private String AndroidMusic;
/**
* Android收到推送后打开对应的url。
*
* 仅当AndroidOpenType=”URL”时传入
*/
private String AndroidOpenUrl;
/**
* 指定点击通知后跳转的Activity。
*/
private String AndroidPopupActivity;
/**
* 辅助弹窗模式下标题内容。AndroidPopupActivity参数不为空时,该参数必填。
*
* 长度限制:30字符。
*
* 如使用厂商通道,则需同时符合厂商通道的限制,具体内容参见:Android端辅助通道推送限制
*/
private String AndroidPopupTitle;
/**
* 辅助弹窗模式下Body内容。AndroidPopupActivity参数不为空时,该参数必填。
*
* 长度限制:128字符。
*
* 如使用厂商通道,则需同时符合厂商通道的限制,具体内容参见:Android端辅助通道推送限制
*/
private String AndroidPopupBody;
/**
* Android自定义通知栏样式,取值:1-100
*/
private Integer AndroidNotificationBarType;
/**
* Android通知在通知栏展示时排列位置的优先级。可取值: -2,-1,0,1,2
*/
private Integer AndroidNotificationBarPriority;
/**
* 设定通知的扩展属性。当推送类型PushType设置为MESSAGE消息类型时,该属性不生效。
*
* 该参数要以Json map的格式传入,否则会解析出错
*/
private String AndroidExtParameters;
/**
* 推送类型为消息时设备不在线,则这条推送会使用辅助弹窗功能。默认值为false,仅当PushType=MESSAGE时生效。
*
* 如果消息转通知推送成功,收到通知是展示的数据为服务端设置的AndroidPopupTitle和AndroidPopupBody参数值,
* 点击通知在辅助弹窗的onSysNoticeOpened方法中获取到的数据是服务端设置的Title和Body参数值
*/
private Boolean AndroidRemind;
/**
* 设置NotificationChannel参数,具体用途请参见常见问题:Android 8.0以上设备通知接收不到
*/
private String AndroidNotificationChannel;
/**
* 设置小米通知类型的channelId,需要在小米平台申请,详见:申请链接
*/
private String AndroidNotificationXiaomiChannel;
}
package com.glsx.modules.notify.model;
import lombok.Data;
@Data
public class BaseRequest {
/**
* 1.阿里云(其他暂未实现)
*/
private Integer type;
/**
* 平台的AppKey(分安卓ios)
*/
private Long appKey;
/**
* 推送类型
* MESSAGE:表示消息。
* NOTICE:表示通知。
*/
private String pushType;
/**
* iOS:iOS设备
* ANDROID:Android设备
* ALL:全部类型设备
* 说明 该参数仅对旧的不分端App有意义。新的分端App下,该参数填写“ALL”或与App分端类型对应的值均可
*/
private String deviceType;
/**
* Android推送时通知/消息的标题以及iOS消息的标题(必填)。iOS 10+通知显示标题,(iOS 8.2 <= iOS系统 < iOS 10)替换通知应用名称(选填)。长度限制:200字节。
*/
private String title;
/**
* Android推送时通知的内容/消息的内容;iOS消息/通知内容,推送的内容大小是有限制的,参见产品限制。
*/
private String body;
/**
* 推送目标
* DEVICE:根据设备推送。
* ACCOUNT:根据账号推送。
* ALIAS:根据别名推送。
* TAG:根据标签推送。
* ALL:推送给全部设备(同一种DeviceType的两次全推的间隔至少为1秒)。
* TBD:初始化持续推送,推送目标由后续的ContinuouslyPush接口指定。
*/
private String target;
/**
* 根据Target来设定,多个值使用逗号分隔,超过限制需要分多次推送。
* Target=DEVICE,值如deviceid1,deviceid2(最多支持1000个)。
* Target=ACCOUNT,值如account1,account2(最多支持1000个)。
* Target=ALIAS,值如alias1,alias2(最多支持1000个)。
* Target=TAG,支持单Tag和多Tag,格式请参见标签格式。
* Target=ALL,值为ALL。
* Target=TBD,值为TBD。
*/
private String targetValue;
}
package com.glsx.modules.notify.model;
import lombok.Data;
@Data
public class HighLevelRequest extends BaseRequest{
/**
* 推送任务自定义标识,当JobKey不为空时,回执日志中会附带该字段。查看回执日志参见回执日志。
*/
private String JobKey;
/**
* 离线消息/通知是否保存。StoreOffline默认设置为false。
*
* 若保存,在推送时候用户不在线,在过期时间(ExpireTime)内用户上线时会被再次发送。
* ExpireTime默认为72小时。iOS通知走Apns链路,不受StoreOffline影响。
*/
private Boolean StoreOffline;
/**
* 用于定时发送。不设置缺省是立即发送。
*
* 设置定时发送最迟不能晚于7天发送。
*
* 时间格式按照ISO8601标准表示,并需要使用UTC时间,格式为YYYY-MM-DDThh:mm:ssZ。
* 注:Target为TBD(持续推送)时,不支持定时设置。
*/
private String PushTime;
/**
* 离线消息/通知的过期时间,和StoreOffline配合使用,过期则不会再被发送,最长保存72小时。默认为72小时。
*
* 时间格式按照ISO8601标准表示,并需要使用UTC时间,格式为YYYY-MM-DDThh:mm:ssZ,
* 过期时间不能小于当前时间或者定时发送时间加上3秒(ExpireTime > PushTime+ 3秒),
* 3秒是为了冗余网络和系统延迟造成的误差。
*/
private String ExpireTime;
/**
* 补发短信的模板名,可以在短信模板管理界面获取,是系统分配的名称,而非开发者设置的名称
*/
private String SmsTemplateName;
/**
* 补发短信的签名
*/
private String SmsSignName;
/**
* 短信模板的变量名值对,格式: key1=value1&key2=value2。
*/
private String SmsParams;
/**
* 触发短信的延迟时间,秒。
*
* 推荐设置为 15 秒以上,避免短信和推送的重复
*/
private Integer SmsDelaySecs;
/**
* 触发短信的条件。可取值:
*
* 0:推送未收到时触发。
* 1:用户未打开时触发。
*/
private Integer SmsSendPolicy;
}
package com.glsx.modules.notify.model;
import lombok.Data;
@Data
public class IOSNotifyRequest extends HighLevelRequest {
/**
* iOS的通知是通过APNs中心来发送的,需要填写对应的环境信息。
* DEV:表示开发环境。
* PRODUCT:表示生产环境。
*/
private String iOSApnsEnv;
/**
* 消息推送时设备不在线(既与移动推送的服务端的长连接通道不通),
* 则这条推送会做为通知,通过苹果的APNs通道送达一次。
* 说明 离线消息转通知仅适用于生产环境
*/
private Boolean iOSRemind;
/**
* iOS消息转通知时使用的iOS通知内容,仅当iOSApnsEnv=PRODUCT && iOSRemind为true时有效
*/
private String iOSRemindBody;
/**
* iOS应用图标右上角角标。
* <p>
* 说明 若iOSBadgeAutoIncrement设置为True,则此项必须为空。
*/
private Integer iOSBadge;
/**
* 是否开启角标自增功能,默认为false。
* <p>
* 当该项为true时,iOSBadge必须为为空。角标自增功能由推送服务端维护每个设备的角标计数,
* 需要用户使用1.9.5以上版本的sdk,并且需要用户主动同步角标数字到服务端
*/
private Boolean iOSBadgeAutoIncrement;
/**
* 是否开启iOS静默通知
*/
private Boolean iOSSilentNotification;
/**
* iOS通知声音。指定存放在app bundle或沙盒Library/Sounds目录下的音频文件名,
* 请参见:iOS推送如何设定通知声音。
* <p>
* 若指定为空串(””),通知为静音;若不设置,默认填充default为系统提示音
*/
private String iOSMusic;
/**
* iOS通知副标题内容(iOS 10+)
*/
private String iOSSubtitle;
/**
* 指定iOS通知Category(iOS 10+)
*/
private Boolean iOSNotificationCategory;
/**
* 是否使能iOS通知扩展处理(iOS 10+
*/
private Boolean iOSMutableContent;
/**
* iOS通知的扩展属性。
* <p>
* iOS10+可以在此指定富媒体推送通知的资源Url:
* {“attachment”: “https://xxxx.xxx/notification_pic.png"} 。
* 该参数要以Json map的格式传入,否则会解析出错
*/
private String iOSExtParameters;
/**
* 设备收到有相同CollapseId的消息,会合并成一条。
* 设备不在线,连续发相同CollapseId的消息,
* 通知栏只会显示一条,iOS 10+支持设置此参数
*/
private String iOSNotificationCollapseId;
}
package com.glsx.modules.notify.model.param;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel("推送参数")
public class NotifyReqParam {
@ApiModelProperty(value = "1:阿里云(其他暂未实现)",required = true,example = "1")
private Integer type;
@ApiModelProperty(value = "iOS:iOS设备,ANDROID:Android设备 ",required = true,example = "ANDROID")
private String DeviceType;
@ApiModelProperty(value = "Android/iOS推送时的消息/通知标题",required = true)
private String Title;
@ApiModelProperty(value = "Android/iOS推送时的消息/通知内容,推送的内容大小是有限制的",required = true)
private String Body;
@ApiModelProperty(value = "这里传别名,即用户ID",required = true)
private String TargetValue;
}
package com.glsx.modules.notify.demo; package com.glsx.modules.notify.push;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
......
package com.glsx.modules.notify.demo; package com.glsx.modules.notify.push;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.glsx.modules.notify.demo.android.AndroidBroadcast; import com.glsx.modules.notify.push.android.AndroidBroadcast;
import com.glsx.modules.notify.demo.android.AndroidCustomizedcast; import com.glsx.modules.notify.push.android.AndroidCustomizedcast;
import com.glsx.modules.notify.demo.android.AndroidGroupcast; import com.glsx.modules.notify.push.android.AndroidGroupcast;
import com.glsx.modules.notify.demo.android.AndroidUnicast; import com.glsx.modules.notify.push.android.AndroidUnicast;
import com.glsx.modules.notify.demo.ios.IOSBroadcast; import com.glsx.modules.notify.push.ios.IOSBroadcast;
import com.glsx.modules.notify.demo.ios.IOSCustomizedcast; import com.glsx.modules.notify.push.ios.IOSCustomizedcast;
import com.glsx.modules.notify.demo.ios.IOSGroupcast; import com.glsx.modules.notify.push.ios.IOSGroupcast;
import com.glsx.modules.notify.demo.ios.IOSUnicast; import com.glsx.modules.notify.push.ios.IOSUnicast;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
......
package com.glsx.modules.notify.demo; package com.glsx.modules.notify.push;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
......
package com.glsx.modules.notify.demo; package com.glsx.modules.notify.push;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
......
package com.glsx.modules.notify.demo; package com.glsx.modules.notify.push;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
......
package com.glsx.modules.notify.demo.android; package com.glsx.modules.notify.push.android;
import com.glsx.modules.notify.demo.AndroidNotification; import com.glsx.modules.notify.push.AndroidNotification;
public class AndroidBroadcast extends AndroidNotification { public class AndroidBroadcast extends AndroidNotification {
public AndroidBroadcast(String appkey,String appMasterSecret) throws Exception { public AndroidBroadcast(String appkey,String appMasterSecret) throws Exception {
......
package com.glsx.modules.notify.demo.android; package com.glsx.modules.notify.push.android;
import com.glsx.modules.notify.demo.AndroidNotification; import com.glsx.modules.notify.push.AndroidNotification;
public class AndroidCustomizedcast extends AndroidNotification { public class AndroidCustomizedcast extends AndroidNotification {
public AndroidCustomizedcast(String appkey,String appMasterSecret) throws Exception { public AndroidCustomizedcast(String appkey,String appMasterSecret) throws Exception {
......
package com.glsx.modules.notify.demo.android; package com.glsx.modules.notify.push.android;
import com.glsx.modules.notify.demo.AndroidNotification; import com.glsx.modules.notify.push.AndroidNotification;
public class AndroidFilecast extends AndroidNotification { public class AndroidFilecast extends AndroidNotification {
public AndroidFilecast(String appkey,String appMasterSecret) throws Exception { public AndroidFilecast(String appkey,String appMasterSecret) throws Exception {
......
package com.glsx.modules.notify.demo.android; package com.glsx.modules.notify.push.android;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.glsx.modules.notify.demo.AndroidNotification; import com.glsx.modules.notify.push.AndroidNotification;
public class AndroidGroupcast extends AndroidNotification { public class AndroidGroupcast extends AndroidNotification {
public AndroidGroupcast(String appkey,String appMasterSecret) throws Exception { public AndroidGroupcast(String appkey,String appMasterSecret) throws Exception {
......
package com.glsx.modules.notify.demo.android; package com.glsx.modules.notify.push.android;
import com.glsx.modules.notify.demo.AndroidNotification; import com.glsx.modules.notify.push.AndroidNotification;
public class AndroidUnicast extends AndroidNotification { public class AndroidUnicast extends AndroidNotification {
public AndroidUnicast(String appkey,String appMasterSecret) throws Exception { public AndroidUnicast(String appkey,String appMasterSecret) throws Exception {
......
package com.glsx.modules.notify.demo.ios; package com.glsx.modules.notify.push.ios;
import com.glsx.modules.notify.demo.IOSNotification; import com.glsx.modules.notify.push.IOSNotification;
public class IOSBroadcast extends IOSNotification { public class IOSBroadcast extends IOSNotification {
public IOSBroadcast(String appkey,String appMasterSecret) throws Exception { public IOSBroadcast(String appkey,String appMasterSecret) throws Exception {
......
package com.glsx.modules.notify.demo.ios; package com.glsx.modules.notify.push.ios;
import com.glsx.modules.notify.demo.IOSNotification; import com.glsx.modules.notify.push.IOSNotification;
public class IOSCustomizedcast extends IOSNotification { public class IOSCustomizedcast extends IOSNotification {
public IOSCustomizedcast(String appkey,String appMasterSecret) throws Exception { public IOSCustomizedcast(String appkey,String appMasterSecret) throws Exception {
......
package com.glsx.modules.notify.demo.ios; package com.glsx.modules.notify.push.ios;
import com.glsx.modules.notify.demo.IOSNotification; import com.glsx.modules.notify.push.IOSNotification;
public class IOSFilecast extends IOSNotification { public class IOSFilecast extends IOSNotification {
public IOSFilecast(String appkey,String appMasterSecret) throws Exception { public IOSFilecast(String appkey,String appMasterSecret) throws Exception {
......
package com.glsx.modules.notify.demo.ios; package com.glsx.modules.notify.push.ios;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.glsx.modules.notify.demo.IOSNotification; import com.glsx.modules.notify.push.IOSNotification;
public class IOSGroupcast extends IOSNotification { public class IOSGroupcast extends IOSNotification {
public IOSGroupcast(String appkey,String appMasterSecret) throws Exception { public IOSGroupcast(String appkey,String appMasterSecret) throws Exception {
......
package com.glsx.modules.notify.demo.ios; package com.glsx.modules.notify.push.ios;
import com.glsx.modules.notify.demo.IOSNotification; import com.glsx.modules.notify.push.IOSNotification;
public class IOSUnicast extends IOSNotification { public class IOSUnicast extends IOSNotification {
public IOSUnicast(String appkey,String appMasterSecret) throws Exception{ public IOSUnicast(String appkey,String appMasterSecret) throws Exception{
......
package com.glsx.modules.notify.service;
import com.glsx.plat.core.web.R;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @author fengzhi
* @date 2021/4/8 14:14
*/
@Service
public class DeviceService {
public R getAllDevicesByUser(@PathVariable String uid) {
return R.ok("hello fz");
}
}
package com.glsx.modules.notify.service;
import com.glsx.modules.notify.factory.PushFactory;
import com.glsx.modules.notify.factory.PushStrategy;
import com.glsx.modules.notify.model.BaseRequest;
import com.glsx.plat.core.web.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class PushService {
@Autowired
private PushFactory pushFactory;
public R pushNotify(BaseRequest pushRequest){
//获取对应策略,1:任意触发条件满足 2:全部触发条件满足
PushStrategy pushStrategy = null;
try {
pushStrategy = pushFactory.getStrategy(pushRequest.getType());
} catch (Exception e) {
R.error("创建策略失败:" + e.getMessage());
}
return pushStrategy.pushNotify(pushRequest);
}
}
#项目名称 #项目名称
spring.application.name=youMeng-platform-service spring.application.name=push-platform-service
#日志路径(logback-spring.xml读取,不要加入配置中心) #日志路径(logback-spring.xml读取,不要加入配置中心)
#logging.path=/data/java_log/jetty/${spring.application.name}/${spring.application.name}.log logging.path=/data/java_log/jetty/${spring.application.name}/${spring.application.name}.log
#nacos配置中心 #nacos配置中心
spring.cloud.nacos.config.server-addr=192.168.0.63:8848 spring.cloud.nacos.config.server-addr=192.168.0.63:8848
......
...@@ -18,7 +18,7 @@ logback-spring.xml引入文件: ...@@ -18,7 +18,7 @@ logback-spring.xml引入文件:
<property resource="bootstrap.properties"/> <property resource="bootstrap.properties"/>
<property name="APP_NAME" value="${spring.application.name}"/> <property name="APP_NAME" value="${spring.application.name}"/>
<!-- 生成日志文件目录 --> <!-- 生成日志文件目录 -->
<!-- <property name="LOG_FILE" value="${logging.path}"/>--> <property name="LOG_FILE" value="${logging.path}"/>
<contextName>${APP_NAME}</contextName> <contextName>${APP_NAME}</contextName>
<turboFilter class="com.glsx.plat.common.loggin.BootTurboFilter"> <turboFilter class="com.glsx.plat.common.loggin.BootTurboFilter">
......
package com.glsx.modules.notify.controller; package com.glsx.modules.notify.controller;
import com.glsx.modules.notify.demo.BroadCastService; import com.glsx.modules.notify.factory.PushType;
import com.glsx.modules.notify.service.DeviceService; import com.glsx.modules.notify.model.AndroidNotifyRequest;
import com.glsx.modules.notify.model.BaseRequest;
import com.glsx.modules.notify.model.param.NotifyReqParam;
import com.glsx.modules.notify.push.BroadCastService;
import com.glsx.modules.notify.service.PushService;
import com.glsx.plat.core.web.R;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
...@@ -14,15 +20,15 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; ...@@ -14,15 +20,15 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@Slf4j @Slf4j
public class DeviceControllerTest { public class DeviceControllerTest {
@Autowired @Autowired
private DeviceService deviceService; private BroadCastService demo;
@Autowired @Autowired
private BroadCastService demo; PushService pushService;
@Test @Test
public void getAllDevicesByUser() { public void testPush_android_notify() {
try { try {
demo.sendAndroidUnicast(); //demo.sendAndroidUnicast();
//demo.sendIOSUnicast(); //demo.sendIOSUnicast();
/* TODO these methods are all available, just fill in some fields and do the test /* TODO these methods are all available, just fill in some fields and do the test
* demo.sendAndroidCustomizedcastFile(); * demo.sendAndroidCustomizedcastFile();
...@@ -37,6 +43,34 @@ public class DeviceControllerTest { ...@@ -37,6 +43,34 @@ public class DeviceControllerTest {
* demo.sendIOSCustomizedcast(); * demo.sendIOSCustomizedcast();
* demo.sendIOSFilecast(); * demo.sendIOSFilecast();
*/ */
NotifyReqParam pushRequest = new NotifyReqParam();
pushRequest.setType(PushType.ALIYUN.getCode());
pushRequest.setTitle("消息推送测试标题");
pushRequest.setBody("内容test");
pushRequest.setDeviceType("ANDROID");
pushRequest.setTargetValue("com.glsx.aicar.push");
BaseRequest baseRequest = new BaseRequest();
BeanUtils.copyProperties(pushRequest,baseRequest);
R r = pushService.pushNotify(baseRequest);
log.info("fzpush====="+r);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Test
public void testPush_ios_notify() {
try {
BaseRequest pushRequest = new BaseRequest();
pushRequest.setType(PushType.ALIYUN.getCode());
pushRequest.setTitle("消息推送测试标题");
pushRequest.setBody("内容test");
pushRequest.setDeviceType("iOS");
pushRequest.setTargetValue("com.glsx.aicar.push");
R r = pushService.pushNotify(pushRequest);
log.info("fzpush====="+r);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!