Skip to content

Commit 0d0b3e5

Browse files
author
yutaipu
committed
支持文件推送
1 parent 2532940 commit 0d0b3e5

File tree

9 files changed

+136
-132
lines changed

9 files changed

+136
-132
lines changed

.classpath

Lines changed: 0 additions & 61 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,4 @@ test-output/
1414
*.iml
1515

1616
*.releaseBackup
17-
release.properties
18-
17+
release.properties

.project

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/main/java/cn/jpush/api/JPushClient.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ public PushResult sendPush(PushPayload pushPayload) throws APIConnectionExceptio
204204
public PushResult sendPush(String payloadString) throws APIConnectionException, APIRequestException {
205205
return _pushClient.sendPush(payloadString);
206206
}
207+
208+
/**
209+
* Send a file push with PushPayload object.
210+
*
211+
* @param pushPayload payload object of a push.
212+
* @return PushResult The result object of a Push. Can be printed to a JSON.
213+
* @throws APIConnectionException if a remote or network exception occurs.
214+
* @throws APIRequestException if a request exception occurs.
215+
*/
216+
public PushResult sendFilePush(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
217+
return _pushClient.sendFilePush(pushPayload);
218+
}
207219

208220
/**
209221
* Validate a push action, but do NOT send it actually.
@@ -1233,5 +1245,7 @@ private ScheduleResult createPeriodicalSchedule(String name, String start, Strin
12331245
public void close() {
12341246
_pushClient.close();
12351247
}
1248+
1249+
12361250
}
12371251

src/main/java/cn/jpush/api/push/PushClient.java

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@
1414
import cn.jpush.api.push.model.*;
1515
import cn.jpush.api.push.model.audience.Audience;
1616
import com.google.gson.*;
17-
import com.google.gson.reflect.TypeToken;
1817

19-
import java.lang.reflect.Type;
20-
import java.util.HashMap;
2118
import java.util.List;
22-
import java.util.Map;
2319

2420
/**
2521
* Entrance for sending Push.
@@ -39,6 +35,7 @@ public class PushClient {
3935
private String _pushValidatePath;
4036
private String batchRegidPushPath;
4137
private String batchAliasPushPath;
38+
private String _filePushPath;
4239

4340
private JsonParser _jsonParser = new JsonParser();
4441

@@ -92,6 +89,7 @@ public PushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPro
9289
this._baseUrl = (String) conf.get(ClientConfig.PUSH_HOST_NAME);
9390
this._pushPath = (String) conf.get(ClientConfig.PUSH_PATH);
9491
this._pushValidatePath = (String) conf.get(ClientConfig.PUSH_VALIDATE_PATH);
92+
this._filePushPath = (String) conf.get(ClientConfig.FILE_PUSH_PATH);
9593

9694
this._apnsProduction = (Integer) conf.get(ClientConfig.APNS_PRODUCTION);
9795
this._timeToLive = (Long) conf.get(ClientConfig.TIME_TO_LIVE);
@@ -106,6 +104,7 @@ public PushClient(String masterSecret, String appKey, HttpProxy proxy, ClientCon
106104
this._baseUrl = (String) conf.get(ClientConfig.PUSH_HOST_NAME);
107105
this._pushPath = (String) conf.get(ClientConfig.PUSH_PATH);
108106
this._pushValidatePath = (String) conf.get(ClientConfig.PUSH_VALIDATE_PATH);
107+
this._filePushPath = (String) conf.get(ClientConfig.FILE_PUSH_PATH);
109108

110109
this.batchAliasPushPath = (String) conf.get(ClientConfig.BATCH_ALIAS_PUSH_PATH);
111110
this.batchRegidPushPath = (String) conf.get(ClientConfig.BATCH_REGID_PUSH_PATH);
@@ -162,35 +161,16 @@ public void setBaseUrl(String baseUrl) {
162161
}
163162

164163
public PushResult sendPush(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
165-
Preconditions.checkArgument(!(null == pushPayload), "pushPayload should not be null");
166-
167-
if (_apnsProduction > 0) {
168-
pushPayload.resetOptionsApnsProduction(true);
169-
} else if (_apnsProduction == 0) {
170-
pushPayload.resetOptionsApnsProduction(false);
171-
}
172-
173-
if (_timeToLive >= 0) {
174-
pushPayload.resetOptionsTimeToLive(_timeToLive);
175-
}
164+
checkPushPayload(pushPayload);
176165

177166
ResponseWrapper response = _httpClient.sendPost(_baseUrl + _pushPath, getEncryptData(pushPayload));
178167

179168
return BaseResult.fromResponse(response, PushResult.class);
180169
}
181170

182-
public PushResult sendPushValidate(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
183-
Preconditions.checkArgument(!(null == pushPayload), "pushPayload should not be null");
184-
185-
if (_apnsProduction > 0) {
186-
pushPayload.resetOptionsApnsProduction(true);
187-
} else if (_apnsProduction == 0) {
188-
pushPayload.resetOptionsApnsProduction(false);
189-
}
190171

191-
if (_timeToLive >= 0) {
192-
pushPayload.resetOptionsTimeToLive(_timeToLive);
193-
}
172+
public PushResult sendPushValidate(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
173+
checkPushPayload(pushPayload);
194174

195175
ResponseWrapper response = _httpClient.sendPost(_baseUrl + _pushValidatePath, getEncryptData(pushPayload));
196176

@@ -211,6 +191,14 @@ public PushResult sendPush(String payloadString) throws APIConnectionException,
211191
return BaseResult.fromResponse(response, PushResult.class);
212192
}
213193

194+
public PushResult sendFilePush(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
195+
checkPushPayload(pushPayload);
196+
197+
ResponseWrapper response = _httpClient.sendPost(_baseUrl + _filePushPath, getEncryptData(pushPayload));
198+
199+
return BaseResult.fromResponse(response, PushResult.class);
200+
}
201+
214202
public PushResult sendPushValidate(String payloadString) throws APIConnectionException, APIRequestException {
215203
Preconditions.checkArgument(StringUtils.isNotEmpty(payloadString), "pushPayload should not be empty");
216204

@@ -358,6 +346,20 @@ private String getEncryptData(String pushPayload, Audience audience) {
358346
return pushPayload;
359347
}
360348

349+
private void checkPushPayload(PushPayload pushPayload) {
350+
Preconditions.checkArgument(!(null == pushPayload), "pushPayload should not be null");
351+
352+
if (_apnsProduction > 0) {
353+
pushPayload.resetOptionsApnsProduction(true);
354+
} else if (_apnsProduction == 0) {
355+
pushPayload.resetOptionsApnsProduction(false);
356+
}
357+
358+
if (_timeToLive >= 0) {
359+
pushPayload.resetOptionsTimeToLive(_timeToLive);
360+
}
361+
}
362+
361363
}
362364

363365

src/main/java/cn/jpush/api/push/model/audience/Audience.java

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ public class Audience implements PushModel {
1515
private static final String ALL = "all";
1616

1717
private final boolean all;
18+
private final boolean file;
1819
private final Set<AudienceTarget> targets;
1920

20-
private Audience(boolean all, Set<AudienceTarget> targets) {
21+
private Audience(boolean all, boolean file, Set<AudienceTarget> targets) {
2122
this.all = all;
23+
this.file = file;
2224
this.targets = targets;
2325
}
24-
26+
2527
public static Builder newBuilder() {
2628
return new Builder();
2729
}
@@ -99,7 +101,12 @@ public static Audience abTest(Collection<String> abTestIds) {
99101
AudienceTarget target = AudienceTarget.abTest(abTestIds);
100102
return newBuilder().addAudienceTarget(target).build();
101103
}
102-
104+
105+
public static Audience file(String fileId) {
106+
AudienceTarget target = AudienceTarget.file(fileId);
107+
return newBuilder().setFile(Boolean.TRUE).addAudienceTarget(target).build();
108+
}
109+
103110

104111
public boolean isAll() {
105112
return this.all;
@@ -109,9 +116,17 @@ public JsonElement toJSON() {
109116
if (all) {
110117
return new JsonPrimitive(ALL);
111118
}
112-
119+
113120
// if not all, there will be target be set.
114121
JsonObject json = new JsonObject();
122+
if (file) {
123+
for (AudienceTarget target : targets) {
124+
if (AudienceType.FILE == target.getAudienceType()) {
125+
json.add(target.getAudienceTypeValue(), target.toFileJSON());
126+
}
127+
}
128+
return json;
129+
}
115130
if (null != targets) {
116131
for (AudienceTarget target : targets) {
117132
json.add(target.getAudienceTypeValue(), target.toJSON());
@@ -126,44 +141,54 @@ public static Audience fromJsonElement(JsonElement jsonElement) {
126141
}
127142
boolean all = !jsonElement.isJsonObject();
128143
if (all) {
129-
return new Audience(all, null);
144+
return new Audience(true, false,null);
130145
}
146+
boolean file = false;
131147
JsonObject jsonObject = jsonElement.getAsJsonObject();
132148
Set<AudienceTarget> audienceTargetSet = new HashSet<>();
133149
for (AudienceType type : AudienceType.values()) {
134150
JsonArray jsonArray = jsonObject.getAsJsonArray(type.value());
135151
if (jsonArray == null) {
136152
continue;
137153
}
154+
if (AudienceType.FILE == type) {
155+
file = true;
156+
}
138157
audienceTargetSet.add(AudienceTarget.fromJsonElement(jsonArray, type));
139158
}
140-
return new Audience(all, audienceTargetSet);
159+
return new Audience(false, file, audienceTargetSet);
141160
}
142161

143162
public static class Builder {
144163
private boolean all = false;
164+
private boolean file = false;
145165
private Set<AudienceTarget> audienceBuilder = null;
146-
166+
147167
public Builder setAll(boolean all) {
148168
this.all = all;
149169
return this;
150170
}
151-
171+
172+
public Builder setFile(boolean file) {
173+
this.file = file;
174+
return this;
175+
}
176+
152177
public Builder addAudienceTarget(AudienceTarget target) {
153178
if (null == audienceBuilder) {
154179
audienceBuilder = new HashSet<AudienceTarget>();
155180
}
156181
audienceBuilder.add(target);
157182
return this;
158183
}
159-
184+
160185
public Audience build() {
161186
Preconditions.checkArgument(! (all && null != audienceBuilder), "If audience is all, no any other audience may be set.");
162187
Preconditions.checkArgument(! (all == false && null == audienceBuilder), "No any audience target is set.");
163-
return new Audience(all, audienceBuilder);
188+
return new Audience(all, file, audienceBuilder);
164189
}
165190
}
166-
191+
167192
}
168193

169194

0 commit comments

Comments
 (0)