java实现apns消息推送

阅读更多
import javapns.back.PushNotificationManager;
import javapns.back.SSLConnectionHelper;
import javapns.data.Device;
import javapns.data.PayLoad;

public class a {

	public static void main(String[] args) throws Exception {
		try {
			// 从客户端获取的deviceToken,在此为了测试简单,写固定的一个测试设备标识。
			String deviceToken = "b606c95c4b3956de89d67ecfa02811ebc5d1a09c6f832c3be67b1d9554a66683";
			System.out.println("Push Start deviceToken:" + deviceToken);
			// 定义消息模式
			PayLoad payLoad = new PayLoad();
			payLoad.addAlert("this is test!");
			payLoad.addBadge(1);// 消息推送标记数,小红圈中显示的数字。
			payLoad.addSound("default");
			// 注册deviceToken
			PushNotificationManager pushManager = PushNotificationManager.getInstance();
			pushManager.addDevice("iPhone", deviceToken);
			// 连接APNS
			String host = "gateway.sandbox.push.apple.com";
			// String host = "gateway.push.apple.com";
			int port = 2195;
			String certificatePath = "E:/123.p12";// 前面生成的用于JAVA后台连接APNS服务的*.p12文件位置
			String certificatePassword = "8dbmds";// p12文件密码。
			pushManager.initializeConnection(host, port, certificatePath, certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
			// 发送推送 991847304
			Device client = pushManager.getDevice("iPhone");
			System.out.println("推送消息: " + client.getToken() + "\n" + payLoad.toString() + " ");
			pushManager.sendNotification(client, payLoad);
			// 停止连接APNS
			pushManager.stopConnection();
			// 删除deviceToken
			pushManager.removeDevice("iPhone");
			System.out.println("Push End");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
  • 6ea219f1-31c6-39a5-aa8d-a04b49e97399.jar (85.7 KB)
  • 下载次数: 16
  • 7d21a70f-ce75-3a27-a743-526b62758091.jar (470.2 KB)
  • 下载次数: 12
  • 400d919d-90d5-33a2-801f-cfc35f76ffce.jar (1.6 MB)
  • 下载次数: 13
  • 501555cd-38ef-31c2-8660-a374a0b1b62f.jar (51.9 KB)
  • 下载次数: 12
  • 5732565c-1f23-3d0d-a094-ee8354be34d8.jar (255.7 KB)
  • 下载次数: 17

你可能感兴趣的:(java,apns)