IOS的推送机制:
APNS是Apple Push Notification Service的缩写,也就是苹果的推送服务器。
推送流程
1.IOS从APNS Server中获取deviceToken,发送给应用程序端
2.应用程序端将Token发送给服务器
3.服务器将向APNS发送消息
4.APNS将信息推送给ipone应用程序
APNS推送的 常用框架有两种:
无论是javaPNS,还是pushy都需要ios的证书,xxxx.p12
1.javaPNS 相对比较简单,但是也有很大的缺点,javaPNS所依赖的jar包,从2016年开始已经不在更新了,而且JavaPNS推送是由字节限制的,
maven项目中 javaPNS需要的依赖:最新的版本是2016年5月发布的2.4.0
<!-- https://mvnrepository.com/artifact/com.github.fernandospr/javapns-jdk16 -->
<dependency>
<groupId>com.github.fernandospr</groupId>
<artifactId>javapns-jdk16</artifactId>
<version>2.4.0</version>
</dependency>
使用javaPNS推送
public class IOSAPNS {
// 从客户端获取的deviceToken,在此为了测试,设一个固定值
private static final String DEVICE_TOKEN ="35a52709fe5dda0490fec6be158dbc861e52c6d98d707900f559a41fae11c281";
public static void send() {
List<String> deviceToken = new ArrayList<String>();
// 证书文件(.p12)在服务器端的目录
String filePath = null;
try {
String path = "C:/work/apns-dev-cert.p12";
filePath = java.net.URLDecoder.decode(path, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("filePath=" + filePath);
String certificatePath = (filePath);
// 获取ios_development.p12的密码
String password = "123123";
JSONObject js = new JSONObject();
js.put("wwe", "测试数据");
// 设别标识
deviceToken.add(DEVICE_TOKEN);
// 发送消息
sendpush(deviceToken, certific