当前位置: 首页 > 工具软件 > JavaPNS > 使用案例 >

java 实现 ios 推送_Java实现iOS推送(利用javapns1.6)

曾喜
2023-12-01

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 = "d7e6132895b388cf016433167c9e2d97fe4b76ca5a1692209a3b6e3cb3fdcd9c";

//被推送的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 = "D:/PushDev.p12"; //刚才在mac系统下导出的证书

String certificatePassword= "123456";

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());

}

}

}

 类似资料: