gpt4 book ai didi

ios - 如何在调用 API 时向 APNS 发送推送证书?

转载 作者:行者123 更新时间:2023-12-01 19:34:30 32 4
gpt4 key购买 nike

从我的服务器应用程序(Spring Boot 应用程序)使用 apns 推送证书发送推送通知时被卡住了。苹果讨论 here关于我们如何发送带有证书的推送的概述,但没有关于 TLS 通信的技术细节(特别是关于如何将我的推送证书发送到 APNS 并进行 API 调用)。

任何人有任何引用或文章?

最佳答案

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

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

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

<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();
}

关于ios - 如何在调用 API 时向 APNS 发送推送证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60620051/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com