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<String, String> 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<String, String> 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<String> 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<String> getAlias() {
		return alias;
	}

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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值