/**
* apple批量的推送方法
* @param tokens iphone手机获取的token
* @param content 推送消息的内容
* @param count 应用图标上小红圈上的数值
* @param goal 目标服务器 true:正式 false:测试
* @parm List 返回发送失败的tokens
*/
public List ApnsSend(List tokenList,String content,int count ,boolean goal){
List tokens = new ArrayList();
try {
String certificatePath="";
String certificatePassword="";
if(!goal){
//测试证书地址+密码
certificatePath=PropertiesUtil.getProperty("testCertificatePath");
certificatePassword=PropertiesUtil.getProperty("testCertificatePwd");
}else{
//正式证书地址+密码
certificatePath=PropertiesUtil.getProperty("certificatePath");
certificatePassword=PropertiesUtil.getProperty("certificatePwd");
}
PushNotificationManager pushManager = new PushNotificationManager();//true:表示的是产品发布推送服务 false:表示的是产品测试推送服务
pushManager.initializeConnection(new AppleNotificationServerBasicImpl(certificatePath, certificatePassword, goal));
//这个地方估计数据多会有错,不知道怎样该怎样做 是先拿到链接在用线程发怕长时间链接APNS服务器会给断开, 还是一个一个的开连接发
/*AppleNotificationServer server = null;
try {
server = new AppleNotificationServerBasicImpl(certificatePath,certificatePassword,goal);//true 正式 false 测试服务器
} catch (KeystoreException e) {
receipt="服务器链接失败";
System.out.println(receipt);
}*/
List tasks = TaskProducer.produce(tokenList,content,count,pushManager);
Queue taskQueue = new ConcurrentLinkedQueue();
taskQueue.addAll(tasks);
List threadHolder = new LinkedList();
TaskHandler taskHandler = new TaskHandler(taskQueue);
for(int i = 0; i < 10; i ++) {
Thread thread = new Thread(taskHandler);
threadHolder.add(thread);
thread.start();
}
while(true) {
boolean allFinished = true;
for(Thread thread : threadHolder) {
allFinished = allFinished && !thread.isAlive();
}
if(allFinished) {
break;
}
tokens = taskHandler.getTokens();
return tokens;
}catch(Exception e){
return tokens;
}
}
private static class TaskHandler implements Runnable {
private final Queue tasks;
public List tokens = new ArrayList();
public TaskHandler(Queue tasks) {
this.tasks = tasks;
}
public void run() {
while(!tasks.isEmpty()) {
Task task = tasks.poll();
if(task != null) {
try {
tokens.add(task.start());
} catch (Exception e) {
}
}
}
}
public List getTokens(){
return tokens;
}
}
/**
* @param args
* 测试方法
*/
public static void main(String[] args) {
PhonePush pp = new PhonePush();
List tokens=new ArrayList();
tokens.add("76edc85fd2e6704b27974d774cc046d7e33a3440fd6f39ba18c729387e6c788a");
tokens.add("dc2cf037bd4465c851b1d96a86b0a028307bc7e443435b6fafe93c2957bb415c");
tokens.add("d44f95e7f08bf62377a5b041584ce5dfcb9ce89e3d01a3846b96fe80e2a0991");
String content="批量测试"; //测试内容
int count=10; //手机上显示的数值 最好给1
boolean goal=false; //正式服务起ture 测试服务器false
tokens=pp.ApnsSend(tokens, content, count, goal);
System.out.println(tokens.size());
}
}
//还需要2个类 目的将所有任务加入到线程池
import java.util.List;
import javapns.communication.exceptions.CommunicationException;
import javapns.communication.exceptions.KeystoreException;
import javapns.devices.Device;
import javapns.devices.implementations.basic.BasicDevice;
import javapns.notification.PushNotificationManager;
import javapns.notification.PushNotificationPayload;
import javapns.notification.PushedNotification;
public class Task {
private int id;
private String token;
private PushNotificationPayload payload;
private List notifications ;
private PushNotificationManager pushManager;
public Task(int id,String token,PushNotificationPayload payload,PushNotificationManager pushManager) {
this.id = id;
this.token=token;
this.payload=payload;
this.pushManager=pushManager;
}
public String start() throws CommunicationException, KeystoreException {
String ftoken="";
Device device = new BasicDevice();
device.setToken(token);
notifications = pushManager.sendNotifications(payload, device);
List failedNotifications = PushedNotification.findFailedNotifications(notifications);
int failed = failedNotifications.size();
if(failed>0){
System.out.println(Thread.currentThread().getName() + ": error send faild " + id+token);
ftoken=token;
System.out.println(ftoken);
}
System.out.println(Thread.currentThread().getName() + ": start to handle task " + id+token);
return ftoken;
}
}
//第二个类 其实可以不用将每个的token的payload都加载一遍,因为内容都一样,可以将一个封装好的payload穿进去,这里就不做了。批量还没有做大量的测试,会有BUG的 //- -
import java.util.LinkedList;
import java.util.List;
import org.json.JSONException;
import javapns.notification.PushNotificationManager;
import javapns.notification.PushNotificationPayload;
public class TaskProducer {
public static List produce(List tokenList,String content,int count,PushNotificationManager pushManager) throws JSONException {
List tasks = new LinkedList();
PushNotificationPayload payload = null;
for(int i = 0; i < tokenList.size(); i ++) {
payload = new PushNotificationPayload();
payload.addAlert(content);
payload.addSound("default");
payload.addBadge(count);
tasks.add(new Task((i + 1),tokenList.get(i),payload,pushManager));
}
return tasks;
}
}