public class clientServiceImpl {
private static Logger LOG = LoggerFactory.getLogger(clientServiceImpl.class);
private String regid = "" ;
protected static JPushClient jPushClient = null ;
public clientServiceImpl(String t){
regid=t;
}
/**
* 声明jPushClient
* @return
*/
public JPushClient getJPushClient(){
return new JPushClient(JPushConfig.MASTER_SECRET,JPushConfig.APP_KEY);
}
/**
*
* @param mobile
* @return
* regid和手机号进行关联
*/
public boolean bindMobile(String mobile) {
try {
DefaultResult result = getJPushClient().bindMobile(regid, mobile);
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("Error Message: " + e.getErrorMessage());
}finally {
getJPushClient().close();
}
return false;
}
/**
*
* @param name
* @return
*生成别名
*/
public boolean bindAlias(String name, HashSet tagsToAdd) {
try {
DefaultResult defaultResult = getJPushClient().updateDeviceTagAlias(regid, name, tagsToAdd,
new HashSet(getJPushClient().getDeviceTagAlias(regid).tags));
LOG.info("Got result"+defaultResult);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
e.printStackTrace();
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
e.printStackTrace();
}finally {
getJPushClient().close();
}
return false;
}
/**
* 生成标签
* @param name
* @param tagsToAdd
* @return
*/
public boolean bindTags(String name, HashSet tagsToAdd){
try {
DefaultResult result = getJPushClient().updateDeviceTagAlias(regid, name
, new HashSet(Collections.singleton((getJPushClient().getDeviceTagAlias(regid).alias)))
, tagsToAdd);
LOG.info("Got result"+result);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
e.printStackTrace();
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
e.printStackTrace();
}finally {
getJPushClient().close();
}
return false;
}
/**
* 更新标签
* @param
* @return
*/
public boolean changeTags(List> list) {
for (int i = 0;i
try {
DefaultResult defaultResult = getJPushClient().addRemoveDevicesFromTag(
String.valueOf(list.get(i))
, Collections.singleton(regid)
, null);
LOG.info("Got result" + defaultResult);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
e.printStackTrace();
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
e.printStackTrace();
}
}
return false;
}
/**
* 删除标签
*/
public void clearTags() {
try {
DefaultResult defaultResult = getJPushClient().deleteTag(String.valueOf(getJPushClient().getDeviceTagAlias(regid).tags),regid);
LOG.info("Got result"+defaultResult);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
LOG.error("Error response from JPush server. Should review and fix it. ", e);
}finally {
getJPushClient().close();
}
}
/**
* 判断设备与别名的绑定关系
* @return
*/
public BooleanResult hasMobile() {
BooleanResult deviceInTag = null;
try {
deviceInTag = getJPushClient().isDeviceInTag(
String.valueOf(getJPushClient().getDeviceTagAlias(regid).alias), regid);
LOG.info("Got result" + deviceInTag);
return deviceInTag ;
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}finally {
getJPushClient().close();
}
return deviceInTag ;
}
/**
* 删除别名
* @param
*/
public void unbindAlias() {
try {
DefaultResult defaultResult = getJPushClient().updateDeviceTagAlias(regid, true, true);
LOG.info("Got result"+defaultResult);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
e.printStackTrace();
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
e.printStackTrace();
}finally {
getJPushClient().close();
}
}
/**
* 解除绑定
*/
public void unbindMobile() {
DefaultResult result = null;
try {
result = getJPushClient().bindMobile(regid, null);
LOG.info("Got result"+result);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
e.printStackTrace();
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
e.printStackTrace();
}
}
public HashMap dgetInfo() {
HashMap map = new HashMap<>();
try {
map.put("alias",getJPushClient().getDeviceTagAlias(regid).alias);
map.put("tags",getJPushClient().getDeviceTagAlias(regid).tags);
map.put("mobile",getJPushClient().isDeviceInTag(
String.valueOf(getJPushClient().getDeviceTagAlias(regid).alias), regid));
LOG.info("alias"+" : "+map.get("alias")+" , "+"teges"+" : "+map.get("tags")+" , "+"mobile"+" : "+map.get("mobile"));
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
e.printStackTrace();
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
e.printStackTrace();
}
return map;
}
}