第一种
import javapns.back.PushNotificationManager;
import javapns.back.SSLConnectionHelper;import javapns.data.Device;
import javapns.data.PayLoad;
public class Main {
/** APNs Server Host **/
private static final String HOST = "gateway.sandbox.push.apple.com";
/** APNs Port */
private static final int PORT = 2195;
public static void sendNotification() {
try {
int N = 1;
String tokenArray[] = new String[N];
tokenArray[0] = "f933082340c0d27c60e4c9ebfd4ed1b0524d9be49d51aa7fb0126dc4c141ae5c";
//b23366059d920e741bc8200ffed6747e81013b63130cb2819c747d6afc199c8d //要填写的token 9的系统
String deviceID = "iPhone";
for(int i=0; i<N; i++) {
deviceID = deviceID + i;
PayLoad payLoad = new PayLoad();
payLoad.addAlert("推送测试.......");
payLoad.addBadge(4);
payLoad.addSound("default");
String contentUrl;
//993402940480
contentUrl = "http://xxx.com/YearGoddness.aspx?tp=2";//推送链接
payLoad.addCustomDictionary("url", contentUrl);
payLoad.addCustomDictionary("id", "7555");
PushNotificationManager pushManager = PushNotificationManager.getInstance();
String p12FilePath = "ts.p12";//证书名称
String p12Password = "123654";//证书密码
pushManager.initializeConnection(HOST, PORT, p12FilePath, p12Password, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
String token = tokenArray[i];
pushManager.addDevice(deviceID, token);
Device client = pushManager.getDevice(deviceID);
// Send Push
pushManager.sendNotification(client, payLoad);
pushManager.stopConnection();
Thread.sleep(1000);
}
// pushManager.addDevice(deviceID, deviceToken);
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try
{
Main.sendNotification();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
第二种
import java.util.HashMap;
import java.util.Iterator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import javapns.back.PushNotificationManager;
import javapns.back.SSLConnectionHelper;
import javapns.data.Device;
import javapns.data.PayLoad;
public class MainApnsSend {
public static void main(String[] args) throws Exception {
try {
String deviceToken = "192958b8695bb35567e0d24d71c1232b686f305e5735483023ee5285b72aa612";
//被推送的iphone应用程序标示符
PropertyConfigurator.configure("bin/log4j.properties");
Logger console = Logger.getLogger(MainApnsSend.class);
PayLoad payLoad = new PayLoad();
payLoad.addAlert("彩通万岁!OLALA!!AAAA");
payLoad.addBadge(1);
payLoad.addSound("default");
PushNotificationManager pushManager = PushNotificationManager.getInstance();
pushManager.addDevice("iPhone", deviceToken);
String host= "gateway.sandbox.push.apple.com"; //测试用的苹果推送服务器
int port = 2195;
String certificatePath = "ts.p12"; //刚才在mac系统下导出的证书
String certificatePassword= "123654";
String contentUrl;
contentUrl = "http://www.baidu.com/";//推送链接
payLoad.addCustomDictionary("url", contentUrl);
pushManager.initializeConnection(host, port, certificatePath,certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
//Send Push
Device client = pushManager.getDevice("iPhone");
pushManager.sendNotification(client, payLoad);
pushManager.stopConnection();
pushManager.removeDevice("iPhone");
System.out.println("push succeed!");
}
catch (Exception e) {
//e.printStackTrace();
System.out.println("e.getMessage() = " + e.getMessage());
}
}
}