IOS 推送服务端代码

来源:互联网 发布:女生说话软件 编辑:程序博客网 时间:2024/05/29 02:11
/** *@author LiTengFei *@version 2014-7-12  下午10:12:35 */package com.ldu.notification;import com.ldu.util.ProjectPath;import javapns.back.PushNotificationManager;import javapns.back.SSLConnectionHelper;import javapns.data.Device;import javapns.data.PayLoad;/** * TODO *  * @author LiTengFei * @version 2014-7-12 下午10:12:35 */public class IOSPushNotificationManager {public static void pushNotification(String deviceToken, String alertString, int badge, String sound) {try {// 从客户端获取的deviceToken,在此为了测试简单,写固定的一个测试设备标识。// String deviceToken =// "12f4669b 3f5585c0 fe433594 8f29b704 401d9ec1 f5ba967f b13b19a6 55c64a93";if (deviceToken.length() == 0) {deviceToken = "12f4669b 3f5585c0 fe433594 8f29b704 401d9ec1 f5ba967f b13b19a6 55c64a93";}System.out.println("Push Start deviceToken:" + deviceToken);// 定义消息模式PayLoad payLoad = new PayLoad();payLoad.addAlert("this is test!");// 消息推送标记数,小红圈中显示的数字。payLoad.addBadge(1);payLoad.addSound("default");// 注册deviceTokenPushNotificationManager pushManager = PushNotificationManager.getInstance();pushManager.addDevice("iPhone", deviceToken);// 连接APNSString host = "gateway.sandbox.push.apple.com";// String host = "gateway.push.apple.com";int port = 2195;// 前面生成的用于JAVA后台连接APNS服务的*.p12文件位置String certificatePath = ProjectPath.getClassesPath() + "com.juttec.guoxing.p12";String certificatePassword = "guoxing";// p12文件密码。pushManager.initializeConnection(host, port, certificatePath, certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);// 发送推送Device client = pushManager.getDevice("iPhone");System.out.println("推送消息: " + client.getToken() + "\n" + payLoad.toString() + " ");pushManager.sendNotification(client, payLoad);// 停止连接APNSpushManager.stopConnection();// 删除deviceTokenpushManager.removeDevice("iPhone");System.out.println("Push End");} catch (Exception ex) {ex.printStackTrace();}}}<pre name="code" class="plain">另外需要 log4j.jar 和 bcprov.jar


                                             
0 0
原创粉丝点击