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库以及如何在Java环境中集成并使用它来实现iOS推送通知。 **JavaPNS库介绍** JavaPNS是一个轻量级、易于使用的库,旨在简化开发人员与Apple Push Notification Service之间的交互。...
JavaPNS是Java平台上用于实现Apple Push Notification Service (APNs)的一个开源库,版本2.2提供了对iPhone设备推送通知的支持。APNs是苹果公司提供的一个服务,允许开发者向iOS、iPadOS以及watchOS设备发送远程通知...
通过以上步骤,开发者可以成功地在iOS应用中实现原生推送功能,利用bcprov-jdk16-145.jar和javapns-jdk16-163.jar这两个关键依赖,与APNs服务器进行高效、安全的通信。然而,值得注意的是,现在的iOS开发趋势是更多...
在iOS设备上实现消息推送服务,开发者通常会利用苹果的Apple Push Notification service(APNs)。本文将详细讲解如何使用JAVA程序来调用APNs服务,为iOS设备提供消息推送功能,并提供必要的依赖包和实例代码。 ...
这个库使得开发者能够轻松地从Java应用程序发送推送通知到iOS和macOS设备。JavaPNS_2.2是该库的一个版本,包含了一些改进和修复,以提高稳定性和性能。 在JavaPNS_2.2.jar文件中,包含了JavaPNS库的核心代码,这些...
这个DEMO对于学习如何在Java后端集成iOS推送通知非常有用,它涵盖了从设置环境到实际推送的全过程,有助于开发者理解APNS的工作原理以及如何在自己的项目中实现这一功能。同时,它也强调了跨平台通信的重要性,即...
苹果公司为了方便开发者向iOS设备发送推送通知,推出了自己的推送服务——Apple Push Notification service (APNs)。为了更好地利用APNs,开发者通常会选择第三方库来简化这一过程。本例中使用的库为`JavaPNS`,它...
总的来说,JavaPNS是一个强大且易于使用的Java库,对于那些希望在Android和iOS平台上实现跨平台推送通知的开发者来说,它是一个非常有价值的工具。通过学习和掌握JavaPNS,开发者可以高效地实现与Apple Push ...