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

这个Java类实现了极光推送服务的客户端功能,包括初始化、绑定手机号、生成别名、绑定和更新标签、删除标签、检查设备与别名绑定关系、解绑别名和手机号等操作。在每个API调用中,都包含了错误处理和日志记录。
摘要由CSDN通过智能技术生成

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
    评论
要在Spring Boot中使用极光推送服务,你需要进行以下几个步骤。 1. 首先,你需要在极光官网(https://www.jiguang.cn/)进行注册并开通极光推送服务。 2. 然后,在你的Spring Boot程序中,创建一个yml配置文件,用于配置推送服务的相关信息。 3. 在程序中引入极光推送的服务类,并注入到需要使用的地方。你可以使用@Autowired注解将JiGuangPushService注入到你的控制器中。 4. 如果你想进行广播推送,即给所有拥有你的app下载安装的用户发送推送消息,你可以使用pushAll方法。该方法接收推送标题和内容作为参数,并返回一个布尔值表示推送是否成功。 5. 如果你想进行单点推送,即给指定的用户发送推送消息,你可以使用push方法。该方法接收推送标题、设备对应的唯一极光ID(regId)和推送内容作为参数,并返回一个布尔值表示推送是否成功。 通过以上步骤,你可以在Spring Boot中使用极光推送服务来发送推送消息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【SpringBoot】在SpringBoot中如何使用 极光推送](https://blog.csdn.net/peng_YuJun/article/details/130163389)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot整合极光推送](https://blog.csdn.net/qq_39252591/article/details/105224791)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值