Skip to content

Commit 1e84ea9

Browse files
committed
Merge pull request #45 from Liuchy1/master
modify apns production and time to live setting
2 parents 58852d7 + 74fc1d0 commit 1e84ea9

File tree

6 files changed

+162
-24
lines changed

6 files changed

+162
-24
lines changed

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

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,25 @@ public JPushClient(String masterSecret, String appKey) {
5555
_scheduleClient = new ScheduleClient(masterSecret, appKey);
5656
}
5757

58+
/**
59+
* Create a JPush Client by custom Client configuration.
60+
*
61+
* @param masterSecret API access secret of the appKey.
62+
* @param appKey The KEY of one application on JPush.
63+
* @param proxy The proxy, if there is no proxy, should be null.
64+
* @param conf The client configuration. Can use ClientConfig.getInstance() as default.
65+
*/
66+
public JPushClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) {
67+
_pushClient = new PushClient(masterSecret, appKey, proxy, conf);
68+
_reportClient = new ReportClient(masterSecret, appKey, proxy, conf);
69+
_deviceClient = new DeviceClient(masterSecret, appKey, proxy, conf);
70+
_scheduleClient = new ScheduleClient(masterSecret, appKey, proxy, conf);
71+
}
72+
73+
/**
74+
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
75+
*
76+
*/
5877
@Deprecated
5978
public JPushClient(String masterSecret, String appKey, int maxRetryTimes) {
6079
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes);
@@ -63,6 +82,10 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes) {
6382
_scheduleClient = new ScheduleClient(masterSecret, appKey, maxRetryTimes);
6483
}
6584

85+
/**
86+
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
87+
*
88+
*/
6689
@Deprecated
6790
public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
6891
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy);
@@ -75,30 +98,33 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr
7598
* Create a JPush Client by custom Client configuration.
7699
*
77100
* If you are using JPush privacy cloud, maybe this constructor is what you needed.
101+
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
78102
*
79103
* @param masterSecret API access secret of the appKey.
80104
* @param appKey The KEY of one application on JPush.
81105
* @param maxRetryTimes Client request retry times.
82106
* @param proxy The proxy, if there is no proxy, should be null.
83107
* @param conf The client configuration. Can use ClientConfig.getInstance() as default.
84108
*/
109+
@Deprecated
85110
public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf) {
86-
this(masterSecret, appKey, proxy, conf);
87111
conf.setMaxRetryTimes(maxRetryTimes);
88-
}
89112

90-
public JPushClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) {
91113
_pushClient = new PushClient(masterSecret, appKey, proxy, conf);
92114
_reportClient = new ReportClient(masterSecret, appKey, proxy, conf);
93115
_deviceClient = new DeviceClient(masterSecret, appKey, proxy, conf);
94116
_scheduleClient = new ScheduleClient(masterSecret, appKey, proxy, conf);
117+
95118
}
96119

