forked from jpush/jpush-api-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushClient.java
More file actions
157 lines (129 loc) · 5.33 KB
/
PushClient.java
File metadata and controls
157 lines (129 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package cn.jpush.api.push;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import cn.jpush.api.common.BaseHttpClient;
import cn.jpush.api.common.DeviceEnum;
import cn.jpush.api.common.ResponseResult;
import cn.jpush.api.common.ValidateRequestParams;
import cn.jpush.api.utils.StringUtils;
public class PushClient extends BaseHttpClient {
private static final String HOST_NAME_SSL = "https://api.jpush.cn";
private static final String HOST_NAME = "http://api.jpush.cn:8800";
private static final String PUSH_PATH = "/v2/push";
private String appKey;
private String masterSecret;
private long timeToLive = -1;
private boolean enableSSL = false;
private boolean apnsProduction = false;
private Set<DeviceEnum> devices = new HashSet<DeviceEnum>();
public PushClient(String masterSecret, String appKey, long timeToLive, DeviceEnum device, boolean apnsProduction) {
this.masterSecret = masterSecret;
this.appKey = appKey;
this.timeToLive = timeToLive;
this.apnsProduction = apnsProduction;
if (null != device) {
this.devices.add(device);
}
}
public MessageResult sendNotification(String notificationContent, NotificationParams params, Map<String, Object> extras) {
if (null != extras) {
params.getMsgContent().setExtras(extras);
}
return sendMessage(notificationContent, params);
}
public MessageResult sendCustomMessage(String msgTitle, String msgContent, CustomMessageParams params, Map<String, Object> extras) {
if (null != msgTitle) {
params.getMsgContent().setTitle(msgTitle);
}
if (null != extras) {
params.getMsgContent().setExtras(extras);
}
return sendMessage(msgContent, params);
}
private MessageResult sendMessage(String content, MessageParams params) {
params.setApnsProduction(this.apnsProduction ? 1 : 0);
params.setAppKey(this.getAppKey());
params.setMasterSecret(this.masterSecret);
if (params.getTimeToLive() == MessageParams.NO_TIME_TO_LIVE) {
// no specific will then use the setting in instance
params.setTimeToLive(this.timeToLive);
}
for (DeviceEnum device : this.getDevices()) {
params.addPlatform(device);
}
params.getMsgContent().setMessage(content);
return sendPush(enableSSL, params);
}
public MessageResult sendPush(final boolean enableSSL, final MessageParams params) {
ValidateRequestParams.checkPushParams(params);
String url = enableSSL ? HOST_NAME_SSL : HOST_NAME;
url += PUSH_PATH;
ResponseResult result = sendPost(url, enableSSL, parse(params), null);
MessageResult rr = null;
if (result.responseCode == RESPONSE_OK) {
rr = _gson.fromJson(result.responseContent, MessageResult.class);
} else {
rr = new MessageResult();
}
rr.responseResult = result;
return rr;
}
protected String parse(MessageParams message) {
String input = String.valueOf(message.getSendNo()) + message.getReceiverType().value() + message.getReceiverValue() + message.getMasterSecret();
int msgType = 0;
if (message instanceof NotificationParams) {
msgType = MsgTypeEnum.NOTIFY.value();
} else if (message instanceof CustomMessageParams) {
msgType = MsgTypeEnum.CUSTOM.value();
}
Map<String, String> nvPair = new HashMap<String, String>();
nvPair.put("sendno", String.valueOf(message.getSendNo()));
nvPair.put("app_key", message.getAppKey());
nvPair.put("receiver_type", String.valueOf(message.getReceiverType().value()));
nvPair.put("receiver_value", message.getReceiverValue());
nvPair.put("verification_code", StringUtils.toMD5(input));
nvPair.put("msg_type", String.valueOf(msgType));
nvPair.put("msg_content", message.getMsgContent().toString());
nvPair.put("platform", message.getPlatform());
nvPair.put("apns_production", message.getApnsProduction() + "");
if (message.getTimeToLive() >=0) {
nvPair.put("time_to_live", String.valueOf(message.getTimeToLive()));
}
if(null != message.getOverrideMsgId() && !"".equals(message.getOverrideMsgId())){
nvPair.put("override_msg_id", message.getOverrideMsgId());
}
return mapWithParms(nvPair);
}
protected String mapWithParms(Map<String, String> nvPair){
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, String> entry : nvPair.entrySet()) {
builder.append(entry.getKey() + "=" + entry.getValue() + "&");
}
return builder.toString();
}
public String getMasterSecret() {
return masterSecret;
}
public long getTimeToLive() {
return timeToLive;
}
public String getAppKey() {
return this.appKey;
}
public Set<DeviceEnum> getDevices() {
if (null == this.devices) {
this.devices = new HashSet<DeviceEnum>();
}
if (this.devices.size() == 0) {
this.devices.add(DeviceEnum.Android);
this.devices.add(DeviceEnum.IOS);
this.devices.add(DeviceEnum.WinPhone);
}
return this.devices;
}
public void setEnableSSL(boolean enableSSL) {
this.enableSSL = enableSSL;
}
}