PushKit.java 3.0 KB
package com.air.push;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.jfinal.log.Log;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;

public class PushKit {
	// 康爱思
	private final static String APP_KEY = "aced41f410cced7e08982f2c";
	private final static String MASTER_SECRET = "0eda135e4bbba17f7e7a11d3";

	public static void main(String[] args) {
		Random random = new Random(System.currentTimeMillis());

		Map<String, String> extras = new HashMap<String, String>();
		extras.put("title", "title by kevin " + random.nextInt());
		extras.put("summary", "summary " + random.nextInt());
		extras.put("content", "content " + random.nextInt());
		extras.put("createTime", new Date().toString());
		extras.put("updateTime", new Date().toString());
		extras.put("pushId", String.valueOf(10001));
		extras.put("pushType", String.valueOf(PushType.PUSH_MESSAGE));

		String alert = "alert by kevin " + random.nextInt();
		String title = extras.get("title");
		boolean flag = PushKit.push(PushKit.allAndroid(alert, title, extras));

		Log.getLog(new PushKit().getClass()).error("flag-->" + flag);
	}

	private PushKit() {
	}

	public static boolean push(PushPayload payload) {
		JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());

		PushResult result = null;
		try {
			result = jpushClient.sendPush(payload);
			Log.getLog(new PushKit().getClass()).error("Got result - " + result);

		} catch (APIConnectionException e) {
			// Connection error, should retry later
			Log.getLog(new PushKit().getClass()).error("Connection error, should retry later", e);

		} catch (APIRequestException e) {
			// Should review the error, and fix the request
			Log.getLog(new PushKit().getClass()).error("Should review the error, and fix the request", e);
			Log.getLog(new PushKit().getClass()).error("HTTP Status: " + e.getStatus());
			Log.getLog(new PushKit().getClass()).error("Error Code: " + e.getErrorCode());
			Log.getLog(new PushKit().getClass()).error("Error Message: " + e.getErrorMessage());
		}

		if (result != null) {
			if (result.isResultOK()) {
				return true;
			}
		}

		return false;
	}

	public static PushPayload allAndroid(String alert, String title, Map<String, String> extras) {
		return PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.all())
				.setNotification(Notification.android(alert, title, extras)).build();
	}

	public static PushPayload allIos(String alert, String title, Map<String, String> extras) {
		return PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.all())
				.setNotification(Notification.android(alert, title, extras)).build();
	}

}