/**
* @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");
// 注册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;
// 前面生成的用于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);
// 停止连接APNS
pushManager.stopConnection();
// 删除deviceToken
pushManager.removeDevice("iPhone");
System.out.println("Push End");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
<pre name="code" class="plain">另外需要 log4j.jar 和 bcprov.jar
IOS 推送服务端代码
最新推荐文章于 2021-08-12 13:45:26 发布