Commit 7f73444c by fz

1.添加对接友盟平台推送,传平台策略字段区分

1 parent 47aaa352
......@@ -220,7 +220,7 @@ public class HttpUtil {
int statusCode = statusLine.getStatusCode();
if (statusCode != 200) {
HTTPException exception = new HTTPException(statusCode);
logger.error("请求地址" + url + "出错,http 状态码为:" + statusCode, exception);
logger.error("请求地址" + url + "出错,http 状态码为:" + statusCode+",错误信息:"+exception.getMessage(), exception);
throw exception;
}
String retData = EntityUtils.toString(entity, JSON_UTF8.getCharset());
......
......@@ -66,8 +66,7 @@ public class AliYunStrategy extends BaseStrategy implements PushStrategy {
return R.error("push请求失败");
Map<String, Object> map = new HashMap();
map.put("RequestId", pushResponse.getRequestId());
map.put("MessageId", pushResponse.getMessageId());
map.put("msg_id", pushResponse.getMessageId());
return R.ok().data(map);
}
......
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;
import com.glsx.plat.core.web.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
//全部满足触发条件策略
@Slf4j
@Component
public class YouMengStrategy implements PushStrategy {
@Autowired
private BroadCastService broadCastService;
@Override
public R pushNotify(BaseRequest request) {
String deviceType = request.getDeviceType();
if (StringUtils.isEmpty(deviceType)) {
return R.error("终端类型DeviceType不能为空");
}
log.info("====="+deviceType);
//iOS:iOS设备
//ANDROID:Android设备
if (deviceType.equals("ANDROID")) {
return pushAndroidNotify(request);
}
if (deviceType.equals("iOS")) {
return pushIOSNotify(request);
}
return R.error("终端类型DeviceType不匹配");
}
private R pushIOSNotify(BaseRequest request) {
return null;
}
private R pushAndroidNotify(BaseRequest request) {
try {
return broadCastService.sendAndroidCustomizedcast(request);
} catch (Exception e) {
e.printStackTrace();
return R.error(e.getMessage());
}
}
//public void sendAndroidCustomizedcast() throws Exception {
// AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
// // TODO Set your alias here, and use comma to split them if there are multiple alias.
......
......@@ -6,7 +6,7 @@ import lombok.Data;
@Data
public class BaseRequest {
/**
* 1.阿里云(其他暂未实现)
* 1.阿里云,2.友盟
*/
private Integer type;
......
......@@ -2,6 +2,7 @@ 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;
......@@ -10,6 +11,7 @@ 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.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
......@@ -21,14 +23,20 @@ public class BroadCastService {
@Value("${youmeng.appMasterSecret}")
private String appMasterSecret;
@Value("${youmeng.android.testMode}")
private String testMode;
@Value("${youmeng.android.aliasType}")
private String aliasType;
@Autowired
private PushClient client;
public void sendAndroidBroadcast() throws Exception {
AndroidBroadcast broadcast = new AndroidBroadcast(appkey,appMasterSecret);
broadcast.setTicker( "Android broadcast ticker");
broadcast.setTitle( "中文的title");
broadcast.setText( "Android broadcast text");
AndroidBroadcast broadcast = new AndroidBroadcast(appkey, appMasterSecret);
broadcast.setTicker("Android broadcast ticker");
broadcast.setTitle("中文的title");
broadcast.setText("Android broadcast text");
broadcast.goAppAfterOpen();
broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
// TODO Set 'production_mode' to 'false' if it's a test device.
......@@ -43,12 +51,12 @@ public class BroadCastService {
}
public void sendAndroidUnicast() throws Exception {
AndroidUnicast unicast = new AndroidUnicast(appkey,appMasterSecret);
AndroidUnicast unicast = new AndroidUnicast(appkey, appMasterSecret);
// TODO Set your device token
unicast.setDeviceToken( "your device_token");
unicast.setTicker( "Android unicast ticker");
unicast.setTitle( "中文的title");
unicast.setText( "Android unicast text");
unicast.setDeviceToken("your device_token");
unicast.setTicker("Android unicast ticker");
unicast.setTitle("中文的title");
unicast.setText("Android unicast text");
unicast.goAppAfterOpen();
unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
// TODO Set 'production_mode' to 'false' if it's a test device.
......@@ -62,7 +70,7 @@ public class BroadCastService {
}
public void sendAndroidGroupcast() throws Exception {
AndroidGroupcast groupcast = new AndroidGroupcast(appkey,appMasterSecret);
AndroidGroupcast groupcast = new AndroidGroupcast(appkey, appMasterSecret);
/* TODO
* Construct the filter condition:
* "where":
......@@ -87,9 +95,9 @@ public class BroadCastService {
filterJson.put("where", whereJson);
groupcast.setFilter(filterJson);
groupcast.setTicker( "Android groupcast ticker");
groupcast.setTitle( "中文的title");
groupcast.setText( "Android groupcast text");
groupcast.setTicker("Android groupcast ticker");
groupcast.setTitle("中文的title");
groupcast.setText("Android groupcast text");
groupcast.goAppAfterOpen();
groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
groupcast.setChannelActivity("your channel activity");
......@@ -102,29 +110,33 @@ public class BroadCastService {
client.send(groupcast);
}
public void sendAndroidCustomizedcast() throws Exception {
AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
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("alias", "alias_type");
customizedcast.setAlias(baseRequest.getTargetValue(), aliasType);
//通知栏提示文字
customizedcast.setTicker( "Android customizedcast ticker");
customizedcast.setTicker(baseRequest.getTitle());
//通知标题
customizedcast.setTitle( "中文的title");
customizedcast.setTitle(baseRequest.getTitle());
//通知文字描述
customizedcast.setText( "Android customizedcast text");
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");
client.send(customizedcast);
return client.send(customizedcast);
}
//public void sendAndroidCustomizedcastFile() throws Exception {
......@@ -165,13 +177,13 @@ public class BroadCastService {
//}
public void sendIOSBroadcast() throws Exception {
IOSBroadcast broadcast = new IOSBroadcast(appkey,appMasterSecret);
IOSBroadcast broadcast = new IOSBroadcast(appkey, appMasterSecret);
//alert值设置为字符串
//broadcast.setAlert("IOS 广播测试");
//alert的值设置为字典
broadcast.setAlert("今日天气" , "" , "今日可能下雨🌂");
broadcast.setBadge( 0);
broadcast.setSound( "default");
broadcast.setAlert("今日天气", "", "今日可能下雨🌂");
broadcast.setBadge(0);
broadcast.setSound("default");
// TODO set 'production_mode' to 'true' if your app is under production mode
broadcast.setTestMode();
// Set customized fields
......@@ -180,15 +192,15 @@ public class BroadCastService {
}
public void sendIOSUnicast() throws Exception {
IOSUnicast unicast = new IOSUnicast(appkey,appMasterSecret);
IOSUnicast unicast = new IOSUnicast(appkey, appMasterSecret);
// TODO Set your device token
unicast.setDeviceToken( "your device_token");
unicast.setDeviceToken("your device_token");
//alert值设置为字符串
//unicast.setAlert("IOS 单播测试");
//alert的值设置为字典
unicast.setAlert("今日天气" , "" , "今日可能下雨🌂");
unicast.setBadge( 0);
unicast.setSound( "default");
unicast.setAlert("今日天气", "", "今日可能下雨🌂");
unicast.setBadge(0);
unicast.setSound("default");
// TODO set 'production_mode' to 'true' if your app is under production mode
unicast.setTestMode();
// Set customized fields
......@@ -198,7 +210,7 @@ public class BroadCastService {
public void sendIOSGroupcast() throws Exception {
IOSGroupcast groupcast = new IOSGroupcast(appkey,appMasterSecret);
IOSGroupcast groupcast = new IOSGroupcast(appkey, appMasterSecret);
/* TODO
* Construct the filter condition:
* "where":
......@@ -223,25 +235,25 @@ public class BroadCastService {
groupcast.setFilter(filterJson);
//groupcast.setAlert("IOS 组播测试");
//alert的值设置为字典
groupcast.setAlert("今日天气" , "subtitle" , "今日可能下雨🌂");
groupcast.setBadge( 0);
groupcast.setSound( "default");
groupcast.setAlert("今日天气", "subtitle", "今日可能下雨🌂");
groupcast.setBadge(0);
groupcast.setSound("default");
// TODO set 'production_mode' to 'true' if your app is under production mode
groupcast.setTestMode();
client.send(groupcast);
}
public void sendIOSCustomizedcast() throws Exception {
IOSCustomizedcast customizedcast = new IOSCustomizedcast(appkey,appMasterSecret);
IOSCustomizedcast customizedcast = new IOSCustomizedcast(appkey, appMasterSecret);
// TODO Set your alias and alias_type 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.
customizedcast.setAlias("alias", "alias_type");
//customizedcast.setAlert("IOS 个性化测试");
//alert的值设置为字典
customizedcast.setAlert("今日天气" , "" , "今日可能下雨🌂");
customizedcast.setBadge( 0);
customizedcast.setSound( "default");
customizedcast.setAlert("今日天气", "", "今日可能下雨🌂");
customizedcast.setBadge(0);
customizedcast.setSound("default");
// TODO set 'production_mode' to 'true' if your app is under production mode
customizedcast.setTestMode();
client.send(customizedcast);
......
......@@ -3,12 +3,15 @@ package com.glsx.modules.notify.push;
import com.alibaba.fastjson.JSONObject;
import com.glsx.common.utils.HttpUtil;
import com.glsx.plat.core.web.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Component
@Slf4j
......@@ -100,7 +103,7 @@ public class PushClient {
//
// return fileId;
//}
public boolean send(UmengNotification msg) throws Exception {
public R send(UmengNotification msg) throws Exception {
String timestamp = Integer.toString((int) (System.currentTimeMillis() / 1000));
msg.setPredefinedKeyValue("timestamp", timestamp);
String url = host + postPath;
......@@ -123,8 +126,11 @@ public class PushClient {
String errorCode = data.getString("error_code");
String errorMsg = data.getString("error_msg");
log.error("友盟fail===errorCode:" + errorCode + "===errorMsg:" + errorMsg);
return false;
return R.error(errorCode,errorMsg);
}
return true;
Map<String, Object> map = new HashMap();
map.put("msg_id", data.get("msg_id"));
return R.ok().data(map);
}
}
......@@ -44,7 +44,7 @@ public class DeviceControllerTest {
*/
NotifyReqParam pushRequest = new NotifyReqParam();
pushRequest.setType(PushType.ALIYUN.getCode());
pushRequest.setType(PushType.YOUMENG.getCode());
pushRequest.setTitle("消息推送测试标题");
pushRequest.setBody("内容test");
pushRequest.setDeviceType("ANDROID");
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!