因为被这个困扰了好久,所以记录一下…
网上大部分都是关于.p12的代码,没有看到有.p8的。
先上github:https://github.com/relayrides/pushy(如果有不懂,可以再来这里看)
在Java端使用Pushy进行APNs消息推送
1 首先先加入这两个包:
<!-- ios推送 pushy框架-->
<dependency>
<groupId>com.turo</groupId>
<artifactId>pushy</artifactId>
<version>0.13.7</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-tcnative-boringssl-static</artifactId>
<version>2.0.20.Final</version>
<scope>runtime</scope>
</dependency>
2 身份认证:
苹果APNs提供了两种认证的方式:基于JWT的身份信息token认证和基于证书的身份认证。Pushy也同样支持这两种认证方式。(待会会有区别)
如何获取苹果APNs身份认证证书可以查考官方文档。
3 Pushy使用:
这是.p8的:
package com.imagedt.message.util;
import java.io.File;
import com.turo.pushy.apns.auth.ApnsSigningKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.turo.pushy.apns.ApnsClient;
import com.turo.pushy.apns.ApnsClientBuilder;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
public class APNSConnect {
private static final Logger logger = LoggerFactory.getLogger(APNSConnect.class);
private static ApnsClient apnsClient = null;
public static ApnsClient getAPNSConnect() {
if (apnsClient == null) {
try {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup(4);
apnsClient = new ApnsClientBuilder().setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST)
.setSigningKey(ApnsSigningKey.loadFromPkcs8File(new File("path/你的放置.p8证书的路径"),
"你的teamid", "你的keyid"))
.setConcurrentConnections(4).setEventLoopGroup(eventLoopGroup).build();
} catch (Exception e) {
logger.error("ios get pushy apns client failed!");
e.printStackTrace();
}
}
return apnsClient;
}
}
还有个较好的,把文件放到resource下(建议)
路径可以写到application中。获取就是了
if (apnsClient == null) {
try {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup(4);
// apnsClient = new ApnsClientBuilder().setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST)
// .setSigningKey(ApnsSigningKey.loadFromPkcs8File(new File(jpushConfig.getPath()),//"C:/Users/user/Desktop/AuthKey_XNQ237XNW7