Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

个推添加通知消息方式(原先只支持透传消息) #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<groupId>com.sensorsdata.focus</groupId>
<artifactId>sf-channel-push-getui</artifactId>
<version>0.1.2</version>
<version>0.1.3-SNAPSHOT</version>

<repositories>
<repository>
Expand All @@ -49,7 +49,7 @@
<dependency>
<groupId>com.gexin.platform</groupId>
<artifactId>gexin-rp-sdk-http</artifactId>
<version>4.1.0.5</version>
<version>4.1.1.3</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -160,4 +160,11 @@
</plugins>
</pluginManagement>
</build>

<distributionManagement>
<snapshotRepository>
<id>nexus</id>
<url>http://git.sensorsdata.cn:8280/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class GetuiChannelConfig extends ChannelConfig {
@ConfigField(cname = "请求地址", desc = "个推请求地址", defaultValue = "http://sdk.open.api.igexin.com/apiex.htm")
private String url = "http://sdk.open.api.igexin.com/apiex.htm";

@ConfigField(cname = "推送类型", desc = "推送类型", defaultValue = "PASSTHROUGH")
private String pushType;

@ConfigField(cname = "AppID", desc = "平台 AppID,可在平台配置页面获取")
@NotBlank
@Size(min = 22, max = 22)
Expand Down
70 changes: 54 additions & 16 deletions src/main/java/cn/sensorsdata/focus/channel/GetuiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.sensorsdata.focus.channel.entry.PushTask;
import com.sensorsdata.focus.channel.push.PushTaskUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
Expand All @@ -47,6 +48,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -68,6 +70,7 @@ public class GetuiClient extends ChannelClient {
private String getuiApiHost;
private String appKey;
private String appId;
private String pushType;

private static final String STR_SF_DATA = "sf_data";

Expand All @@ -78,6 +81,7 @@ public void initChannelClient(ChannelConfig channelConfig) {
appId = getuiChannelConfig.getAppId();
appKey = getuiChannelConfig.getAppKey();
getuiApiHost = getuiChannelConfig.getUrl();
pushType = getuiChannelConfig.getPushType();
String masterSecret = getuiChannelConfig.getMasterSecret();

// 这个 client 的 close 还要发 http 请求,比较奇怪,先不管
Expand All @@ -97,7 +101,12 @@ public void send(List<MessagingTask> messagingTasks) throws Exception {
MessagingTask messagingTask = taskList.get(0);
PushTask pushTask = messagingTask.getPushTask();

AbstractTemplate template = constructTemplate2(pushTask);
AbstractTemplate template = null;
if (StringUtils.isBlank(pushType) || "PASSTHROUGH".equalsIgnoreCase(pushType)) {
template = constructTemplate2(pushTask);
} else {
template = constructTemplateForNotification(pushTask);
}
template.setAppId(appId);
template.setAppkey(appKey);

Expand All @@ -107,6 +116,8 @@ public void send(List<MessagingTask> messagingTasks) throws Exception {
message.setData(template);
message.setOffline(true);
message.setOfflineExpireTime(MESSAGE_OFFLINE_EXPIRE_TIME);
// 厂商下发策略:控制消息经由个推通道或厂商下发。
message.setStrategyJson(constructStrategyJson());

singleMessageBatch.add(Pair.of(message, messagingTask));
log.debug("add push task into batch. [task='{}']", messagingTask);
Expand All @@ -126,6 +137,8 @@ public void send(List<MessagingTask> messagingTasks) throws Exception {
message.setOffline(true);
// 离线有效时间,单位为毫秒,可选
message.setOfflineExpireTime(MESSAGE_OFFLINE_EXPIRE_TIME);
// 厂商下发策略:控制消息经由个推通道或厂商下发
message.setStrategyJson(constructStrategyJson());

String failReason = null;
try {
Expand Down Expand Up @@ -246,6 +259,41 @@ private String constructTransmissionContent(PushTask pushTask) {
return pushTask.getSfData();
}

/**
* ios消息推送
*
* @param pushTask 单条推送任务
* @return
*/
private APNPayload constructAPNPayload(PushTask pushTask) {
APNPayload apnPayload = new APNPayload();
APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
alertMsg.setTitle(pushTask.getMsgTitle());
alertMsg.setBody(pushTask.getMsgContent());
apnPayload.setAlertMsg(alertMsg);
apnPayload.addCustomMsg(STR_SF_DATA, pushTask.getSfData());
return apnPayload;
}

/**
* 构建厂商下发策略:控制消息经由个推通道或厂商下发。
* 主要影响iOS通知消息
* 具体信息可以参考:http://docs.getui.com/getui/server/java/push/
*
* @return
*/
private String constructStrategyJson() {
String result = null;
try {
Map<String, Integer> strategyJsonMap = new HashMap<>();
strategyJsonMap.put("ios", 4);
result = OBJECT_MAPPER.writeValueAsString(strategyJsonMap);
} catch (JsonProcessingException e) {
log.error("construct strategyJson exception. ", e);
}
return result;
}

/**
* 使用透传模板构造消息
*/
Expand All @@ -268,12 +316,7 @@ private TransmissionTemplate constructTemplate2(PushTask pushTask) {
transmissionTemplate.set3rdNotifyInfo(notify);
}

APNPayload apnPayload = new APNPayload();
APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
alertMsg.setTitle(pushTask.getMsgTitle());
alertMsg.setBody(pushTask.getMsgContent());
apnPayload.setAlertMsg(alertMsg);
apnPayload.addCustomMsg(STR_SF_DATA, pushTask.getSfData());
APNPayload apnPayload = constructAPNPayload(pushTask);
transmissionTemplate.setAPNInfo(apnPayload);

log.debug("construct template. [content='{}', intent='{}', cid='{}']", transmissionContent, intent,
Expand All @@ -282,22 +325,17 @@ private TransmissionTemplate constructTemplate2(PushTask pushTask) {
}

/**
* 使用通知模板构造消息。默认使用 constructTemplate2 而不是本函数
* 使用通知模板构造消息。默认使用 constructTemplate2
*/
private NotificationTemplate constructTemplate(PushTask pushTask) { // NOSONAR 暂时保留这种方式
private NotificationTemplate constructTemplateForNotification(PushTask pushTask) {
NotificationTemplate notificationTemplate = new NotificationTemplate();
// 透传消息设置,1 为强制启动应用,客户端接收到消息后就会立即启动应用;2 为等待应用启动
notificationTemplate.setTransmissionType(1);
notificationTemplate.setTransmissionType(2);
// 这里也可以传消息,但如果目的只是打开 App,可以不传
notificationTemplate.setStyle(constructStyle(pushTask));
notificationTemplate.setTransmissionContent(pushTask.getSfData());

APNPayload apnPayload = new APNPayload();
APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
alertMsg.setTitle(pushTask.getMsgTitle());
alertMsg.setBody(pushTask.getMsgContent());
apnPayload.setAlertMsg(alertMsg);
apnPayload.addCustomMsg(STR_SF_DATA, pushTask.getSfData());
APNPayload apnPayload = constructAPNPayload(pushTask);
notificationTemplate.setAPNInfo(apnPayload);

return notificationTemplate;
Expand Down
58 changes: 41 additions & 17 deletions src/test/java/cn/sensorsdata/focus/channel/GetuiClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,66 @@
import org.junit.Ignore;
import org.junit.Test;

import java.util.Collections;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class GetuiClientTest extends TestCase {

// 个推账号
private static final String APP_ID = "aaaaaaaaaaaaaaaaaaaaaa";
private static final String APP_KEY = "bbbbbbbbbbbbbbbbbbbbbb";
private static final String MASTER_SECRET = "cccccccccccccccccccccc";
// 测试设备的推送 ID
private static final String CLIENT_ID = "dddddddddddddddddddddddddddddddd";
private static final String APP_ID = "geO2qJxEb99qGLGniiyyi4";
private static final String APP_KEY = "CJE44wmIfm5OffjboZ1iO3";
private static final String MASTER_SECRET = "mExfQVhKfv6K64HLnegs64";

@Test
@Ignore
public void testSend() throws Exception {
GetuiChannelConfig getuiChannelConfig = new GetuiChannelConfig();
getuiChannelConfig.setAppId(APP_ID);
getuiChannelConfig.setAppKey(APP_KEY);
getuiChannelConfig.setMasterSecret(MASTER_SECRET);
// 测试设备的推送 ID
List<String> clientIdList = new ArrayList<>();
clientIdList.add("ac02835e5a68e362dcd1992266fd7b8a");
clientIdList.add("3e0052bf7ae0816374ceea9255c0a54c");

// testDistinctPushType(null, clientIdList);
// testDistinctPushType("passthrough", clientIdList);
testDistinctPushType("notification", clientIdList);
}

private PushTask generatePushTask(String pushType, String clientId) {
PushTask pushTask = new PushTask();
pushTask.setLandingType(LandingType.CUSTOMIZED);
pushTask.setLandingType(LandingType.LINK);
Map<String, String> testMap = new LinkedHashMap<>();
testMap.put("aaa", "bb");
pushTask.setCustomized(testMap);
pushTask.setMsgContent("content111");
pushTask.setMsgTitle("title123");
pushTask.setClientId(CLIENT_ID);
pushTask.setMsgContent("content7" + pushType);
pushTask.setMsgTitle("title7" + pushType);
pushTask.setClientId(clientId);
pushTask.setLinkUrl("http://sensorsdata.cn");
pushTask.setSfData(
"{\"sf_link_url\":\"http://sensorsdata.cn\",\"sf_landing_type\":\"LINK\",\"sf_msg_id\":\"83ddb764-e163-453e-a715-d68621170b71\",\"sf_plan_id\":\"3\",\"sf_audience_id\":5,\"sf_plan_strategy_id\":\"0\",\"sf_strategy_unit_id\":\"100\",\"sf_plan_type\":\"运营计划\",\"customized\":{ \"book_id\":\"12345\",\"news_id\":\"678\"}}");
return pushTask;
}

MessagingTask messagingTask = new MessagingTask();
messagingTask.setPushTask(pushTask);
private void testDistinctPushType(String pushType, List<String> clientIdList) throws Exception {
GetuiChannelConfig getuiChannelConfig = new GetuiChannelConfig();
getuiChannelConfig.setAppId(APP_ID);
getuiChannelConfig.setAppKey(APP_KEY);
getuiChannelConfig.setMasterSecret(MASTER_SECRET);
getuiChannelConfig.setPushType(pushType);
// getuiChannelConfig.setIntentTemplate("intent:w w ww ;end");

List<MessagingTask> pushTaskList = new ArrayList<>();
clientIdList.forEach(clientId -> {
PushTask pushTask = generatePushTask(pushType, clientId);
MessagingTask messagingTask = new MessagingTask();
messagingTask.setPushTask(pushTask);
pushTaskList.add(messagingTask);
});

GetuiClient getuiClient = new GetuiClient();
getuiClient.initChannelClient(getuiChannelConfig);
getuiClient.send(Collections.singletonList(messagingTask));

getuiClient.send(pushTaskList);
getuiClient.close();
}
}