Skip to content
Merged
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
44 changes: 39 additions & 5 deletions src/main/java/cn/jpush/api/JPushClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ public JPushClient(String masterSecret, String appKey) {
_scheduleClient = new ScheduleClient(masterSecret, appKey);
}

/**
* Create a JPush Client by custom Client configuration.
*
* @param masterSecret API access secret of the appKey.
* @param appKey The KEY of one application on JPush.
* @param proxy The proxy, if there is no proxy, should be null.
* @param conf The client configuration. Can use ClientConfig.getInstance() as default.
*/
public JPushClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) {
_pushClient = new PushClient(masterSecret, appKey, proxy, conf);
_reportClient = new ReportClient(masterSecret, appKey, proxy, conf);
_deviceClient = new DeviceClient(masterSecret, appKey, proxy, conf);
_scheduleClient = new ScheduleClient(masterSecret, appKey, proxy, conf);
}

/**
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
*
*/
@Deprecated
public JPushClient(String masterSecret, String appKey, int maxRetryTimes) {
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes);
Expand All @@ -63,6 +82,10 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes) {
_scheduleClient = new ScheduleClient(masterSecret, appKey, maxRetryTimes);
}

/**
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
*
*/
@Deprecated
public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy);
Expand All @@ -75,30 +98,33 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr
* Create a JPush Client by custom Client configuration.
*
* If you are using JPush privacy cloud, maybe this constructor is what you needed.
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
*
* @param masterSecret API access secret of the appKey.
* @param appKey The KEY of one application on JPush.
* @param maxRetryTimes Client request retry times.
* @param proxy The proxy, if there is no proxy, should be null.
* @param conf The client configuration. Can use ClientConfig.getInstance() as default.
*/
@Deprecated
public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf) {
this(masterSecret, appKey, proxy, conf);
conf.setMaxRetryTimes(maxRetryTimes);
}

public JPushClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) {
_pushClient = new PushClient(masterSecret, appKey, proxy, conf);
_reportClient = new ReportClient(masterSecret, appKey, proxy, conf);
_deviceClient = new DeviceClient(masterSecret, appKey, proxy, conf);
_scheduleClient = new ScheduleClient(masterSecret, appKey, proxy, conf);

}



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

}

