Java调用极光推送推送消息

在项目开发过程中用到了定时给移动端推送消息的功能,经过测试发现可以正常使用,代码如下

public class JPushManager {

	// 日志
	private static final Logger log = Logger.getLogger(JPushManager.class);
	// 推送客户端
	private static JPushClient buyerJpushClient = null;

	static {
		String buyerAppKey = null; // 推送app key
		String buyerMasterSecret = null;// 推送app主密码
		// int buyerMaxRetryTimes = 3;// 推送最大重发次数

		buyerAppKey = "********";
		buyerMasterSecret = "***********";
		ClientConfig config = ClientConfig.getInstance();
		config.setMaxRetryTimes(5);
		config.setApnsProduction(false); 	// development env
		config.setTimeToLive(60 * 60 * 24);
		buyerJpushClient = new JPushClient(buyerMasterSecret, buyerAppKey,
				null, config);
	}

	public static void sendClient(JPushData pushData,
			Map extraMap) {
		PushPayload payload = buildPushObject_all_alias_alert(pushData,
				extraMap);
		try {
			PushResult result = buyerJpushClient.sendPush(payload);
			log.info("Got result - " + result);
		} catch (APIConnectionException e) {
			log.error("Connection error, should retry later", e);
		} catch (APIRequestException e) {
			log.error("Should review the error, and fix the request", e);
			log.info("HTTP Status: " + e.getStatus());
			log.info("Error Code: " + e.getErrorCode());
			log.info("Error Message: " + e.getErrorMessage());
		}
	}

	public static PushPayload buildPushObject_all_alias_alert(
			JPushData pushData, Map extraMap) {
		Message message = Message.newBuilder().setMsgContent(pushData.getContent())
				.setTitle(pushData.getTitle()).setContentType(pushData.getTag()).build();
		return PushPayload.newBuilder().setPlatform(Platform.all())
				.setAudience(Audience.registrationId(pushData.getAlias()))
//				.setNotification(Notification.alert(pushData.getContent()))//通知
				.setMessage(message)//使用自定义消息推送
				.build();
	}
}



public class JPushData {
	
	private String title;  //推送标题
	
	private String content;  //推荐内容
	
	private String tag;  //推送分类标签
	
	
	private List alias;  //

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public List getAlias() {
		return alias;
	}

	public void setAlias(List alias) {
		this.alias = alias;
	}
	
}


你可能感兴趣的:(Java编程)