使用Pushy Java库发送iOS推送报No route to host错误求助
问题排查与替代方案
一、网络异常排查步骤
针对No route to host和DNS解析失败的问题,按以下顺序排查:
- 验证网络连通性:在运行代码的机器上执行
ping api.sandbox.push.apple.com或telnet api.sandbox.push.apple.com 443,确认能正常连通。如果不通,优先检查网络路由、防火墙规则。 - 检查DNS配置:用
nslookup api.sandbox.push.apple.com或dig api.sandbox.push.apple.com查看域名解析结果。若解析失败,尝试切换公共DNS(如8.8.8.8)测试,或在代码中为Pushy指定自定义DNS服务器。 - 确认防火墙/安全组规则:确保服务器出站规则允许访问443端口,且放行苹果APNS沙箱环境的IP段。
- 代理配置检查:若机器需通过代理访问外网,需在
ApnsClientBuilder中调用.setProxy()方法配置代理信息,否则请求无法正常发送。 - 证书有效性验证:虽然当前异常是网络问题,但仍需确认P12证书路径正确、密码无误、证书未过期且对应沙箱环境。
二、替代Java APNS库推荐
如果Pushy的问题无法快速解决,可考虑以下成熟库:
- Java-APNS(notnoop):老牌稳定库,支持HTTP/2协议,同时兼容证书和Token两种认证方式,文档完善,社区活跃度高。
- APNS2 Java:轻量级HTTP/2专属APNS客户端,代码简洁高效,适合对性能有要求的项目。
- Spring Cloud Apple Push Notification:适配Spring生态的模块,可快速集成到Spring Boot项目中,简化推送配置流程。
附:问题代码与异常信息
代码示例
String topic = "XXX"; ApnsClient apnsClient = new ApnsClientBuilder().setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST) .setClientCredentials(new File( "apns_a5400831-a2db-480b-9487-52cfe61fc4f0.p12"), "Password123!") .build(); ApnsPayloadBuilder payloadBuilder = new SimpleApnsPayloadBuilder(); String payload = payloadBuilder.buildMdmPayload(magic); String sanitizedToken = TokenUtil.sanitizeTokenString(deviceToken); try { SimpleApnsPushNotification pushNotification = new SimpleApnsPushNotification(sanitizedToken, topic, payload); PushNotificationResponse<SimpleApnsPushNotification> pushNotificationResponse = apnsClient .sendNotification(pushNotification).get(); if (pushNotificationResponse.isAccepted()) { if (logger.isDebugEnabled()) { logger.debug("testCheckin(String, String) - {}", "Push notification accepted by APNs gateway."); } } else { if (logger.isDebugEnabled()) { logger.debug("testCheckin(String, String) - {}", "Notification rejected by the APNs gateway: " + pushNotificationResponse.getRejectionReason()); } pushNotificationResponse.getTokenInvalidationTimestamp().ifPresent(timestamp -> { System.out.println(" …and the token is invalid as of " + timestamp); }); } } catch (Exception e) { logger.warn("testCheckin(String, String) - exception ignored", e); }
异常信息
15:55:58 WARN [com.eatthepath.pushy.apns.ApnsChannelPool] Failed to create channel: io.netty.resolver.dns.DnsResolveContext$SearchDomainUnknownHostException: Failed to resolve 'api.sandbox.push.apple.com' and search domain query for configured domains failed as well: [lan] at io.netty.resolver.dns.DnsResolveContext.finishResolve(DnsResolveContext.java:1047) ... Caused by: java.net.NoRouteToHostException: No route to host at java.base/sun.nio.ch.DatagramChannelImpl.send0(Native Method) ...
内容的提问来源于stack exchange,提问作者Shadowman
相关产品推荐
相关产品推荐

