利用javapns对IOS进行推送

start

package com.jynine.javapns;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javapns.Push;
import javapns.communication.exceptions.CommunicationException;
import javapns.communication.exceptions.KeystoreException;
import javapns.devices.Device;
import javapns.devices.Devices;
import javapns.notification.PayloadPerDevice;
import javapns.notification.PushNotificationPayload;
import javapns.notification.transmission.PushQueue;

import org.apache.commons.lang.StringUtils;
import org.json.JSONException;

public class IosPushUtil {
	public static String keystore = null; 
	public static String password = null; 
	public static String host = null;
	public static Boolean production = true;//true:production false: sandbox 
	public static final int numberOfThreads = 8;
	static{
		Properties propertie = new Properties();
		InputStream inputStream;
		
		try {
			inputStream = IosPushUtil.class.getClassLoader()
					.getResourceAsStream("push.properties");
			propertie.load(inputStream);
			keystore = propertie.getProperty("certificatePath");
			password = propertie.getProperty("certificatePassword","123456");
			host = propertie.getProperty("host","gateway.push.apple.com");
			production = Boolean.valueOf(propertie.getProperty("production", "true"));
			inputStream.close();
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	public static void main(String[] args) throws Exception {
		//pushMsgNotification("hello!!!2", true, "iostoken");
	//	pushBadgeNotification(1,  "iostoken");
		String[] devs= new String[10000];
		for (int i = 0; i < devs.length; i++) {
		devs[i] = "iostoken";
		}
		List<Device> devices=Devices.asDevices(devs);
		System.out.println(devices.size());
		//pushPayLoadByThread(devices, "Hello 2222222", 1, null, null);
		//pushPayloadDevicePairs(devices, "Hello 111111111", 1, null, null);
		//pushPayloadDevicePairs(devices, "Hello +++", 1, null, null);
		queue(devices,"Hello 2222222", 1, null, null);
	}
	/**
	 * 推送一个简单消息
	 * @param msg 消息
	 * @param devices 设备
	 * @throws CommunicationException
	 * @throws KeystoreException
	 */
	public static void pushMsgNotification(String msg,Object devices) throws CommunicationException, KeystoreException{
		Push.alert(msg, keystore, password, production, devices);
	}
	/**
	 * 推送一个标记
	 * @param badge 标记
	 * @param devices 设备
	 * @throws CommunicationException
	 * @throws KeystoreException
	 */
	public static void pushBadgeNotification(int badge,Object devices) throws CommunicationException, KeystoreException{
		Push.badge(badge, keystore, password, production, devices);
	}
	/**
	 * 推送一个语音
	 * @param sound 语音
	 * @param devices 设备
	 * @throws CommunicationException
	 * @throws KeystoreException
	 */
	public static void pushSoundNotification(String sound,Object devices) throws CommunicationException, KeystoreException{
		Push.sound(sound, keystore, password, production, devices);
	}
	/**
	 * 推送一个alert+badge+sound通知
	 * @param message 消息
	 * @param badge 标记
	 * @param sound 声音
	 * @param devices 设备
	 * @throws CommunicationException
	 * @throws KeystoreException
	 */
	public static void pushCombinedNotification(String message,int badge,String sound,Object devices) throws CommunicationException, KeystoreException{
		Push.combined(message, badge, sound, keystore, password, production, devices);
	}
	/**
	 * 通知Apple的杂志内容
	 * @param devices 设备
	 * @throws CommunicationException
	 * @throws KeystoreException
	 */
	public static void contentAvailable(Object devices) throws CommunicationException, KeystoreException{
		Push.contentAvailable(keystore, password, production, devices);
	}
	/**
	 * 推送有用的调试信息
	 * @param devices 设备
	 * @throws CommunicationException
	 * @throws KeystoreException
	 */
	public static void test(Object devices) throws CommunicationException, KeystoreException{
		Push.test(keystore, password, production, devices);
	}
	/**
	 * 推送自定义负载
	 * @param devices
	 * @param msg
	 * @param badge
	 * @param sound
	 * @param map
	 * @throws JSONException
	 * @throws CommunicationException
	 * @throws KeystoreException
	 */
	public static void pushPayload(List<Device> devices, String msg,Integer badge,String sound,Map<String,String> map) throws JSONException, CommunicationException, KeystoreException{
		PushNotificationPayload payload = customPayload(msg, badge, sound, map);
		Push.payload(payload, keystore, password, production, devices);
	}
	/**
	 * 用内置线程推送负载信息
	 * @param devices
	 * @param msg
	 * @param badge
	 * @param sound
	 * @param map
	 * @throws Exception
	 */
	public static void pushPayLoadByThread(List<Device> devices, String msg,Integer badge,String sound,Map<String,String> map) throws Exception{
		PushNotificationPayload payload = customPayload(msg, badge, sound, map);
		Push.payload(payload, keystore, password, production, numberOfThreads, devices);
	}
	/**
	 * 推送配对信息
	 * @param devices
	 * @param msg
	 * @param badge
	 * @param sound
	 * @param map
	 * @throws JSONException
	 * @throws CommunicationException
	 * @throws KeystoreException
	 */
	public static void pushPayloadDevicePairs(List<Device> devices,String msg,Integer badge,String sound,Map<String,String> map) throws JSONException, CommunicationException, KeystoreException{
		List<PayloadPerDevice> payloadDevicePairs = new ArrayList<PayloadPerDevice>();
		PayloadPerDevice perDevice = null;
		for (int i = 0; i <devices.size(); i++) {
			perDevice = new PayloadPerDevice(customPayload(msg+"--->"+i, badge, sound, map), devices.get(i));
			payloadDevicePairs.add(perDevice);
		}
		Push.payloads(keystore, password, production, payloadDevicePairs);
	}
	/**
	 * 用线程推配对信息
	 * @param devices
	 * @param msg
	 * @param badge
	 * @param sound
	 * @param map
	 * @throws Exception
	 */
	public static void pushPayloadDevicePairsByThread(List<Device> devices,String msg,Integer badge,String sound,Map<String,String> map) throws Exception{
		List<PayloadPerDevice> payloadDevicePairs = new ArrayList<PayloadPerDevice>();
		PayloadPerDevice perDevice = null;
		for (int i = 0; i <devices.size(); i++) {
			perDevice = new PayloadPerDevice(customPayload(msg+"--->"+i, badge, sound, map), devices.get(i));
			payloadDevicePairs.add(perDevice);
		}
		Push.payloads(keystore, password, production,numberOfThreads, payloadDevicePairs);
	}
	/**
	 * 队列多线程推送
	 * @param devices
	 * @param msg
	 * @param badge
	 * @param sound
	 * @param map
	 * @throws KeystoreException
	 * @throws JSONException
	 */
	public static void queue(List<Device> devices,String msg,Integer badge,String sound,Map<String,String> map) throws KeystoreException, JSONException{
		PushQueue queue = Push.queue(keystore, password, production, numberOfThreads);
		queue.start();
		PayloadPerDevice perDevice = null;
		for (int i = 0; i <devices.size(); i++) {
			perDevice = new PayloadPerDevice(customPayload(msg+"--->"+i, badge, sound, map), devices.get(i));
			queue.add(perDevice);
		}
	}
	/**
	 * 自定义负载
	 * @param msg
	 * @param badge
	 * @param sound
	 * @param map 自定义字典
	 * @return
	 * @throws JSONException
	 */
	private static PushNotificationPayload customPayload(String msg,Integer badge,String sound,Map<String,String> map) throws JSONException{
		PushNotificationPayload payload = PushNotificationPayload.complex();
		if(StringUtils.isNotEmpty(msg)){
			payload.addAlert(msg);			
		}
		if(badge != null){			
			payload.addBadge(badge);
		}
		payload.addSound(StringUtils.defaultIfEmpty(sound, "default"));
		if(map!=null && !map.isEmpty()){
			Object[] keys = map.keySet().toArray();		
			Object[] vals = map.values().toArray();
			if(keys!= null && vals != null && keys.length == vals.length){
				for (int i = 0; i < map.size(); i++) {					
					payload.addCustomDictionary(String.valueOf(keys[i]),String.valueOf(vals[i]));
				}
			}
		}
		return payload;
	}
}

  


以前用的 javapns-jdk15-165.jar 不支持多线程。后来在网上找到新的一个jar,可以支持多线程发送,与大家分享一下,不知道写的对不对,不对的话请指出,多谢。

将消息以多线程方式推送到iPhone服务器,不知道写的对不对,我在本地上测试是没有问题的,如有疑问请留言。

以下项目所用的jar包:

JavaPNS_2.1.jar

bcprov-jdk16-145-1.jar

log4j-1.2.15.jar

只记得这几个jar,如果不对,请留言我在看一下,多谢指正

JavaPNS_2.1.jar 地址

http://code.google.com/p/javapns/ 

 

Java代码   收藏代码
  1. public static void main(String[] args){  
  2.     String keystore = "D:/XXXXXXXX/XXX.p12";//证书路径和证书名  
  3.     String password = "XXXXXXXX"// 证书密码  
  4.     String token = "XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX";// 手机唯一标识  
  5.     boolean production = true// 设置true为正式服务地址,false为开发者地址  
  6.     int threadThreads = 10// 线程数  
  7. try {  
  8.         // 建立与Apple服务器连接  
  9.         AppleNotificationServer server = new AppleNotificationServerBasicImpl(keystore, password, production  );  
  10.     List<PayloadPerDevice> list = new ArrayList<PayloadPerDevice>();  
  11.     PushNotificationPayload payload = new PushNotificationPayload();  
  12.     payload.addAlert("推送内容");  
  13.     payload.addSound("default");// 声音  
  14.         payload.addBadge(1);//图标小红圈的数值  
  15.     payload.addCustomDictionary("url","www.baidu.com");// 添加字典   
  16.     PayloadPerDevice pay = new PayloadPerDevice(payload,token);// 将要推送的消息和手机唯一标识绑定  
  17.     list.add(pay);  
  18.   
  19.         NotificationThreads work = new NotificationThreads(server,list,threadThreads);//   
  20.     work.setListener(DEBUGGING_PROGRESS_LISTENER);// 对线程的监听,一定要加上这个监听  
  21.     work.start(); // 启动线程  
  22.     work.waitForAllThreads();// 等待所有线程启动完成  
  23.   
  24.  } catch (Exception e) {  
  25.     e.printStackTrace();  
  26.         }  
  27. }  
 

 

Java代码   收藏代码
  1. // 线程监听  
  2. public static final NotificationProgressListener DEBUGGING_PROGRESS_LISTENER = new NotificationProgressListener() {  
  3.         public void eventThreadStarted(NotificationThread notificationThread) {  
  4.             System.out.println("   [EVENT]: thread #" + notificationThread.getThreadNumber() + " started with " + " devices beginning at message id #" + notificationThread.getFirstMessageIdentifier());  
  5.         }  
  6.         public void eventThreadFinished(NotificationThread thread) {  
  7.             System.out.println("   [EVENT]: thread #" + thread.getThreadNumber() + " finished: pushed messages #" + thread.getFirstMessageIdentifier() + " to " + thread.getLastMessageIdentifier() + " toward "" devices");  
  8.         }  
  9.         public void eventConnectionRestarted(NotificationThread thread) {  
  10.             System.out.println("   [EVENT]: connection restarted in thread #" + thread.getThreadNumber() + " because it reached " + thread.getMaxNotificationsPerConnection() + " notifications per connection");  
  11.         }  
  12.         public void eventAllThreadsStarted(NotificationThreads notificationThreads) {  
  13.             System.out.println("   [EVENT]: all threads started: " + notificationThreads.getThreads().size());  
  14.         }  
  15.         public void eventAllThreadsFinished(NotificationThreads notificationThreads) {  
  16.             System.out.println("   [EVENT]: all threads finished: " + notificationThreads.getThreads().size());  
  17.         }  
  18.         public void eventCriticalException(NotificationThread notificationThread, Exception exception) {  
  19.             System.out.println("   [EVENT]: critical exception occurred: " + exception);  
  20.         }  
  21.      };  
 

 

 


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值