JPushUtils

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());
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值