Expand All @@ -121,6 +151,7 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr
*
* If you are using JPush privacy cloud, and you want different settings from default globally,
* maybe this constructor is what you needed.
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setGolbalPushSetting} instead of this constructor.
*
* @param masterSecret API access secret of the appKey.
* @param appKey The KEY of one application on JPush.
Expand All @@ -129,6 +160,7 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr
* @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
* @param timeToLive Global time_to_live setting. It will override PushPayload Options.
*/
@Deprecated
public JPushClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf,
boolean apnsProduction, long timeToLive) {
_pushClient = new PushClient(masterSecret, appKey, proxy, conf);
Expand All @@ -142,12 +174,14 @@ public JPushClient(String masterSecret, String appKey, HttpProxy proxy, ClientCo
* Create a JPush Client with global settings.
*
* If you want different settings from default globally, this constructor is what you needed.
*
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setGolbalPushSetting} instead of this constructor.
*
* @param masterSecret API access secret of the appKey.
* @param appKey The KEY of one application on JPush.
* @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
* @param timeToLive Global time_to_live setting. It will override PushPayload Options.
*/
@Deprecated
public JPushClient(String masterSecret, String appKey, boolean apnsProduction, long timeToLive) {
_pushClient = new PushClient(masterSecret, appKey, apnsProduction, timeToLive);
_reportClient = new ReportClient(masterSecret, appKey);
Expand Down
51 changes: 50 additions & 1 deletion src/main/java/cn/jpush/api/common/ClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,32 @@ public class ClientConfig extends HashMap<String, Object> {
public static final Object CONNECTION_TIMEOUT_SCHEMA = Integer.class;
public static final int DEFAULT_CONNECTION_TIMEOUT = 5 * 1000;

/**
* Global APNs environment setting.
* Setting to -1, if you want to use PushPayload Options{@link cn.jpush.api.push.model.Options#apnsProduction}.
* Default value is -1.
* Setting to 0, if you want to use global setting as development environment.
* Setting to 1, if you want to use global setting as production environment.
*
*/
public static final String APNS_PRODUCTION = "apns.production";
public static final Object APNS_PRODUCTION_SCHEMA = Integer.class;
public static final int DEFAULT_APNS_PRODUCTION = -1;

/**
* Global time_to_live setting. Time unit is second.
* Setting to -1, if you want to use PushPayload Options{@link cn.jpush.api.push.model.Options#timeToLive}.
* Default value is -1.
* It will override PushPayload Options, while it is a positive integer value.
*/
public static final String TIME_TO_LIVE = "time.to.live";
public static final Object TIME_TO_LIVE_SCHEMA = Long.class;
public static final long DEFAULT_TIME_TO_LIVE = -1;

private static ClientConfig instance = new ClientConfig();

private ClientConfig() {
super(20);
super(32);
this.put(DEVICE_HOST_NAME, "https://device.jpush.cn");
this.put(DEVICES_PATH, "/v3/devices");
this.put(TAGS_PATH, "/v3/tags");
Expand All @@ -86,6 +108,9 @@ private ClientConfig() {
this.put(READ_TIMEOUT, DEFAULT_READ_TIMEOUT);
this.put(CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);

this.put(APNS_PRODUCTION, DEFAULT_APNS_PRODUCTION);
this.put(TIME_TO_LIVE, DEFAULT_TIME_TO_LIVE);

}

public static ClientConfig getInstance() {
Expand Down Expand Up @@ -184,4 +209,28 @@ public Integer getConnectionTimeout() {
return (Integer) this.get(CONNECTION_TIMEOUT);
}

public static void setApnsProduction(Map conf, boolean production) {
if(production) {
conf.put(APNS_PRODUCTION, 1);
} else {
conf.put(APNS_PRODUCTION, 0);
}
}

public void setApnsProduction(boolean production) {
setApnsProduction(this, production);
}

public static void setTimeToLive(Map conf, long timeToLive) {
conf.put(TIME_TO_LIVE, timeToLive);
}

public void setTimeToLive(long timeToLive) {
setTimeToLive(this, timeToLive);
}

public void setGolbalPushSetting(boolean apnsProduction, long timeToLive) {
setApnsProduction(this, apnsProduction);
setTimeToLive(timeToLive);
}
}
8 changes: 8 additions & 0 deletions src/main/java/cn/jpush/api/device/DeviceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,19 @@ public DeviceClient(String masterSecret, String appKey) {
this(masterSecret, appKey, null, ClientConfig.getInstance());
}

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

/**
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
*
*/
@Deprecated
public DeviceClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
ClientConfig conf = ClientConfig.getInstance();
Expand Down
67 changes: 49 additions & 18 deletions src/main/java/cn/jpush/api/push/PushClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ public class PushClient {
private JsonParser _jsonParser = new JsonParser();

// If not present, true by default.
private boolean _apnsProduction = true;
private int _apnsProduction;

// If not present, the default value is 86400(s) (one day)
private long _timeToLive = 60 * 60 * 24;

private boolean _globalSettingEnabled = false;
private long _timeToLive;

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

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

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

this._apnsProduction = (Integer) conf.get(ClientConfig.APNS_PRODUCTION);
this._timeToLive = (Long) conf.get(ClientConfig.TIME_TO_LIVE);

String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
this._httpClient = new NativeHttpClient(authCode, proxy, conf);
}
Expand All @@ -85,6 +90,9 @@ public PushClient(String masterSecret, String appKey, HttpProxy proxy, ClientCon
this._pushPath = (String) conf.get(ClientConfig.PUSH_PATH);
this._pushValidatePath = (String) conf.get(ClientConfig.PUSH_VALIDATE_PATH);

this._apnsProduction = (Integer) conf.get(ClientConfig.APNS_PRODUCTION);
this._timeToLive = (Long) conf.get(ClientConfig.TIME_TO_LIVE);

String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
this._httpClient = new NativeHttpClient(authCode, proxy, conf);

Expand All @@ -94,24 +102,38 @@ public PushClient(String masterSecret, String appKey, HttpProxy proxy, ClientCon
* Create a Push Client with global settings.
*
* If you want different settings from default globally, this constructor is what you needed.
*
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig} instead of this constructor.
*
* @param masterSecret API access secret of the appKey.
* @param appKey The KEY of one application on JPush.
* @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
* @param timeToLive Global time_to_live setting. It will override PushPayload Options.
*/
@Deprecated
public PushClient(String masterSecret, String appKey, boolean apnsProduction, long timeToLive) {
this(masterSecret, appKey);

this._apnsProduction = apnsProduction;
if(apnsProduction) {
_apnsProduction = 1;
} else {
_apnsProduction = 0;
}
this._timeToLive = timeToLive;
this._globalSettingEnabled = true;
}


/**
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setGolbalPushSetting} instead of this method.
*
* @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
* @param timeToLive Global time_to_live setting. It will override PushPayload Options.
*/
@Deprecated
public void setDefaults(boolean apnsProduction, long timeToLive) {
this._apnsProduction = apnsProduction;
if(apnsProduction) {
_apnsProduction = 1;
} else {
_apnsProduction = 0;
}
this._timeToLive = timeToLive;
this._globalSettingEnabled = true;
}

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

if (_globalSettingEnabled) {
if (_apnsProduction > 0) {
pushPayload.resetOptionsApnsProduction(true);
} else if(_apnsProduction == 0) {
pushPayload.resetOptionsApnsProduction(false);
}

if (_timeToLive >= 0) {
pushPayload.resetOptionsTimeToLive(_timeToLive);
pushPayload.resetOptionsApnsProduction(_apnsProduction);
}

ResponseWrapper response = _httpClient.sendPost(_baseUrl + _pushPath, pushPayload.toString());

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

if (_globalSettingEnabled) {
if (_apnsProduction > 0) {
pushPayload.resetOptionsApnsProduction(true);
} else if(_apnsProduction == 0) {
pushPayload.resetOptionsApnsProduction(false);
}

if (_timeToLive >= 0) {
pushPayload.resetOptionsTimeToLive(_timeToLive);
pushPayload.resetOptionsApnsProduction(_apnsProduction);
}

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


}


8 changes: 8 additions & 0 deletions src/main/java/cn/jpush/api/report/ReportClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ public ReportClient(String masterSecret, String appKey) {
this(masterSecret, appKey, null, ClientConfig.getInstance());
}

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

/**
* This will be removed in the future. Please use ClientConfig{@link cn.jpush.api.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
*
*/
@Deprecated
public ReportClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
ServiceHelper.checkBasic(appKey, masterSecret);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/cn/jpush/api/schedule/ScheduleClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ public ScheduleClient(String masterSecret, String appkey) {
this(masterSecret, appkey, null, ClientConfig.getInstance());
}

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

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