120+
121+
97122
/**
98123
* Create a JPush Client by custom Client configuration with global settings.
99124
*
100125
* If you are using JPush privacy cloud, and you want different settings from default globally,
101126
* maybe this constructor is what you needed.
127+
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setGolbalPushSetting} instead of this constructor.
102128
*
103129
* @param masterSecret API access secret of the appKey.
104130
* @param appKey The KEY of one application on JPush.
@@ -111,8 +137,12 @@ public JPushClient(String masterSecret, String appKey, HttpProxy proxy, ClientCo
111137
@Deprecated
112138
public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf,
113139
boolean apnsProduction, long timeToLive) {
114-
this(masterSecret, appKey, proxy, conf, apnsProduction, timeToLive);
115140
conf.setMaxRetryTimes(maxRetryTimes);
141+
_pushClient = new PushClient(masterSecret, appKey, proxy, conf);
142+
_reportClient = new ReportClient(masterSecret, appKey, proxy, conf);
143+
_deviceClient = new DeviceClient(masterSecret, appKey, proxy, conf);
144+
_scheduleClient = new ScheduleClient(masterSecret, appKey, proxy, conf);
145+
_pushClient.setDefaults(apnsProduction, timeToLive);
116146

117147
}
118148

@@ -121,6 +151,7 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr
121151
*
122152
* If you are using JPush privacy cloud, and you want different settings from default globally,
123153
* maybe this constructor is what you needed.
154+
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setGolbalPushSetting} instead of this constructor.
124155
*
125156
* @param masterSecret API access secret of the appKey.
126157
* @param appKey The KEY of one application on JPush.
@@ -129,6 +160,7 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr
129160
* @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
130161
* @param timeToLive Global time_to_live setting. It will override PushPayload Options.
131162
*/
163+
@Deprecated
132164
public JPushClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf,
133165
boolean apnsProduction, long timeToLive) {
134166
_pushClient = new PushClient(masterSecret, appKey, proxy, conf);
@@ -142,12 +174,14 @@ public JPushClient(String masterSecret, String appKey, HttpProxy proxy, ClientCo
142174
* Create a JPush Client with global settings.
143175
*
144176
* If you want different settings from default globally, this constructor is what you needed.
145-
*
177+
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setGolbalPushSetting} instead of this constructor.
178+
*
146179
* @param masterSecret API access secret of the appKey.
147180
* @param appKey The KEY of one application on JPush.
148181
* @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
149182
* @param timeToLive Global time_to_live setting. It will override PushPayload Options.
150183
*/
184+
@Deprecated
151185
public JPushClient(String masterSecret, String appKey, boolean apnsProduction, long timeToLive) {
152186
_pushClient = new PushClient(masterSecret, appKey, apnsProduction, timeToLive);
153187
_reportClient = new ReportClient(masterSecret, appKey);

src/main/java/cn/jpush/api/common/ClientConfig.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,32 @@ public class ClientConfig extends HashMap<String, Object> {
6060
public static final Object CONNECTION_TIMEOUT_SCHEMA = Integer.class;
6161
public static final int DEFAULT_CONNECTION_TIMEOUT = 5 * 1000;
6262

63+
/**
64+
* Global APNs environment setting.
65+
* Setting to -1, if you want to use PushPayload Options{@link cn.jpush.api.push.model.Options#apnsProduction}.
66+
* Default value is -1.
67+
* Setting to 0, if you want to use global setting as development environment.
68+
* Setting to 1, if you want to use global setting as production environment.
69+
*
70+
*/
71+
public static final String APNS_PRODUCTION = "apns.production";
72+
public static final Object APNS_PRODUCTION_SCHEMA = Integer.class;
73+
public static final int DEFAULT_APNS_PRODUCTION = -1;
74+
75+
/**
76+
* Global time_to_live setting. Time unit is second.
77+
* Setting to -1, if you want to use PushPayload Options{@link cn.jpush.api.push.model.Options#timeToLive}.
78+
* Default value is -1.
79+
* It will override PushPayload Options, while it is a positive integer value.
80+
*/
81+
public static final String TIME_TO_LIVE = "time.to.live";
82+
public static final Object TIME_TO_LIVE_SCHEMA = Long.class;
83+
public static final long DEFAULT_TIME_TO_LIVE = -1;
84+
6385
private static ClientConfig instance = new ClientConfig();
6486

6587
private ClientConfig() {
66-
super(20);
88+
super(32);
6789
this.put(DEVICE_HOST_NAME, "https://device.jpush.cn");
6890
this.put(DEVICES_PATH, "/v3/devices");
6991
this.put(TAGS_PATH, "/v3/tags");
@@ -86,6 +108,9 @@ private ClientConfig() {
86108
this.put(READ_TIMEOUT, DEFAULT_READ_TIMEOUT);
87109
this.put(CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
88110

111+
this.put(APNS_PRODUCTION, DEFAULT_APNS_PRODUCTION);
112+
this.put(TIME_TO_LIVE, DEFAULT_TIME_TO_LIVE);
113+
89114
}
90115

91116
public static ClientConfig getInstance() {
@@ -184,4 +209,28 @@ public Integer getConnectionTimeout() {
184209
return (Integer) this.get(CONNECTION_TIMEOUT);
185210
}
186211

212+
public static void setApnsProduction(Map conf, boolean production) {
213+
if(production) {
214+
conf.put(APNS_PRODUCTION, 1);
215+
} else {
216+
conf.put(APNS_PRODUCTION, 0);
217+
}
218+
}
219+
220+
public void setApnsProduction(boolean production) {
221+
setApnsProduction(this, production);
222+
}
223+
224+
public static void setTimeToLive(Map conf, long timeToLive) {
225+
conf.put(TIME_TO_LIVE, timeToLive);
226+
}
227+
228+
public void setTimeToLive(long timeToLive) {
229+
setTimeToLive(this, timeToLive);
230+
}
231+
232+
public void setGolbalPushSetting(boolean apnsProduction, long timeToLive) {
233+
setApnsProduction(this, apnsProduction);
234+
setTimeToLive(timeToLive);
235+
}
187236
}

src/main/java/cn/jpush/api/device/DeviceClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,19 @@ public DeviceClient(String masterSecret, String appKey) {
2929
this(masterSecret, appKey, null, ClientConfig.getInstance());
3030
}
3131

32+
/**
33+
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
34+
*
35+
*/
3236
@Deprecated
3337
public DeviceClient(String masterSecret, String appKey, int maxRetryTimes) {
3438
this(masterSecret, appKey, maxRetryTimes, null);
3539
}
3640

41+
/**
42+
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
43+
*
44+
*/
3745
@Deprecated
3846
public DeviceClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
3947
ClientConfig conf = ClientConfig.getInstance();

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

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ public class PushClient {
3434
private JsonParser _jsonParser = new JsonParser();
3535

3636
// If not present, true by default.
37-
private boolean _apnsProduction = true;
37+
private int _apnsProduction;
3838

3939
// If not present, the default value is 86400(s) (one day)
40-
private long _timeToLive = 60 * 60 * 24;
41-
42-
private boolean _globalSettingEnabled = false;
40+
private long _timeToLive;
4341

4442
/**
4543
* Create a Push Client.
@@ -51,14 +49,18 @@ public PushClient(String masterSecret, String appKey) {
5149
this(masterSecret, appKey, null, ClientConfig.getInstance());
5250
}
5351

52+
/**
53+
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig} instead of this constructor.
54+
*/
5455
@Deprecated
5556
public PushClient(String masterSecret, String appKey, int maxRetryTimes) {
5657
this(masterSecret, appKey, maxRetryTimes, null);
5758
}
5859

5960
/**
6061
* Create a Push Client with max retry times.
61-
*
62+
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig} instead of this constructor.
63+
*
6264
* @param masterSecret API access secret of the appKey.
6365
* @param appKey The KEY of one application on JPush.
6466
* @param maxRetryTimes max retry times
@@ -74,6 +76,9 @@ public PushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPro
7476
this._pushPath = (String) conf.get(ClientConfig.PUSH_PATH);
7577
this._pushValidatePath = (String) conf.get(ClientConfig.PUSH_VALIDATE_PATH);
7678

79+
this._apnsProduction = (Integer) conf.get(ClientConfig.APNS_PRODUCTION);
80+
this._timeToLive = (Long) conf.get(ClientConfig.TIME_TO_LIVE);
81+
7782
String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
7883
this._httpClient = new NativeHttpClient(authCode, proxy, conf);
7984
}
@@ -85,6 +90,9 @@ public PushClient(String masterSecret, String appKey, HttpProxy proxy, ClientCon
8590
this._pushPath = (String) conf.get(ClientConfig.PUSH_PATH);
8691
this._pushValidatePath = (String) conf.get(ClientConfig.PUSH_VALIDATE_PATH);
8792

93+
this._apnsProduction = (Integer) conf.get(ClientConfig.APNS_PRODUCTION);
94+
this._timeToLive = (Long) conf.get(ClientConfig.TIME_TO_LIVE);
95+
8896
String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
8997
this._httpClient = new NativeHttpClient(authCode, proxy, conf);
9098

@@ -94,24 +102,38 @@ public PushClient(String masterSecret, String appKey, HttpProxy proxy, ClientCon
94102
* Create a Push Client with global settings.
95103
*
96104
* If you want different settings from default globally, this constructor is what you needed.
97-
*
105+
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig} instead of this constructor.
106+
*
98107
* @param masterSecret API access secret of the appKey.
99108
* @param appKey The KEY of one application on JPush.
100109
* @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
101110
* @param timeToLive Global time_to_live setting. It will override PushPayload Options.
102111
*/
112+
@Deprecated
103113
public PushClient(String masterSecret, String appKey, boolean apnsProduction, long timeToLive) {
104114
this(masterSecret, appKey);
105-
106-
this._apnsProduction = apnsProduction;
115+
if(apnsProduction) {
116+
_apnsProduction = 1;
117+
} else {
118+
_apnsProduction = 0;
119+
}
107120
this._timeToLive = timeToLive;
108-
this._globalSettingEnabled = true;
109121
}
110-
122+
123+
/**
124+
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setGolbalPushSetting} instead of this method.
125+
*
126+
* @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
127+
* @param timeToLive Global time_to_live setting. It will override PushPayload Options.
128+
*/
129+
@Deprecated
111130
public void setDefaults(boolean apnsProduction, long timeToLive) {
112-
this._apnsProduction = apnsProduction;
131+
if(apnsProduction) {
132+
_apnsProduction = 1;
133+
} else {
134+
_apnsProduction = 0;
135+
}
113136
this._timeToLive = timeToLive;
114-
this._globalSettingEnabled = true;
115137
}
116138

117139
public void setBaseUrl(String baseUrl) {
@@ -121,11 +143,16 @@ public void setBaseUrl(String baseUrl) {
121143
public PushResult sendPush(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
122144
Preconditions.checkArgument(! (null == pushPayload), "pushPayload should not be null");
123145

124-
if (_globalSettingEnabled) {
146+
if (_apnsProduction > 0) {
147+
pushPayload.resetOptionsApnsProduction(true);
148+
} else if(_apnsProduction == 0) {
149+
pushPayload.resetOptionsApnsProduction(false);
150+
}
151+
152+
if (_timeToLive >= 0) {
125153
pushPayload.resetOptionsTimeToLive(_timeToLive);
126-
pushPayload.resetOptionsApnsProduction(_apnsProduction);
127154
}
128-
155+
129156
ResponseWrapper response = _httpClient.sendPost(_baseUrl + _pushPath, pushPayload.toString());
130157

131158
return BaseResult.fromResponse(response, PushResult.class);
@@ -134,9 +161,14 @@ public PushResult sendPush(PushPayload pushPayload) throws APIConnectionExceptio
134161
public PushResult sendPushValidate(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
135162
Preconditions.checkArgument(! (null == pushPayload), "pushPayload should not be null");
136163

137-
if (_globalSettingEnabled) {
164+
if (_apnsProduction > 0) {
165+
pushPayload.resetOptionsApnsProduction(true);
166+
} else if(_apnsProduction == 0) {
167+
pushPayload.resetOptionsApnsProduction(false);
168+
}
169+
170+
if (_timeToLive >= 0) {
138171
pushPayload.resetOptionsTimeToLive(_timeToLive);
139-
pushPayload.resetOptionsApnsProduction(_apnsProduction);
140172
}
141173

142174
ResponseWrapper response = _httpClient.sendPost(_baseUrl + _pushValidatePath, pushPayload.toString());
@@ -172,7 +204,6 @@ public PushResult sendPushValidate(String payloadString) throws APIConnectionExc
172204
return BaseResult.fromResponse(response, PushResult.class);
173205
}
174206

175-
176207
}
177208

178209

src/main/java/cn/jpush/api/report/ReportClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@ public ReportClient(String masterSecret, String appKey) {
2626
this(masterSecret, appKey, null, ClientConfig.getInstance());
2727
}
2828

29+
/**
30+
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
31+
*
32+
*/
2933
@Deprecated
3034
public ReportClient(String masterSecret, String appKey, int maxRetryTimes) {
3135
this(masterSecret, appKey, maxRetryTimes, null);
3236
}
3337

38+
/**
39+
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
40+
*
41+
*/
3442
@Deprecated
3543
public ReportClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
3644
ServiceHelper.checkBasic(appKey, masterSecret);

src/main/java/cn/jpush/api/schedule/ScheduleClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,19 @@ public ScheduleClient(String masterSecret, String appkey) {
2525
this(masterSecret, appkey, null, ClientConfig.getInstance());
2626
}
2727

28+
/**
29+
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
30+
*
31+
*/
2832
@Deprecated
2933
public ScheduleClient(String masterSecret, String appKey, int maxRetryTimes) {
3034
this(masterSecret, appKey, maxRetryTimes, null);
3135
}
3236

37+
/**
38+
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
39+
*
40+
*/
3341
@Deprecated
3442
public ScheduleClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
3543
ServiceHelper.checkBasic(appKey, masterSecret);

0 commit comments

Comments
 (0)