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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.2.5</version>
<version>3.2.6</version>
</dependency>
```
### jar 包方式
Expand Down
36 changes: 31 additions & 5 deletions example/main/java/cn/jpush/api/examples/PushExample.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.jpush.api.examples;

import cn.jpush.api.common.ClientConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -21,8 +22,8 @@ public class PushExample {
protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);

// demo App defined in resources/jpush-api.conf
private static final String appKey ="dd1066407b044738b6479275";
private static final String masterSecret = "2b38ce69b1de2a7fa95706ea";
private static final String appKey ="e5c0d34f58732cf09b2d4d74";
private static final String masterSecret = "4cdda6d3c8b029941dbc5cb3";

public static final String TITLE = "Test from API example";
public static final String ALERT = "Test from API Example - alert";
Expand All @@ -31,7 +32,7 @@ public class PushExample {
public static final String TAG = "tag_api";

public static void main(String[] args) {
testSendPush();
testSendPushWithCustomConfig();
}


Expand Down Expand Up @@ -126,7 +127,32 @@ public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
.build())
.build();
}



public static void testSendPushWithCustomConfig() {
ClientConfig config = ClientConfig.getInstance();
// Setup the custom hostname
config.setPushHostName("https://api.jpush.cn");

JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3, null, config);

// For push, all you need do is to build PushPayload object.
PushPayload payload = buildPushObject_all_all_alert();

try {
PushResult result = jpushClient.sendPush(payload);
LOG.info("Got result - " + result);

} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);

} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
LOG.info("Msg ID: " + e.getMsgId());
}
}

}

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.2.5-SNAPSHOT</version>
<version>3.2.6-SNAPSHOT</version>
<packaging>jar</packaging>
<url>https://github.com/jpush/jpush-api-java-client</url>
<name>JPush API Java Client</name>
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/cn/jpush/api/JPushClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Map;
import java.util.Set;

import cn.jpush.api.common.ClientConfig;
import cn.jpush.api.common.TimeUnit;
import cn.jpush.api.common.connection.HttpProxy;
import cn.jpush.api.common.resp.APIConnectionException;
Expand Down Expand Up @@ -56,6 +57,45 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy);
_deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes, proxy);
}

/**
* Create a JPush Client by custom Client configuration.
*
* If you are using JPush privacy cloud, maybe this constructor is what you needed.
*
* @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.
*/
public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf) {
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
_deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes, 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.
*
* @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.
* @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
* @param timeToLive Global time_to_live setting. It will override PushPayload Options.
*/
public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf,
boolean apnsProduction, long timeToLive) {
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
_deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
_pushClient.setDefaults(apnsProduction, timeToLive);
}

/**
* Create a JPush Client with global settings.
Expand Down
101 changes: 101 additions & 0 deletions src/main/java/cn/jpush/api/common/ClientConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package cn.jpush.api.common;


import java.util.HashMap;
import java.util.Map;

public class ClientConfig extends HashMap<String, Object> {

private static ClientConfig instance = new ClientConfig();

public static final String DEVICE_HOST_NAME = "device.host.name";
public static final Object DEVICE_HOST_NAME_SCHEMA = String.class;

public static final String DEVICES_PATH = "devices.path";
public static final Object DEVICES_PATH_SCHEMA = String.class;

public static final String TAGS_PATH = "tags.path";
public static final Object TAGS_PATH_SCHEMA = String.class;

public static final String ALIASES_PATH = "aliases.path";
public static final Object ALIASES_PATH_SCHEMA = String.class;

public static final String PUSH_HOST_NAME = "push.host.name";
public static final Object PUSH_HOST_NAME_SCHEMA = String.class;

public static final String PUSH_PATH = "push.path";
public static final Object PUSH_PATH_SCHEMA = String.class;

public static final String PUSH_VALIDATE_PATH = "push.validate.path";
public static final Object PUSH_VALIDATE_PATH_SCHMEA = String.class;

public static final String REPORT_HOST_NAME = "report.host.name";
public static final Object REPORT_HOST_NAME_SCHEMA = String.class;

public static final String REPORT_RECEIVE_PATH = "report.receive.path";
public static final Object REPORT_RECEIVE_PATH_SCHEMA = String.class;

public static final String REPORT_USER_PATH = "report.user.path";
public static final Object REPORT_USER_PATH_SCHEMA = String.class;

public static final String REPORT_MESSAGE_PATH = "report.message.path";
public static final Object REPORT_MESSAGE_PATH_SCHEMA = String.class;

private ClientConfig() {
super();
this.put(DEVICE_HOST_NAME, "https://device.jpush.cn");
this.put(DEVICES_PATH, "/v3/devices");
this.put(TAGS_PATH, "/v3/tags");
this.put(ALIASES_PATH, "/v3/aliases");

this.put(PUSH_HOST_NAME, "https://api.jpush.cn");
this.put(PUSH_PATH, "/v3/push");
this.put(PUSH_VALIDATE_PATH, "/v3/push/validate");

this.put(REPORT_HOST_NAME, "https://report.jpush.cn");
this.put(REPORT_RECEIVE_PATH, "/v3/received");
this.put(REPORT_USER_PATH, "/v3/users");
this.put(REPORT_MESSAGE_PATH, "/v3/messages");
}

public static ClientConfig getInstance() {
return instance;
}

public static void setDeviceHostName(Map conf, String hostName) {
conf.put(DEVICE_HOST_NAME, hostName);
}

/**
* Setup custom device api host name, if using the JPush privacy cloud.
* @param hostName the custom api host name, default is JPush domain name
*/
public void setDeviceHostName(String hostName) {
setDeviceHostName(this, hostName);
}

public static void setPushHostName(Map conf, String hostName) {
conf.put(PUSH_HOST_NAME, hostName);
}

/**
* Setup custom push api host name, if using the JPush privacy cloud.
* @param hostName the custom api host name, default is JPush domain name
*/
public void setPushHostName(String hostName) {
setPushHostName(this, hostName);
}

public static void setReportHostName(Map conf, String hostName) {
conf.put(REPORT_HOST_NAME, hostName);
}

/**
* Setup custom report api host name, if using the JPush privacy cloud.
* @param hostName the custom api host name, default is JPush domain name
*/
public void setReportHostName(String hostName) {
setReportHostName(this, hostName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public long getMsgId() {

public int getErrorCode() {
ErrorObject eo = getErrorObject();
if (null != eo) {
if (null != eo && null != eo.error) {
return eo.error.code;
}
return -1;
}

public String getErrorMessage() {
ErrorObject eo = getErrorObject();
if (null != eo) {
if (null != eo && null != eo.error) {
return eo.error.message;
}
return null;
Expand Down
60 changes: 43 additions & 17 deletions src/main/java/cn/jpush/api/common/resp/ResponseWrapper.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package cn.jpush.api.common.resp;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;

public class ResponseWrapper {
private static final Logger LOG = LoggerFactory.getLogger(ResponseWrapper.class);
private static final int RESPONSE_CODE_NONE = -1;

private static Gson _gson = new Gson();
private static JsonParser jsonParser = new JsonParser();

public int responseCode = RESPONSE_CODE_NONE;
public String responseContent;
Expand All @@ -36,21 +40,43 @@ public void setRateLimit(String quota, String remaining, String reset) {
}

public void setErrorObject() {
error = new ErrorObject();
error.error = new ErrorEntity();
try {
error = _gson.fromJson(responseContent, ErrorObject.class);
} catch (JsonSyntaxException e) {
int index = responseContent.indexOf("error");
if( -1 != index ) {
int from = responseContent.indexOf("{", index);
int to = responseContent.indexOf("}", from);
String errorStr = responseContent.substring(from, to + 1);
error = new ErrorObject();
try {
error.error = _gson.fromJson(errorStr, ErrorEntity.class);
} catch (JsonSyntaxException e1) {
LOG.error("unknown response content:" + responseContent, e);
JsonElement element = jsonParser.parse(responseContent);
JsonObject errorObj = null;
if( element instanceof JsonArray) {
JsonArray array = (JsonArray) element;
for(int i = 0; i < array.size(); i++) {
if(array.get(i).getAsString().contains("error")) {
errorObj = array.get(i).getAsJsonObject();
break;
}
}
} else if(element instanceof JsonObject) {
errorObj = (JsonObject) element;
} else {
// nothing
}
if(null != errorObj) {
JsonObject errorMsg = errorObj;
if(errorObj.has("msg_id")) {
error.msg_id = errorObj.get("msg_id").getAsLong();
}
if (errorObj.has("error")) {
errorMsg = (JsonObject) errorObj.get("error");
}
if(errorMsg.has("code")) {
error.error.code = errorMsg.get("code").getAsInt();
}
if(errorMsg.has("message")) {
error.error.message = errorMsg.get("message").getAsString();
}
}
} catch(JsonSyntaxException e) {
LOG.error("Unexpected - responseContent:" + responseContent, e);
} catch (Exception e) {
LOG.error("Unexpected - responseContent:" + responseContent, e);
}
}

Expand All @@ -75,9 +101,9 @@ public class ErrorEntity {
public String message;

@Override
public String toString() {
return _gson.toJson(this);
}
public String toString() {
return _gson.toJson(this);
}
}

}
Loading