java批量推送_java de Iphone批量推送代码

news/2025/2/13 20:35:29/

/**

* 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;

}

}


http://www.ppmy.cn/news/181038.html

相关文章

UICollectionView 适配 iPhone 7 Plus

UICollectionView 适配 iPhone 7 Plus 需求&#xff1a;在屏幕上水平放置 5 张正方形图片&#xff0c;每张图片的宽度相等&#xff0c;无缝隙排列铺满一个屏幕宽度。 看似很简单的需求。用 UICollectionView 实现的话&#xff0c;把 UICollectionView 的宽度设置为屏幕宽度&…

6s如何无线连电脑连接服务器,iPhone 6s无线连接能力:WiFi与4G大提速

【PConline 杂谈】9月10日凌晨&#xff0c;苹果在旧金山正式发布了新一代iPhone&#xff0c;分别是iPhone 6s与iPhone 6s Plus两款机型。如同前期爆料一样&#xff0c;苹果为新iPhone带来了3D Touch等一系列功能和配置的提升&#xff0c;当中iPhone 6s和iPhone 6s Plus相比上代…

iphone 程序中加入表情符

NSString *s "This is a smiley \ue415 face"; testFace.text s; 上面代码就可以显示出笑脸符号&#xff0c;testFace是一个IBOutLet. 快试试吧。 这是为什么呢&#xff0c;因为iphone中包含日本字体&#xff0c;它支持unicode emoji字符集。 更多笑脸符号&am…

iPhone iPad高级编程

iPhone & iPad高级编程 编辑推荐 1. 本书中的示例颇具价值&#xff0c;浓缩了iPhone和iPad编程智慧&#xff0c;具有新颖独到之处。开发人员在日常工作中可以参考这些绝佳示例迅速找到解决方案。不管对于初始茅庐的新手&#xff0c;还是经验丰富的专业人员&#xff0c;本书…

iPhone数据库结构

/* * iphone database structure * author zye * contact zyetudou.com * update 20070924 * version 0.1 * url http://yegq.yeax.com/?p162 * copyleft 此文档可随意分发。如果你发布的iphone应用得益于此文档&#xff0c;建议注明&#xff0c;或给我email&#xff0c;共贺之…

iPhone开发秘笈pdf

下载地址&#xff1a;网盘下载 内容简介 本书提供了关于iPhone SDK 以及iPhone 开发的全面信息&#xff0c;对iPhone SDK 中的各种组件做了深入浅出的介绍&#xff0c;包括iPhone 3.0 SDK 的所有新增特性&#xff0c;同时对iPhone 开发的基本流程、基本原理和基本原则进…

iPhone第二节:登录、通讯录界面

登录、通讯录界面 mainViewController.m #import "ViewController.h" #import "ViewController2.h" #import "ViewController3.h" #import "ViewControllerTest.h"interface ViewController ()endimplementation ViewControllersynthe…

iPhone第五节:多媒体和手势

多媒体和手势 #import <UIKit/UIKit.h>interface CameraViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate>property (nonatomic, strong) UIImageView * cameraImageView;end#import …