JavaPNS初级使用,关于Push工具类的使用

首先的Push里简单的

alert、badge、sound和combined就不过多的介绍了
就是推送简单的提示和声音,以及组合等推送。

重点介绍推送有数据内容的部分
payload有两种方法
其内部调用的内容不相同
其中一个是单线程的推送
通过PushNotificationManager发送推送
public static PushedNotifications payload(Payload payload, Object keystore, String password, boolean production, Object devices) throws CommunicationException, KeystoreException {
		return sendPayload(payload, keystore, password, production, devices);
	}

可以传递要推送设备Token的List,Payload里面就是要推送的方式以及推送的内容字典等。

另外一个就是多线程的推送
public static PushedNotifications payload(Payload payload, Object keystore, String password, boolean production, int numberOfThreads, Object devices) throws Exception {
		if (numberOfThreads <= 0) return sendPayload(payload, keystore, password, production, devices);
		AppleNotificationServer server = new AppleNotificationServerBasicImpl(keystore, password, production);
		List<Device> deviceList = Devices.asDevices(devices);
		NotificationThreads threads = new NotificationThreads(server, payload, deviceList, numberOfThreads);
		threads.start();
		try {
			threads.waitForAllThreads(true);
		} catch (InterruptedException e) {
		}
		return threads.getPushedNotifications(true);
	}



以上方法都是同步执行的,也就是当发送推送之后得到反馈才继续往下执行
----------------
下面介绍queue方法
public static PushQueue queue(Object keystore, String password, boolean production, int numberOfThreads) throws KeystoreException {
		AppleNotificationServer server = new AppleNotificationServerBasicImpl(keystore, password, production);
		PushQueue queue = numberOfThreads <= 1 ? new NotificationThread(server) : new NotificationThreads(server, numberOfThreads);
		return queue;
	}

这个方法是异步执行,执行完之后,不需要等待推送的反馈即可直接往下执行


在这你会发现,其实推送用的都是NotificationThread或者NotificationThreads
而这两个类有若干重载的构造方法
使用不同的构造方法来决定到底是同步还是异步推送


接下来是payloads方法
也是有两个重载的方法
也分别对应单线程和多线程
这个方法主要是可以针对不同的设备Token发送不同的信息内容
也就是说,针对于每一个设备有属于自己的信息内容
这里面主要使用的就是 PayloadPerDevice这个类
里面是一对一的内容,一个Payload,对应一个Token

以上大体就是JavaPNS最简单基础的使用方法,关于Push类的使用

猜你喜欢

转载自liyunpeng.iteye.com/blog/1814587