极光后台服务器推送消息通知

1:推送工具类

package com.jetf.util;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;

import com.jfinal.kit.JsonKit;

public class AJpushUtils {
	static Logger logger = LoggerFactory.getLogger(AJpushUtils.class);
	public static AjPushPojo ajPushPojo = null;
	public static JPushClient jpushClient = null;

	/**
	 * 发送通知
	 * 
	 * @param registrationId
	 *            设备标识
	 * @param alert
	 *            推送内容
	 */
	public static void jSend_notification(String masterSecret, String appKey,
			String content, String alert, Map map,
			String... alias) {
		logger.info(masterSecret);
		jpushClient = new JPushClient(masterSecret, appKey, 3);
		try {
			ajPushPojo = new AjPushPojo();
			// ajPushPojo.setMessage(content);
			// 根据别名推送通知,不传默认推送全部
			if (alias.length >= 1) {
				ajPushPojo.setAlias(alias);
			}
			ajPushPojo.setNotification(alert, map);
			PushResult result = jpushClient
					.sendPush(JsonKit.toJson(ajPushPojo));
			logger.info(result.msg_id + "===" + result.sendno);

		} catch (APIConnectionException e) {
			logger.info(" e: {}", e);
		} catch (APIRequestException e) {
			logger.info(" e: {}", e);

		}
	}

	/**
	 * 发送消息
	 * 
	 * @param registrationId
	 *            设备标识
	 * @param alert
	 *            推送内容
	 */
	public static void jSend_Message(String masterSecret, String appKey,
			String alert, Map map, String... alias) {
		logger.info(masterSecret);
		jpushClient = new JPushClient(masterSecret, appKey, 3);
		try {
			ajPushPojo = new AjPushPojo();
			// 根据别名推送通知,不传默认推送全部
			if (alias.length >= 1) {
				ajPushPojo.setAlias(alias);
			}
			ajPushPojo.setMessage(alert, map);
			PushResult result = jpushClient
					.sendPush(JsonKit.toJson(ajPushPojo));
			logger.info(result.msg_id + "===" + result.sendno);

		} catch (APIConnectionException e) {
			logger.info(" e: {}", e);
		} catch (APIRequestException e) {
			logger.info(" e: {}", e);

		}
	}

	public static void jSend_notification1(String masterSecret, String appKey,
			String content, String alert, Map map,
			String... alias) {

	}
}


2:POJO类

package com.jetf.util;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.kit.JsonKit;

public class AjPushPojo {
	private String platform = "all";
	private String audience = "all";
	private String notification = "{}";
	private String message = "{\"msg_content\":\"sadsa\"}";
	private String options = "{\"time_to_live\": 60,\"apns_production\": true}";

	public String getPlatform() {
		return platform;
	}

	public void setPlatform(String... platform) {
		if (platform.length == 1) {
			this.platform = platform[0];
		} else {
			this.platform = JsonKit.toJson(platform);
		}
	}

	public String getAudience() {
		return audience;
	}

	public void setAlias(String... audience) {
		Map map = new HashMap();
		map.put("alias", (audience));
		this.audience = JsonKit.toJson(map);
	}

	public void setAudience(String audience) {
		this.audience = audience;
	}

	public String getNotification() {
		return notification;
	}

	public void setNotification(String alert, Map extras) {
		Map map = new HashMap();
		Map map1 = new HashMap();
		Map map2 = new HashMap();
		map1.put("extras", extras);
		map1.put("alert", alert);
		map2.put("extras", extras);
		map2.put("alert", alert);
		map2.put("sound", "default");
		map2.put("content-available", true);
		map.put("android", map1);
		map.put("ios", (map2));
		this.notification = JsonKit.toJson(map);
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message, Map extras) {
		this.message = "{}";
		Map map = new HashMap();
		map.put("extras", extras);
		map.put("msg_content", message);
		map.put("content_type", "text");
		this.message = JsonKit.toJson(map);
	}

	public String getOptions() {
		return options;
	}

	public void setOptions(String options) {
		this.options = options;
	}

}


3:JAR包    jpush-client-3.2.7.jar

MAVEN 仓库


cn.jpush.api
jpush-client
3.2.7

你可能感兴趣的:(java)