|
| 1 | +package cn.jpush.api.im; |
| 2 | + |
| 3 | +import cn.jpush.api.common.ServiceHelper; |
| 4 | +import cn.jpush.api.common.connection.HttpProxy; |
| 5 | +import cn.jpush.api.common.connection.IHttpClient; |
| 6 | +import cn.jpush.api.common.connection.NativeHttpClient; |
| 7 | +import cn.jpush.api.common.resp.APIConnectionException; |
| 8 | +import cn.jpush.api.common.resp.APIRequestException; |
| 9 | +import cn.jpush.api.common.resp.BaseResult; |
| 10 | +import cn.jpush.api.common.resp.ResponseWrapper; |
| 11 | +import cn.jpush.api.push.model.PushPayload; |
| 12 | +import cn.jpush.api.utils.Preconditions; |
| 13 | +import cn.jpush.api.utils.StringUtils; |
| 14 | + |
| 15 | +import com.google.gson.JsonParseException; |
| 16 | +import com.google.gson.JsonParser; |
| 17 | + |
| 18 | +/** |
| 19 | + * Entrance for sending Push. |
| 20 | + * |
| 21 | + * For the following parameters, you can set them by instance creation. |
| 22 | + * This action will override setting in PushPayload Optional. |
| 23 | + * * apnsProduction If not present, the default is true. |
| 24 | + * * timeToLive If not present, the default is 86400(s) (one day). |
| 25 | + * |
| 26 | + * Can be used directly. |
| 27 | + */ |
| 28 | +public class IMClient { |
| 29 | + public static final String HOST_NAME_SSL = "https://api.jpush.cn"; |
| 30 | + public static final String PUSH_PATH = "/v3/push"; |
| 31 | + public static final String PUSH_VALIDATE_PATH = "/v3/push/validate"; |
| 32 | + |
| 33 | + private final NativeHttpClient _httpClient; |
| 34 | + private JsonParser _jsonParser = new JsonParser(); |
| 35 | + |
| 36 | + // If not present, true by default. |
| 37 | + private boolean _apnsProduction = true; |
| 38 | + |
| 39 | + // If not present, the default value is 86400(s) (one day) |
| 40 | + private long _timeToLive = 60 * 60 * 24; |
| 41 | + |
| 42 | + private boolean _globalSettingEnabled = false; |
| 43 | + |
| 44 | + private String _baseUrl; |
| 45 | + |
| 46 | + /** |
| 47 | + * Create a Push Client. |
| 48 | + * |
| 49 | + * @param masterSecret API access secret of the appKey. |
| 50 | + * @param appKey The KEY of one application on JPush. |
| 51 | + */ |
| 52 | + public IMClient(String masterSecret, String appKey) { |
| 53 | + this(masterSecret, appKey, IHttpClient.DEFAULT_MAX_RETRY_TIMES); |
| 54 | + } |
| 55 | + |
| 56 | + public IMClient(String masterSecret, String appKey, int maxRetryTimes) { |
| 57 | + this(masterSecret, appKey, maxRetryTimes, null); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Create a Push Client with max retry times. |
| 62 | + * |
| 63 | + * @param masterSecret API access secret of the appKey. |
| 64 | + * @param appKey The KEY of one application on JPush. |
| 65 | + * @param maxRetryTimes max retry times |
| 66 | + */ |
| 67 | + public IMClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) { |
| 68 | + ServiceHelper.checkBasic(appKey, masterSecret); |
| 69 | + |
| 70 | + String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret); |
| 71 | + this._baseUrl = HOST_NAME_SSL; |
| 72 | + this._httpClient = new NativeHttpClient(authCode, maxRetryTimes, proxy); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Create a Push Client with global settings. |
| 77 | + * |
| 78 | + * If you want different settings from default globally, this constructor is what you needed. |
| 79 | + * |
| 80 | + * @param masterSecret API access secret of the appKey. |
| 81 | + * @param appKey The KEY of one application on JPush. |
| 82 | + * @param apnsProduction Global APNs environment setting. It will override PushPayload Options. |
| 83 | + * @param timeToLive Global time_to_live setting. It will override PushPayload Options. |
| 84 | + */ |
| 85 | + public IMClient(String masterSecret, String appKey, boolean apnsProduction, long timeToLive) { |
| 86 | + this(masterSecret, appKey); |
| 87 | + |
| 88 | + this._apnsProduction = apnsProduction; |
| 89 | + this._timeToLive = timeToLive; |
| 90 | + this._globalSettingEnabled = true; |
| 91 | + } |
| 92 | + |
| 93 | + public void setDefaults(boolean apnsProduction, long timeToLive) { |
| 94 | + this._apnsProduction = apnsProduction; |
| 95 | + this._timeToLive = timeToLive; |
| 96 | + this._globalSettingEnabled = true; |
| 97 | + } |
| 98 | + |
| 99 | + public void setBaseUrl(String baseUrl) { |
| 100 | + this._baseUrl = baseUrl; |
| 101 | + } |
| 102 | + |
| 103 | + public IMResult sendPush(PushPayload pushPayload) throws APIConnectionException, APIRequestException { |
| 104 | + Preconditions.checkArgument(! (null == pushPayload), "pushPayload should not be null"); |
| 105 | + |
| 106 | + if (_globalSettingEnabled) { |
| 107 | + pushPayload.resetOptionsTimeToLive(_timeToLive); |
| 108 | + pushPayload.resetOptionsApnsProduction(_apnsProduction); |
| 109 | + } |
| 110 | + |
| 111 | + ResponseWrapper response = _httpClient.sendPost(_baseUrl + PUSH_PATH, pushPayload.toString()); |
| 112 | + |
| 113 | + return BaseResult.fromResponse(response, IMResult.class); |
| 114 | + } |
| 115 | + |
| 116 | + public IMResult sendPushValidate(PushPayload pushPayload) throws APIConnectionException, APIRequestException { |
| 117 | + Preconditions.checkArgument(! (null == pushPayload), "pushPayload should not be null"); |
| 118 | + |
| 119 | + if (_globalSettingEnabled) { |
| 120 | + pushPayload.resetOptionsTimeToLive(_timeToLive); |
| 121 | + pushPayload.resetOptionsApnsProduction(_apnsProduction); |
| 122 | + } |
| 123 | + |
| 124 | + ResponseWrapper response = _httpClient.sendPost(_baseUrl + PUSH_VALIDATE_PATH, pushPayload.toString()); |
| 125 | + |
| 126 | + return BaseResult.fromResponse(response, IMResult.class); |
| 127 | + } |
| 128 | + |
| 129 | + public IMResult sendPush(String payloadString) throws APIConnectionException, APIRequestException { |
| 130 | + Preconditions.checkArgument(StringUtils.isNotEmpty(payloadString), "pushPayload should not be empty"); |
| 131 | + |
| 132 | + try { |
| 133 | + _jsonParser.parse(payloadString); |
| 134 | + } catch (JsonParseException e) { |
| 135 | + Preconditions.checkArgument(false, "payloadString should be a valid JSON string."); |
| 136 | + } |
| 137 | + |
| 138 | + ResponseWrapper response = _httpClient.sendPost(_baseUrl + PUSH_PATH, payloadString); |
| 139 | + |
| 140 | + return BaseResult.fromResponse(response, IMResult.class); |
| 141 | + } |
| 142 | + |
| 143 | + public IMResult sendPushValidate(String payloadString) throws APIConnectionException, APIRequestException { |
| 144 | + Preconditions.checkArgument(StringUtils.isNotEmpty(payloadString), "pushPayload should not be empty"); |
| 145 | + |
| 146 | + try { |
| 147 | + _jsonParser.parse(payloadString); |
| 148 | + } catch (JsonParseException e) { |
| 149 | + Preconditions.checkArgument(false, "payloadString should be a valid JSON string."); |
| 150 | + } |
| 151 | + |
| 152 | + ResponseWrapper response = _httpClient.sendPost(_baseUrl + PUSH_VALIDATE_PATH, payloadString); |
| 153 | + |
| 154 | + return BaseResult.fromResponse(response, IMResult.class); |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | +} |
| 159 | + |
| 160 | + |
0 commit comments