1

I need to create backend in Spring Boot for push notifications for iOS, but I only came across 3 libraries: java-apns, javapns and pushy. The first two haven't been worked on for several years, and pushy is still in 0.x.x versions. Is it better to use Firebase Cloud Messaging for this? They have a complete solution and it's easy to implement with Spring Boot.

0

I'm using Pushy in many projects (including one with Spring Boot); don't let the 0.x version number fool you. It's maintained on a regular basis and (very handy) supports token-based connection with authorization keys (.p8 files) which (if you choose so) never expire (the old .p12 certificates need to be renewed each year).

If you use Maven, it's as simple as

  1. adding the dependency:
<dependency>
    <groupId>com.turo</groupId>
    <artifactId>pushy</artifactId>
    <version>0.13.8</version>
</dependency>
  1. creating the APNS client
APNS_CLIENT = new ApnsClientBuilder().setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST).setSigningKey(
    ApnsSigningKey.loadFromInputStream(new FileInputStream("APN.p8"), teamID, keyID))
    .build()
  1. creating the payload
ApnsPayloadBuilder payloadBuilder = new ApnsPayloadBuilder();
payloadBuilder.setAlertBody("While processing " + site + " an exception was thrown: " + e.getMessage());
payloadBuilder.setAlertTitle("❌️ Exception thrown");
payloadBuilder.setSound("default");
String payload = payloadBuilder.buildWithDefaultMaximumLength();
  1. and sending the notification:
SimpleApnsPushNotification pushNotification = new SimpleApnsPushNotification(deviceToken, applicationIdentifier, payload);
PushNotificationFuture<SimpleApnsPushNotification, PushNotificationResponse<SimpleApnsPushNotification>> sendNotificationFuture = APNS_CLIENT.sendNotification(pushNotification);
PushNotificationResponse<SimpleApnsPushNotification> pushNotificationResponse = sendNotificationFuture.get();
if (!pushNotificationResponse.isAccepted()) {
    System.err.println("Notification rejected by the APNs gateway: " + pushNotificationResponse.getRejectionReason());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.