如何在调用APNS的API时向其发送推送证书



一直在使用我的服务器应用程序(Spring Boot应用程序(的apns推送证书发送推送通知。Apple在这里讨论了我们如何使用证书发送推送的概述,但没有关于TLS通信的技术细节(特别是关于我如何将推送证书发送到APNS并进行API调用(。

有人有参考资料或文章吗?

由于您提到了您在Spring Boot应用程序上的工作,我假设您使用的是Java和Maven。

我们的后端也是用Java编写的,我最近通过实现Pushy更新了我们的推送通知代码。使用经过战斗测试并定期更新的库比编写自己的库容易得多。我使用pushy在一个相当弱的服务器上每天发送大约10万个推送通知,并且从未遇到过任何问题

实现起来相当简单。他们的自述文件非常有用,但摘要是作为依赖项添加到pom.xml文件中的:

<dependency>
<groupId>com.eatthepath</groupId>
<artifactId>pushy</artifactId>
<version>0.13.11</version>
</dependency>  

下面是一个使用README中被盗代码的用例的简单示例,不过我添加了注释:

// Add these lines as class properties  
// This creates the object that handles the sending of the push notifications.
// Use ApnsClientBuilder.PRODUCTION_APNS_HOST to send pushes to App Store apps
final ApnsClient apnsClient = new ApnsClientBuilder()
.setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST)
.setClientCredentials(new File("/path/to/certificate.p12"), "p12-file-password")
.build();  
// The push notification
final SimpleApnsPushNotification pushNotification;  
// In some method:  
// Creating the object that builds the APNs payload
final ApnsPayloadBuilder payloadBuilder = new ApnsPayloadBuilder();  
// setting the text of the push
payloadBuilder.setAlertBody("Example!");  
// Builds the anps payload for the push notification
final String payload = payloadBuilder.build();  
// The push token of the device you're sending the push to
final String token = TokenUtil.sanitizeTokenString("<efc7492 bdbd8209>");  
// Creating the push notification object using the push token, your app's bundle ID, and the APNs payload
pushNotification = new SimpleApnsPushNotification(token, "com.example.myApp", payload);  
try {
// Asking the APNs client to send the notification 
// and creating the future that will return the status 
// of the push after it's sent.  
// (It's a long line, sorry)
final PushNotificationFuture<SimpleApnsPushNotification, PushNotificationResponse<SimpleApnsPushNotification>> sendNotificationFuture = apnsClient.sendNotification(pushNotification);  
// getting the response from APNs 
final PushNotificationResponse<SimpleApnsPushNotification> pushNotificationResponse = sendNotificationFuture.get();
if (pushNotificationResponse.isAccepted()) {
System.out.println("Push notification accepted by APNs gateway.");
} else {
System.out.println("Notification rejected by the APNs gateway: " +
pushNotificationResponse.getRejectionReason());
if (pushNotificationResponse.getTokenInvalidationTimestamp() != null) {
System.out.println("t…and the token is invalid as of " +
pushNotificationResponse.getTokenInvalidationTimestamp());
}
}
} catch (final ExecutionException e) {
System.err.println("Failed to send push notification.");
e.printStackTrace();
}

最新更新