Commit 8c45e14a by fz

1.统一请求参数,按照业务区分通知服务接口以及相关配置

2.添加终端的通知跳转
1 parent 7f73444c
......@@ -2,7 +2,7 @@ package com.glsx.modules.notify.controller;
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.modules.notify.service.CarHomePushService;
import com.glsx.plat.core.web.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
......@@ -19,10 +19,10 @@ import org.springframework.web.bind.annotation.RestController;
*/
@RestController
@RequestMapping("/push")
@Api(value = "终端推送API", tags = {"通知或者消息推送"})
public class PushController {
@Api(value = "车家智能推送API", tags = {"通知或者消息推送"})
public class CarHomePushController {
@Autowired
private PushService pushService;
private CarHomePushService pushService;
/**
* 推送通知给终端
......
package com.glsx.modules.notify.factory.strategy;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
......@@ -12,7 +13,6 @@ 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;
......@@ -21,20 +21,7 @@ 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;
public class AliYunStrategy implements PushStrategy {
@Autowired
private IAcsClient client;
......@@ -73,25 +60,27 @@ public class AliYunStrategy extends BaseStrategy implements PushStrategy {
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);
//ios通知跳转
Map map=new HashMap();
map.put("type",request.getType());
String extParameters= JSON.toJSONString(map);
pushRequest.setIOSExtParameters(extParameters);
//设置开发环境
pushRequest.setIOSApnsEnv(request.getTestMode());
return sendRequest(pushRequest);
}
......@@ -111,10 +100,11 @@ public class AliYunStrategy extends BaseStrategy implements PushStrategy {
protected void buildBaseRequest(PushRequest request, BaseRequest baseRequest) {
request.setRegionId("cn-hangzhou");
request.setAppKey(Long.valueOf(baseRequest.getAppKey()));
request.setPushType(baseRequest.getPushType());
request.setDeviceType(baseRequest.getDeviceType());
request.setTarget(baseRequest.getTarget());
request.setTargetValue(baseRequest.getTargetValue());
request.setTargetValue(baseRequest.getAlias());
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.factory.strategy;
import com.aliyuncs.push.model.v20160801.PushResponse;
import com.glsx.modules.notify.factory.PushStrategy;
import com.glsx.modules.notify.model.BaseRequest;
import com.glsx.modules.notify.push.BroadCastService;
......@@ -24,7 +23,6 @@ public class YouMengStrategy implements PushStrategy {
return R.error("终端类型DeviceType不能为空");
}
log.info("====="+deviceType);
//iOS:iOS设备
//ANDROID:Android设备
if (deviceType.equals("ANDROID")) {
......@@ -39,7 +37,7 @@ public class YouMengStrategy implements PushStrategy {
}
private R pushIOSNotify(BaseRequest request) {
return null;
return R.error("ios友盟推送还未实现对接");
}
private R pushAndroidNotify(BaseRequest request) {
......
......@@ -8,12 +8,23 @@ public class BaseRequest {
/**
* 1.阿里云,2.友盟
*/
private Integer type;
private Integer platformType;
/**
* 平台的AppKey(分安卓ios)
* DEV:表示开发环境。
* PRODUCT:表示生产环境
*/
private Long appKey;
private String testMode;
/**
* 平台的AppKey(安卓ios的key不相同)
*/
private String appKey;
/**
* 平台的AppKey(友盟需要传)
*/
private String appMasterSecret;
/**
* 推送类型
......@@ -60,5 +71,20 @@ public class BaseRequest {
* Target=ALL,值为ALL。
* Target=TBD,值为TBD。
*/
private String targetValue;
private String alias;
/**
* 该参数只对友盟有意义(alias,aliasType)
*/
private String aliasType;
/**
* 点击通知后跳转,android相关为activity值
*/
private String activity;
/**
* 点击通知后跳转,ios相关为type值
*/
private String type;
}
......@@ -7,18 +7,18 @@ 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 = "iOS:iOS设备,ANDROID:Android设备 ",required = true,example = "ANDROID")
private String DeviceType;
@ApiModelProperty(value = "推送平台 1:阿里推送,2:友盟推送",required = true,example = "1")
private Integer platformType;
@ApiModelProperty(value = "这里传别名,即用户ID",required = true)
private String alias;
@ApiModelProperty(value = "Android/iOS推送时的消息/通知标题",required = true)
private String Title;
private String title;
@ApiModelProperty(value = "Android/iOS推送时的消息/通知内容,推送的内容大小是有限制的",required = true)
private String Body;
@ApiModelProperty(value = "这里传别名,即用户ID",required = true)
private String TargetValue;
private String body;
}
package com.glsx.modules.notify.push;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.glsx.modules.notify.model.BaseRequest;
import com.glsx.modules.notify.push.android.AndroidBroadcast;
import com.glsx.modules.notify.push.android.AndroidCustomizedcast;
import com.glsx.modules.notify.push.android.AndroidGroupcast;
import com.glsx.modules.notify.push.android.AndroidUnicast;
import com.glsx.modules.notify.push.ios.IOSBroadcast;
import com.glsx.modules.notify.push.ios.IOSCustomizedcast;
import com.glsx.modules.notify.push.ios.IOSGroupcast;
import com.glsx.modules.notify.push.ios.IOSUnicast;
import com.glsx.plat.core.web.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class BroadCastService {
@Value("${youmeng.appkey}")
private String appkey;
@Value("${youmeng.appMasterSecret}")
private String appMasterSecret;
@Autowired
private PushClient client;
@Value("${youmeng.android.testMode}")
private String testMode;
public R sendAndroidCustomizedcast(BaseRequest baseRequest) throws Exception {
AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(baseRequest.getAppKey(), baseRequest.getAppMasterSecret());
// TODO Set your alias here, and use comma to split them if there are multiple alias.
// And if you have many alias, you can also upload a file containing these alias, then
// use file_id to send customized notification.
@Value("${youmeng.android.aliasType}")
private String aliasType;
//addAlias("你与后台商定的用来区分用户的不能相同的", "可以是写死的别名项目名随意"
customizedcast.setAlias(baseRequest.getAlias(), baseRequest.getAliasType());
//通知栏提示文字
customizedcast.setTicker(baseRequest.getTitle());
//通知标题
customizedcast.setTitle(baseRequest.getTitle());
//通知文字描述
customizedcast.setText(baseRequest.getBody());
@Autowired
private PushClient client;
//通知跳转,如果activity为空,默认跳转应用,否则跳转指定activity
String activity = baseRequest.getActivity();
if (StringUtils.isEmpty(activity))
customizedcast.goAppAfterOpen();
else
customizedcast.goActivityAfterOpen(activity);
customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
// TODO Set 'production_mode' to 'false' if it's a test device.
// For how to register a test device, please see the developer doc.
if (baseRequest.getTestMode().equals("DEV"))
customizedcast.setTestMode();
else
customizedcast.setProductionMode();
public void sendAndroidBroadcast() throws Exception {
//todo 厂商通道相关参数
customizedcast.setChannelActivity("your channel activity");
customizedcast.setChannelProperties("abc");
return client.send(customizedcast);
}
/*public void sendAndroidBroadcast() throws Exception {
AndroidBroadcast broadcast = new AndroidBroadcast(appkey, appMasterSecret);
broadcast.setTicker("Android broadcast ticker");
broadcast.setTitle("中文的title");
......@@ -71,17 +87,17 @@ public class BroadCastService {
public void sendAndroidGroupcast() throws Exception {
AndroidGroupcast groupcast = new AndroidGroupcast(appkey, appMasterSecret);
/* TODO
* Construct the filter condition:
* "where":
* {
* "and":
* [
* {"tag":"test"},
* {"tag":"Test"}
* ]
* }
*/
*//* TODO
* Construct the filter condition:
* "where":
* {
* "and":
* [
* {"tag":"test"},
* {"tag":"Test"}
* ]
* }
*//*
JSONObject filterJson = new JSONObject();
JSONObject whereJson = new JSONObject();
JSONArray tagArray = new JSONArray();
......@@ -108,36 +124,8 @@ public class BroadCastService {
groupcast.setChannelActivity("your channel activity");
groupcast.setChannelProperties("abc");
client.send(groupcast);
}
}*/
public R sendAndroidCustomizedcast(BaseRequest baseRequest) throws Exception {
AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey, appMasterSecret);
// TODO Set your alias here, and use comma to split them if there are multiple alias.
// And if you have many alias, you can also upload a file containing these alias, then
// use file_id to send customized notification.
//addAlias("你与后台商定的用来区分用户的不能相同的", "可以是写死的别名项目名随意"
customizedcast.setAlias(baseRequest.getTargetValue(), aliasType);
//通知栏提示文字
customizedcast.setTicker(baseRequest.getTitle());
//通知标题
customizedcast.setTitle(baseRequest.getTitle());
//通知文字描述
customizedcast.setText(baseRequest.getBody());
customizedcast.goAppAfterOpen();
customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
// TODO Set 'production_mode' to 'false' if it's a test device.
// For how to register a test device, please see the developer doc.
if (testMode.equals("true"))
customizedcast.setTestMode();
else
customizedcast.setProductionMode();
//厂商通道相关参数
customizedcast.setChannelActivity("your channel activity");
customizedcast.setChannelProperties("abc");
return client.send(customizedcast);
}
//public void sendAndroidCustomizedcastFile() throws Exception {
// AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
......@@ -176,7 +164,7 @@ public class BroadCastService {
// client.send(filecast);
//}
public void sendIOSBroadcast() throws Exception {
/*public void sendIOSBroadcast() throws Exception {
IOSBroadcast broadcast = new IOSBroadcast(appkey, appMasterSecret);
//alert值设置为字符串
//broadcast.setAlert("IOS 广播测试");
......@@ -211,16 +199,16 @@ public class BroadCastService {
public void sendIOSGroupcast() throws Exception {
IOSGroupcast groupcast = new IOSGroupcast(appkey, appMasterSecret);
/* TODO
* Construct the filter condition:
* "where":
* {
* "and":
* [
* {"tag":"iostest"}
* ]
* }
*/
*//* TODO
* Construct the filter condition:
* "where":
* {
* "and":
* [
* {"tag":"iostest"}
* ]
* }
*//*
JSONObject filterJson = new JSONObject();
JSONObject whereJson = new JSONObject();
JSONArray tagArray = new JSONArray();
......@@ -259,18 +247,18 @@ public class BroadCastService {
client.send(customizedcast);
}
//public void sendIOSFilecast() throws Exception {
// IOSFilecast filecast = new IOSFilecast(appkey,appMasterSecret);
// // TODO upload your device tokens, and use '\n' to split them if there are multiple tokens
// String fileId = client.uploadContents(appkey,appMasterSecret,"aa"+"\n"+"bb");
// filecast.setFileId( fileId);
// //filecast.setAlert("IOS 文件播测试");
// //alert的值设置为字典
// filecast.setAlert("今日天气" , "" , "今日可能下雨🌂");
// filecast.setBadge( 0);
// filecast.setSound( "default");
// // TODO set 'production_mode' to 'true' if your app is under production mode
// filecast.setTestMode();
// client.send(filecast);
//}
public void sendIOSFilecast() throws Exception {
IOSFilecast filecast = new IOSFilecast(appkey,appMasterSecret);
// TODO upload your device tokens, and use '\n' to split them if there are multiple tokens
String fileId = client.uploadContents(appkey,appMasterSecret,"aa"+"\n"+"bb");
filecast.setFileId( fileId);
//filecast.setAlert("IOS 文件播测试");
//alert的值设置为字典
filecast.setAlert("今日天气" , "" , "今日可能下雨🌂");
filecast.setBadge( 0);
filecast.setSound( "default");
// TODO set 'production_mode' to 'true' if your app is under production mode
filecast.setTestMode();
client.send(filecast);
}*/
}
......@@ -108,8 +108,8 @@ public class PushClient {
msg.setPredefinedKeyValue("timestamp", timestamp);
String url = host + postPath;
String postBody = msg.getPostBody();
log.info("调用url======="+url);
log.info("调用body======="+postBody);
//log.info("调用url======="+url);
//log.info("调用body======="+postBody);
String sign = DigestUtils.md5Hex(("POST" + url + postBody + msg.getAppMasterSecret()).getBytes("utf8"));
url = url + "?sign=" + sign;
JSONObject respJson = null;
......
package com.glsx.modules.notify.service;
import com.glsx.modules.notify.model.BaseRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class CarHomePushService extends PushService{
/**
* DEV表示开发环境,PRODUCT表示生产环境
*/
@Value("${youmeng.carhome.testMode}")
private String testMode;
/************************ android配置 ************************/
/**
* 友盟平台android的AppKey
*/
@Value("${youmeng.carhome.android.appkey}")
private String appKey_android_youmeng;
/**
* 友盟平台android的appMasterSecret
*/
@Value("${youmeng.carhome.android.appMasterSecret}")
private String appMasterSecret_android_youmeng;
/**
* 可以是写死的别名项目名随意
*/
@Value("${youmeng.carhome.android.aliasType}")
private String aliasType_android_youmeng;
/**
* 点击通知后跳转,android为activity值,ios为type值
*/
@Value("${youmeng.carhome.android.activity}")
private String activity_android_youmeng;
/************************ ios配置 ************************/
/**
* aliyun平台的AppKey
*/
@Value("${aliyun.carhome.ios.appKey}")
private String appKey_ios_aliyun;
/**
* 点击通知后跳转,android为activity值,ios为type值
*/
@Value("${aliyun.carhome.ios.type}")
private String type_ios_aliyun;
@Override
protected void setiOSAliyunValue(BaseRequest pushRequest) {
//appkey
pushRequest.setAppKey(appKey_ios_aliyun);
//环境
pushRequest.setTestMode(testMode);
//通知跳转
pushRequest.setType(type_ios_aliyun);
}
@Override
protected void setiOSYouMengValue(BaseRequest pushRequest) {
}
@Override
protected void setAndroidYouMengValue(BaseRequest pushRequest) {
//appkey
pushRequest.setAppKey(appKey_android_youmeng);
//appMasterSecret
pushRequest.setAppMasterSecret(appMasterSecret_android_youmeng);
//环境
pushRequest.setTestMode(testMode);
//AliasType
pushRequest.setAliasType(aliasType_android_youmeng);
//通知跳转
pushRequest.setActivity(activity_android_youmeng);
}
@Override
protected void setAndroidAliyunValue(BaseRequest pushRequest) {
}
}
......@@ -2,27 +2,49 @@ 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.factory.PushType;
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);
}
public abstract class PushService {
@Autowired
private PushFactory pushFactory;
public R pushNotify(BaseRequest pushRequest) {
//如果是阿里云
if (pushRequest.getPlatformType() == PushType.ALIYUN.getCode()) {
if (pushRequest.getDeviceType().equals("ANDROID"))
setAndroidAliyunValue(pushRequest);
if (pushRequest.getDeviceType().equals("iOS"))
setiOSAliyunValue(pushRequest);
}
//如果是友盟
if (pushRequest.getPlatformType() == PushType.YOUMENG.getCode()) {
if (pushRequest.getDeviceType().equals("ANDROID"))
setAndroidYouMengValue(pushRequest);
if (pushRequest.getDeviceType().equals("iOS"))
setiOSYouMengValue(pushRequest);
}
//获取对应策略,1:任意触发条件满足 2:全部触发条件满足
PushStrategy pushStrategy = null;
try {
pushStrategy = pushFactory.getStrategy(pushRequest.getPlatformType());
} catch (Exception e) {
R.error("创建策略失败:" + e.getMessage());
}
return pushStrategy.pushNotify(pushRequest);
}
protected abstract void setiOSAliyunValue(BaseRequest pushRequest);
protected abstract void setiOSYouMengValue(BaseRequest pushRequest);
protected abstract void setAndroidYouMengValue(BaseRequest pushRequest);
protected abstract void setAndroidAliyunValue(BaseRequest pushRequest);
}
......@@ -4,8 +4,10 @@ import com.glsx.modules.notify.factory.PushType;
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.CarHomePushService;
import com.glsx.modules.notify.service.PushService;
import com.glsx.plat.core.web.R;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -13,47 +15,33 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@Slf4j
public class DeviceControllerTest {
@Autowired
private BroadCastService demo;
@Autowired
PushService pushService;
private CarHomePushService pushService;
@Test
public void testPush_android_notify() {
try {
//demo.sendAndroidUnicast();
//demo.sendIOSUnicast();
/* TODO these methods are all available, just fill in some fields and do the test
* demo.sendAndroidCustomizedcastFile();
* demo.sendAndroidBroadcast();
* demo.sendAndroidGroupcast();
* demo.sendAndroidCustomizedcast();
* demo.sendAndroidFilecast();
*
* demo.sendIOSBroadcast();
* demo.sendIOSUnicast();
* demo.sendIOSGroupcast();
* demo.sendIOSCustomizedcast();
* demo.sendIOSFilecast();
*/
NotifyReqParam pushRequest = new NotifyReqParam();
pushRequest.setType(PushType.YOUMENG.getCode());
//推送平台参数
pushRequest.setPlatformType(PushType.YOUMENG.getCode());
pushRequest.setDeviceType("ANDROID");
//推送别名参数
pushRequest.setAlias("Auv2Nn2JkqhFypmb1gm8u3gVCv1Y5T7xcsWo1YueLrck");
//通知参数
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);
log.info("fzpush===android==="+r);
} catch (Exception ex) {
ex.printStackTrace();
}
......@@ -62,14 +50,21 @@ public class DeviceControllerTest {
@Test
public void testPush_ios_notify() {
try {
BaseRequest pushRequest = new BaseRequest();
pushRequest.setType(PushType.ALIYUN.getCode());
pushRequest.setTitle("消息推送测试标题");
pushRequest.setBody("内容test");
NotifyReqParam pushRequest = new NotifyReqParam();
//推送平台参数
pushRequest.setPlatformType(PushType.ALIYUN.getCode());
pushRequest.setDeviceType("iOS");
pushRequest.setTargetValue("com.glsx.aicar.push");
R r = pushService.pushNotify(pushRequest);
log.info("fzpush====="+r);
//推送别名参数
pushRequest.setAlias("com.glsx.aicar.push");
//通知参数
pushRequest.setTitle("这是消息推送测试标题");
pushRequest.setBody("这是内容test");
BaseRequest baseRequest = new BaseRequest();
BeanUtils.copyProperties(pushRequest,baseRequest);
R r = pushService.pushNotify(baseRequest);
log.info("fzpush===ios==="+r);
} catch (Exception ex) {
ex.printStackTrace();
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!