苹果推送 JAVA

本方案采用了JAVAPNS,下载地址为:http://code.google.com/p/javapns/

代码如下:

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import javapns.notification.AppleNotificationServer;
import javapns.notification.AppleNotificationServerBasicImpl;
import javapns.notification.PayloadPerDevice;
import javapns.notification.PushNotificationPayload;
import javapns.notification.transmission.NotificationProgressListener;
import javapns.notification.transmission.NotificationThread;
import javapns.notification.transmission.NotificationThreads;

/**
 * 苹果通知
 * 
 * @author Administrator
 * 
 */
public class AppleNotification {

	/**
	 * 日志输出类
	 */
	private static Logger logger = Logger.getLogger(AppleNotification.class
			.getName());

	/**
	 * 苹果消息推送
	 * 
	 * @param certificatePath
	 *            证书路径
	 * @param certificatePassword
	 *            证书密码
	 * @param deviceToken
	 *            设备标识
	 * @param msg
	 *            消息内容
	 * @param msgCount
	 *            消息数
	 * @param host
	 *            苹果推送服务器域名(开发:gateway.sandbox.push.apple.com ;
	 *            正式:gateway.push.apple.com)
	 * @return
	 */
	public boolean push(String certificatePath, String certificatePassword,
			String deviceToken, String msg, int msgCount, String host,
			boolean isProduction) {

		String keystore = certificatePath;// 证书路径和证书名
		String password = certificatePassword; // 证书密码
		String token = deviceToken;// 手机唯一标识
		boolean production = isProduction; // 设置true为正式服务地址,false为开发者地址
		int threadThreads = 10; // 线程数
		try {
			// 建立与Apple服务器连接
			AppleNotificationServer server = new AppleNotificationServerBasicImpl(
					keystore, password, production);
			List<PayloadPerDevice> list = new ArrayList<PayloadPerDevice>();
			PushNotificationPayload payload = new PushNotificationPayload();
			payload.addAlert(msg);
			payload.addSound("default");// 声音
			payload.addBadge(msgCount);// 图标小红圈的数值
			PayloadPerDevice pay = new PayloadPerDevice(payload, token);// 将要推送的消息和手机唯一标识绑定
			list.add(pay);

			NotificationThreads work = new NotificationThreads(server, list,
					threadThreads);//
			work.setListener(DEBUGGING_PROGRESS_LISTENER);// 对线程的监听,一定要加上这个监听
			work.start(); // 启动线程
			work.waitForAllThreads();// 等待所有线程启动完成

		} catch (Exception e) {
			logger.error(e.getMessage());
			return false;
		}
		return true;
	}

	public static final NotificationProgressListener DEBUGGING_PROGRESS_LISTENER = new NotificationProgressListener() {

		public void eventThreadStarted(NotificationThread notificationThread) {
			logger.info("   [EVENT]: thread #"
					+ notificationThread.getThreadNumber() + " started with "
					+ " devices beginning at message id #"
					+ notificationThread.getFirstMessageIdentifier());
		}

		public void eventThreadFinished(NotificationThread thread) {
			logger.info("   [EVENT]: thread #"
					+ thread.getThreadNumber() + " finished: pushed messages #"
					+ thread.getFirstMessageIdentifier() + " to "
					+ thread.getLastMessageIdentifier() + " toward "
					+ " devices");
		}

		public void eventConnectionRestarted(NotificationThread thread) {
			logger.info("   [EVENT]: connection restarted in thread #"
					+ thread.getThreadNumber() + " because it reached "
					+ thread.getMaxNotificationsPerConnection()
					+ " notifications per connection");
		}

		public void eventAllThreadsStarted(
				NotificationThreads notificationThreads) {
			logger.info("   [EVENT]: all threads started: "
					+ notificationThreads.getThreads().size());
		}

		public void eventAllThreadsFinished(
				NotificationThreads notificationThreads) {
			logger.info("   [EVENT]: all threads finished: "
					+ notificationThreads.getThreads().size());
		}

		public void eventCriticalException(
				NotificationThread notificationThread, Exception exception) {
			logger.info("   [EVENT]: critical exception occurred: "
					+ exception);
		}
	};
}

猜你喜欢

转载自linyu19872008.iteye.com/blog/1882512