package com.ios.PnsSend;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.ios.domain.PushInfo;
import javapns.notification.AppleNotificationServer;
import javapns.notification.AppleNotificationServerBasicImpl;
import javapns.notification.PayloadPerDevice;
import javapns.notification.PushNotificationPayload;
import javapns.notification.PushedNotification;
import javapns.notification.transmission.NotificationProgressListener;
import javapns.notification.transmission.NotificationThread;
import javapns.notification.transmission.NotificationThreads;
public class PnsSend {
/**
* 基于PNS的消息推送机制
* @param keystore 证书路径和证书名
* @param password 证书密码
* @param production 设置true为正式服务地址,false为开发者地址
* @param pushInfos
*/
public static void pushToClient(String keystore,String password,boolean production,List<PushInfo> pushInfos) {
int threadThreads = 10; // 线程数
try {
// 建立与Apple服务器连接
AppleNotificationServer server = new AppleNotificationServerBasicImpl(keystore, password, production);
List<PayloadPerDevice> list = new ArrayList<PayloadPerDevice>();
for (PushInfo pushInfo : pushInfos) {
PushNotificationPayload payload = new PushNotificationPayload();
payload.addAlert(pushInfo.getAlert());
payload.addSound("default");// 声音
payload.addBadge(1);//图标小红圈的数值
payload.addCustomDictionary(pushInfo.getCustomDictionaryKey(),pushInfo.getCustomDictionaryValue());// 添加字典
PayloadPerDevice pay = new PayloadPerDevice(payload,pushInfo.getToken());// 将要推送的消息和手机唯一标识绑定
list.add(pay);
}
NotificationThreads work = new NotificationThreads(server,list,threadThreads);//
work.setListener(DEBUGGING_PROGRESS_LISTENER);// 对线程的监听,一定要加上这个监听
work.start(); // 启动线程
work.waitForAllThreads();// 等待所有线程启动完成
} catch (Exception e) {
e.printStackTrace();
}
}
// 线程监听
public static final NotificationProgressListener DEBUGGING_PROGRESS_LISTENER = new NotificationProgressListener() {
public void eventThreadStarted(NotificationThread notificationThread) {
System.out.println(" [EVENT]: thread #" + notificationThread.getThreadNumber() + " started with " + " devices beginning at message id #" + notificationThread.getFirstMessageIdentifier());
}
public void eventThreadFinished(NotificationThread thread) {
System.out.println(" [EVENT]: thread #" + thread.getThreadNumber() + " finished: pushed messages #" + thread.getFirstMessageIdentifier() + " to " + thread.getLastMessageIdentifier() + " toward "+ " devices");
}
public void eventConnectionRestarted(NotificationThread thread) {
System.out.println(" [EVENT]: connection restarted in thread #" + thread.getThreadNumber() + " because it reached " + thread.getMaxNotificationsPerConnection() + " notifications per connection");
}
public void eventAllThreadsStarted(NotificationThreads notificationThreads) {
System.out.println(" [EVENT]: all threads started: " + notificationThreads.getThreads().size());
}
public void eventAllThreadsFinished(NotificationThreads notificationThreads) {
System.out.println(" [EVENT]: all threads finished: " + notificationThreads.getThreads().size());
}
public void eventCriticalException(NotificationThread notificationThread, Exception exception) {
System.out.println(" [EVENT]: critical exception occurred: " + exception);
}
};
}