JPUSH服务端集成(JAVA版本)

JPUSH用做APP的消息推送效果很不错,博主的APP项目是使用的IONIC框架的应用。

官网链接:https://www.jiguang.cn

1.首先需要jpush-client-3.2.9.jar

下载地址:http://download.csdn.net/detail/nobmr/9692427

2.集成代码

MessagePush.java

/**
 * Jpush服务端集成
 * @author hack
 *
 */
public class MessagePush {
	
	//TODO 跟服务器建立连接的最大尝试次数
	private static final int MAX_RETRY_NUM = 3;
	//TODO 注册应用的主密码,API主密码
	private static final String MASTER_SECRET = "***";
	//TODO 注册应用的APP_KEY
	private static final String APP_KEY = "***";
	
	private JPushClient jpushClient;
	private String title = "";
	private String content;
	
	public MessagePush(String content){
		this.content = content;
		jpushClient = new JPushClient(MASTER_SECRET, APP_KEY,MAX_RETRY_NUM);
	}
	
	public MessagePush(String title,String content){
		this(content);
		this.title = (title == null ? "" : title);
	}
	
	/**
	 * 向所有人推送消息
	 * @return 消息ID
	 */
	public long sendPushAll(){
		PushPayload payload = buildPushObject_all_all_alert();
		long msgId = 0;
		try{
			PushResult result = jpushClient.sendPush(payload);
			msgId = result.msg_id;
		}catch(APIConnectionException e){
			System.err.println("[Connection Error]:"+ e.toString());
		}catch(APIRequestException e){
			System.out.println("HTTP Status:"+e.getStatus());
			msgId = e.getMsgId();
		}
		return msgId;
	}
	
	/**
	 * 向指定别名的客户端推送消息
	 * @param alias 所有别名的集合[可以使用编号]
	 * @return 消息ID
	 */
	public long sendPushAlias(Set alias){
		//PushPayload payloadAlias = buildPushObject_android_alias_alertWithTitle(alias);
		PushPayload payloadAlias = buildPushObject_all_alias_alertWithTitle(alias);
		long msgId = 0;
		try{
			PushResult result = jpushClient.sendPush(payloadAlias);
			msgId = result.msg_id;
		}catch(APIConnectionException e){
			System.err.println("[Connection Error]:"+ e.toString());
		}catch(APIRequestException e){
			System.out.println("HTTP Status:"+e.getStatus());
			System.out.println("Error Code:"+e.getErrorCode());
			System.out.println("Error Message:"+e.getErrorMessage());
			System.out.println("Msg ID:"+e.getMsgId());
			msgId = e.getMsgId();
		}
		return msgId;
	}
	
	/**
	 * 向指定组推送消息
	 * @param tag 组名称
	 * @return 消息ID
	 */
	public long sendPushTag(String tag){
		PushPayload payloadtag = buildPushObject_android_tag_alertWithTitle(tag);
		long msgId = 0;
		try{
			PushResult result = jpushClient.sendPush(payloadtag);
			msgId = result.msg_id;
		}catch(APIConnectionException e){
			System.err.println("[Connection Error]:"+ e.toString());
		}catch(APIRequestException e){
			System.out.println("HTTP Status:"+e.getStatus());
			System.out.println("Error Code:"+e.getErrorCode());
			System.out.println("Error Message:"+e.getErrorMessage());
			System.out.println("Msg ID:"+e.getMsgId());
			msgId = e.getMsgId();
		}
		return msgId;
	}
	
	
	
	//TODO 下列封装了三种获得消息推送对象(android)
	public PushPayload buildPushObject_android_alias_alertWithTitle(Set alias){
		return PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.alias(alias)).setNotification(Notification.android(content, title, null)).build();
	}
	public PushPayload buildPushObject_android_tag_alertWithTitle(String tag){
		return PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.tag(tag)).setNotification(Notification.android(content, title, null)).build();
	}
	public PushPayload buildPushObject_all_all_alert(){
		return PushPayload.alertAll(content);
	}
	
	//TODO 全平台消息推送
	public PushPayload buildPushObject_all_alias_alertWithTitle(Set alias){
		return PushPayload.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.alias(alias))
				.setNotification(Notification.newBuilder()
						.setAlert(content)
						.addPlatformNotification(AndroidNotification.newBuilder()
								.setTitle(title).build())
						.addPlatformNotification(IosNotification.newBuilder()
								.incrBadge(1)
								//extra_key extra_value
								.addExtra(title,content).build())
								.build())
				.build();
	}
	
}

3.调用部分

首先需要在用户第一次登录或注册的时候向jpush进行注册alias别名,一般来说使用用户ID;然后在相关业务逻辑后面调用消息推送方法。

注:消息推送是一个耗时过程,在实际使用中最好封装到线程中。

你可能感兴趣的:(JavaEE)