diff --git a/src/main/java/cn/jpush/api/JPushClient.java b/src/main/java/cn/jpush/api/JPushClient.java index 68b0f890..0974e573 100644 --- a/src/main/java/cn/jpush/api/JPushClient.java +++ b/src/main/java/cn/jpush/api/JPushClient.java @@ -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); @@ -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); @@ -75,6 +98,7 @@ 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. @@ -82,23 +106,25 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr * @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. @@ -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); } @@ -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. @@ -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); @@ -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); diff --git a/src/main/java/cn/jpush/api/common/ClientConfig.java b/src/main/java/cn/jpush/api/common/ClientConfig.java index 7924d390..76517e45 100644 --- a/src/main/java/cn/jpush/api/common/ClientConfig.java +++ b/src/main/java/cn/jpush/api/common/ClientConfig.java @@ -60,10 +60,32 @@ public class ClientConfig extends HashMap { 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"); @@ -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() { @@ -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); + } } diff --git a/src/main/java/cn/jpush/api/device/DeviceClient.java b/src/main/java/cn/jpush/api/device/DeviceClient.java index 277da7a0..d1f21747 100644 --- a/src/main/java/cn/jpush/api/device/DeviceClient.java +++ b/src/main/java/cn/jpush/api/device/DeviceClient.java @@ -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(); diff --git a/src/main/java/cn/jpush/api/push/PushClient.java b/src/main/java/cn/jpush/api/push/PushClient.java index 9d88e4da..496b0ca1 100644 --- a/src/main/java/cn/jpush/api/push/PushClient.java +++ b/src/main/java/cn/jpush/api/push/PushClient.java @@ -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. @@ -51,6 +49,9 @@ 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); @@ -58,7 +59,8 @@ public PushClient(String masterSecret, String appKey, int maxRetryTimes) { /** * 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 @@ -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); } @@ -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); @@ -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) { @@ -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); @@ -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()); @@ -172,7 +204,6 @@ public PushResult sendPushValidate(String payloadString) throws APIConnectionExc return BaseResult.fromResponse(response, PushResult.class); } - } diff --git a/src/main/java/cn/jpush/api/report/ReportClient.java b/src/main/java/cn/jpush/api/report/ReportClient.java index 7d3c03a3..1ac997c3 100644 --- a/src/main/java/cn/jpush/api/report/ReportClient.java +++ b/src/main/java/cn/jpush/api/report/ReportClient.java @@ -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); diff --git a/src/main/java/cn/jpush/api/schedule/ScheduleClient.java b/src/main/java/cn/jpush/api/schedule/ScheduleClient.java index d5b72874..fd492b14 100644 --- a/src/main/java/cn/jpush/api/schedule/ScheduleClient.java +++ b/src/main/java/cn/jpush/api/schedule/ScheduleClient.java @@ -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);