1、获取p12文件方法哦工程src目录下 找ios开发人员要
2、导入jar文件 <a target=_blank href="http://download.csdn.net/detail/shy323/9452329">http://download.csdn.net/detail/shy323/9452329</a>
3、copy我给的类 就可以测试apns推送了
</pre><pre name="code" class="java">package com.foxconn.util;
import cn.teaey.apns4j.keystore.KeyStoreHelper;
import cn.teaey.apns4j.keystore.KeyStoreWraper;
import cn.teaey.apns4j.network.AppleGateway;
import cn.teaey.apns4j.network.AppleNotificationServer;
import cn.teaey.apns4j.network.SecurityConnection;
import cn.teaey.apns4j.network.SecuritySocketFactory;
import cn.teaey.apns4j.protocol.NotifyPayload;
public class ApnsUtil {
private static SecurityConnection securityConnection;
// 获取apns推送链接
private static SecurityConnection getSecurityConnection() {
if (securityConnection == null) {
try {
// get a keystore
KeyStoreWraper keyStoreWrapper = KeyStoreHelper.getKeyStoreWraper("doctor.p12", "123456");
// get apple server with env
AppleNotificationServer appleNotificationServer = AppleNotificationServer.get(AppleGateway.ENV_DEVELOPMENT);
// init ssl socket factory
SecuritySocketFactory socketFactory = SecuritySocketFactory.Builder.newBuilder().appleServer(appleNotificationServer).keyStoreWrapper(keyStoreWrapper).build();
// create a ssl socket
securityConnection = SecurityConnection.newSecurityConnection(socketFactory);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(securityConnection.hashCode());
return securityConnection;
}
// 推送apns消息
public static void sendNotification(String token, NotifyPayload notifyPayload) {
try {
getSecurityConnection().sendAndFlush(token, notifyPayload);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
NotifyPayload notifyPayload=NotifyPayload.newNotifyPayload();
notifyPayload.sound("msg.mp3");
notifyPayload.alertBody("How are you?");
notifyPayload.badge(1);
ApnsUtil.sendNotification("b356d706e73222705aed97012a7255a5a7356ae7c88914efa8d508a52f73f27b", notifyPayload);
}
}