我使用PUSHY库0.15.2版本通过Spring boot向ios设备发送推送通知。
APNS 代码如下 -
ApnsClientBuilder clientBuilder = new ApnsClientBuilder();
clientBuilder.setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST);
clientBuilder.setConnectionTimeout(Duration.ofMillis(60000));
File file = new File(getClass().getClassLoader().getResource("zzzzz").getFile());
clientBuilder.setClientCredentials(file, "yyyyy");
apnsClient = clientBuilder.build();
ApnsPayloadBuilder payloadBuilder = new SimpleApnsPayloadBuilder();
payloadBuilder.setAlertTitle("xxxx");
payloadBuilder.setAlertBody("This is test message");
SimpleApnsPushNotification pushNotification = new SimpleApnsPushNotification(messageDto.getDeviceId(),
"com.np.gotprint", payloadBuilder.build());
apnsClient.sendNotification(pushNotification).get();
System.out.println("send successfully");
但我收到以下错误 -
Caused by: io.netty.channel.AbstractChannel$AnnotatedSocketException: Network is unreachable: api.sandbox.push.apple.com/2620:149:208:304:0:0:0:1f:443
Caused by: java.net.SocketException: Network is unreachable
at sun.nio.ch.Net.connect0(Native Method)
at sun.nio.ch.Net.connect(Net.java:454)
at sun.nio.ch.Net.connect(Net.java:446)
at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:648)
at io.netty.util.internal.SocketUtils$3.run(SocketUtils.java:83)
at io.netty.util.internal.SocketUtils$3.run(SocketUtils.java:80)
at java.security.AccessController.doPrivileged(Native Method)
at io.netty.util.internal.SocketUtils.connect(SocketUtils.java:80)
注意 - 当我在本地运行代码时,它工作正常,当我在临时环境中部署时抛出错误。
我在这里做错了什么吗?
基于您得到的异常
“网络无法访问:api.sandbox.push.apple.com/2620:149:208:304:0:0:0:1f:443”,
您似乎正在尝试使用 IPv6,但您的网络不支持它。 您可以使用以下系统属性强制 IPv4 首选项。
-Djava.net.preferIPv4Stack=true.