极光推送 java 绑定别名_jPush极光推送 测试demo以及绑定regid/标签/别名

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值