package com.yxjz.v3.push;
import java.util.HashSet;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jiguang.common.resp.DefaultResult;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.PushPayload;
/**
* 推送工具类
* ClassName: JPushUtils <br/>
* Function: <br/>
* date: 2018年1月8日 上午9:57:05 <br/>
*
* @author joy
* @version
*/
public class JPushUtils {
private final static Logger LOGGER = LoggerFactory.getLogger(JPushUtils.class);
/**
* 应用的master_secret
*/
private static final String MASTERSECRET = "您的masterSecret";
/**
* 应用的appkey
*/
private static final String APPKEY = "您的appKey";
/**
* 推送消息
* sendMessage: . <br/>
*
* @author gmh
* @param payload
* @return
*/
public static PushResult sendMessage(PushPayload payload) {
JPushClient jpushClient = new JPushClient(MASTERSECRET, APPKEY);
PushResult result = null;
try {
result = jpushClient.sendPush(payload);
LOGGER.info("Got result - " + result);
} catch (APIConnectionException e) {
LOGGER.error("Connection error, should retry later", e);
} catch (APIRequestException e) {
LOGGER.error("Should review the error, and fix the request", e);
LOGGER.info("HTTP Status: " + e.getStatus());
LOGGER.info("Error Code: " + e.getErrorCode());
LOGGER.info("Error Message: " + e.getErrorMessage());
}
return result;
}
/**
* 修改标签
* updateDeviceTag: . <br/>
*
* @author gmh
* @param registrationId
* @param oldTag
* @param newTag
*/
public static void updateDeviceTag(String registrationId, String oldTag, String newTag) {
JPushClient jpushClient = new JPushClient(MASTERSECRET, APPKEY);
try {
Set<String> tagsToAdd = new HashSet<>();
tagsToAdd.add(newTag);
Set<String> tagsToRemove = new HashSet<>();
tagsToRemove.add(oldTag);
DefaultResult defaultResult = jpushClient.updateDeviceTagAlias(registrationId, null, tagsToAdd,
tagsToRemove);
LOGGER.info("Got result - " + defaultResult);
} catch (APIConnectionException e) {
LOGGER.error("Connection error, should retry later", e);
} catch (APIRequestException e) {
LOGGER.error("Should review the error, and fix the request", e);
LOGGER.info("HTTP Status: " + e.getStatus());
LOGGER.info("Error Code: " + e.getErrorCode());
LOGGER.info("Error Message: " + e.getErrorMessage());
}
}
}
JPushUtils
最新推荐文章于 2022-08-05 09:15:18 发布