com.luues.push.ios.APNSPushUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-mobile-push Show documentation
Show all versions of commons-mobile-push Show documentation
A simple package of IOS and Aurora push
package com.luues.push.ios;
import javapns.Push;
import javapns.communication.exceptions.CommunicationException;
import javapns.communication.exceptions.KeystoreException;
import javapns.devices.Device;
import javapns.devices.exceptions.InvalidDeviceTokenFormatException;
import javapns.notification.AppleNotificationServer;
import javapns.notification.AppleNotificationServerBasicImpl;
import javapns.notification.PayloadPerDevice;
import javapns.notification.PushNotificationPayload;
import javapns.notification.transmission.NotificationProgressListener;
import javapns.notification.transmission.NotificationThread;
import javapns.notification.transmission.NotificationThreads;
import javapns.notification.transmission.PushQueue;
import org.json.JSONException;
import java.util.*;
public class APNSPushUtil {
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 property = new Properties();
InputStream inputStream;
try {
inputStream = APNSPushUtil.class.getClassLoader().getResourceAsStream("push.properties");
property.load(inputStream);
keystore = property.getProperty("certificatePathPush");
password = property.getProperty("certificatePassword", "qty");
host = property.getProperty("host", "gateway.push.apple.com");
production = Boolean.valueOf(property.getProperty("production", "true"));
inputStream.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}*/
/**
* ios 推送
* @param co
* @param title
* @param content
*/
private void pushMsg(Collection co, String title, String content) throws KeystoreException, JSONException {
boolean production = true; // 设置true 为正式服务地址,false 为开发者地址
int threadThreads = 2; // 线程数
// 建立与Apple服务器连接
final AppleNotificationServer server = new AppleNotificationServerBasicImpl(keystore, password, production);
final PushNotificationPayload payload = new PushNotificationPayload();
payload.addAlert(content);
payload.addCustomDictionary("title", title);
Iterator iterator = co.iterator();
int k = 1 ;
int j = 1 ;
List list = new ArrayList<>();
while (iterator.hasNext()) {
String str = (String) iterator.next();
try {
PayloadPerDevice pay = new PayloadPerDevice(payload, str);
list.add(pay);
} catch (InvalidDeviceTokenFormatException e1) {
e1.printStackTrace();
}
if(j == 10){
j = 1;
if(k == 10){
k = 1;
try {
Thread.sleep(2000 * 1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
pushThread pThread = new pushThread(list, threadThreads, server);
pThread.start();
k++;
list = new ArrayList<>();
}
j++;
}
}
public static void apnsTextPush(String deviceToken, String msg, boolean production) {
//boolean production = true; // 设置true 为正式服务地址,false 为开发者地址
int threadThreads = 10; // 线程数
try {
// 建立与Apple服务器连接
if (production) {
keystore = "/home/www/cypt-admin/webapps/root/WEB-INF/classes/push.p12";
} else {
keystore = "/home/www/cypt-admin/webapps/root/WEB-INF/classes/dev.p12";
}
AppleNotificationServer server = new AppleNotificationServerBasicImpl(keystore, password, production);
List list = new ArrayList<>();
PushNotificationPayload payload = new PushNotificationPayload();
payload.addAlert(msg);
payload.addSound("default");// 声音
payload.addBadge(1); //图标小红圈的数值
payload.addSound("msg.mp3");
payload.addCustomDictionary("url", "www.wqzhu.com");// 添加字典
PayloadPerDevice pay = new PayloadPerDevice(payload, deviceToken);// 将要推送的消息和手机唯一标识绑定
list.add(pay);
NotificationThreads work = new NotificationThreads(server, list, threadThreads);//
work.setListener(DEBUGGING_PROGRESS_LISTENER);// 对线程的监听,一定要加上这个监听
work.start(); // 启动线程
work.waitForAllThreads();// 等待所有线程启动完成
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
String keystore = "D:/workspace/wqcypt/cypt-admin/src/course/resources/push.p12"; //证书路径和证书名
//String keystore = "/home/www/cypt-admin/webapps/root/WEB-INF/classes/push.p12"; //证书路径和证书名
String password = "qty"; // 证书密码
String token = "0b1780be7a279acd312d6b8a74658210a8fae4971c3c368a84e0324a34addcfc"; // 手机唯一标识
boolean production = true; // 设置true 为正式服务地址,false 为开发者地址
int threadThreads = 10; // 线程数
try {
// 建立与Apple服务器连接
AppleNotificationServer server = new AppleNotificationServerBasicImpl(keystore, password, production);
List list = new ArrayList<>();
PushNotificationPayload payload = new PushNotificationPayload();
payload.addAlert("诶~这个推送,搞了好久了哇~{{{(>_<)}}}~" + System.currentTimeMillis());
payload.addSound("default");// 声音
payload.addBadge(1);//图标小红圈的数值
payload.addSound("msg.mp3");
payload.addCustomDictionary("msgType", 2); // 添加字典
payload.addCustomDictionary("id", 1446044586970006l); // 添加字典
payload.addCustomDictionary("userType", 1); // 添加字典
PayloadPerDevice pay = new PayloadPerDevice(payload, token);// 将要推送的消息和手机唯一标识绑定
list.add(pay);
NotificationThreads work = new NotificationThreads(server, list, threadThreads);//
work.setListener(DEBUGGING_PROGRESS_LISTENER);// 对线程的监听,一定要加上这个监听
work.start(); // 启动线程
work.waitForAllThreads();// 等待所有线程启动完成
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 推送一个简单消息
*
* @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 devices, String msg, Integer badge, String sound, Map 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 devices, String msg, Integer badge, String sound, Map 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 devices, String msg, Integer badge, String sound, Map map) throws JSONException, CommunicationException, KeystoreException {
List payloadDevicePairs = new ArrayList<>();
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 devices, String msg, Integer badge, String sound, Map map) throws Exception {
List payloadDevicePairs = new ArrayList<>();
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 devices, String msg, Integer badge, String sound, Map 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 map) throws JSONException {
PushNotificationPayload payload = PushNotificationPayload.complex();
if (null != msg && msg.length() > 0) {
payload.addAlert(msg);
}
if (badge != null) {
payload.addBadge(badge);
}
payload.addSound(null == sound ? "default" : sound.length() <= 0 ? "default" : sound);
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;
}
// 线程监听
public static final NotificationProgressListener DEBUGGING_PROGRESS_LISTENER = new NotificationProgressListener() {
public void eventThreadStarted(NotificationThread notificationThread) {
//System.out.println("[EVENT]: thread #" + notificationThread.getThreadNumber() + " started with " + " devices beginning at message id #" + notificationThread.getFirstMessageIdentifier());
}
public void eventThreadFinished(NotificationThread thread) {
//System.out.println("[EVENT]: thread #" + thread.getThreadNumber() + " finished: pushed messages #" + thread.getFirstMessageIdentifier() + " to " + thread.getLastMessageIdentifier() + " toward " + " devices");
}
public void eventConnectionRestarted(NotificationThread thread) {
//System.out.println("[EVENT]: connection restarted in thread #" + thread.getThreadNumber() + " because it reached " + thread.getMaxNotificationsPerConnection() + " notifications per connection");
}
public void eventAllThreadsStarted(NotificationThreads notificationThreads) {
//System.out.println("[EVENT]: all threads started: " + notificationThreads.getThreads().size());
}
public void eventAllThreadsFinished(NotificationThreads notificationThreads) {
//System.out.println("[EVENT]: all threads finished: " + notificationThreads.getThreads().size());
}
public void eventCriticalException(NotificationThread notificationThread, Exception exception) {
//System.out.println("[EVENT]: critical exception occurred: " + exception);
}
};
}
class pushThread extends Thread {
private List list;
private int threadThreads;
private AppleNotificationServer server;
public pushThread(List list, int threadThreads, AppleNotificationServer server){
this.list = list;
this.threadThreads = threadThreads;
this.server = server;
}
public void run(){
try {
NotificationThreads work = new NotificationThreads(server, list, threadThreads);//
work.setListener(APNSPushUtil.DEBUGGING_PROGRESS_LISTENER); // 对线程的监听,一定要加上这个监听
work.start(); // 启动线程
work.waitForAllThreads(); // 等待所有线程启动完成
} catch (Exception e) {
e.printStackTrace();
}
}
}