diff options
Diffstat (limited to 'juick-notifications/src/main/java/com/juick/components/Notifications.java')
-rw-r--r-- | juick-notifications/src/main/java/com/juick/components/Notifications.java | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/juick-notifications/src/main/java/com/juick/components/Notifications.java b/juick-notifications/src/main/java/com/juick/components/Notifications.java index f0ab3b81..575f7460 100644 --- a/juick-notifications/src/main/java/com/juick/components/Notifications.java +++ b/juick-notifications/src/main/java/com/juick/components/Notifications.java @@ -21,12 +21,15 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.android.gcm.server.*; import com.juick.DeviceRegistration; import com.juick.User; -import com.notnoop.apns.APNS; -import com.notnoop.apns.ApnsService; +import com.turo.pushy.apns.ApnsClient; +import com.turo.pushy.apns.PushNotificationResponse; +import com.turo.pushy.apns.util.ApnsPayloadBuilder; +import com.turo.pushy.apns.util.SimpleApnsPushNotification; import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.text.StringEscapeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpMethod; import org.springframework.web.client.RestTemplate; @@ -37,6 +40,8 @@ import javax.annotation.PostConstruct; import javax.inject.Inject; import java.io.IOException; import java.util.*; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; import java.util.stream.Collectors; /** @@ -57,11 +62,14 @@ public class Notifications implements NotificationClientListener, AutoCloseable private final Set<String> invalidGCMTokens = Collections.synchronizedSet(new HashSet<>()); private final Set<String> invalidMPNSTokens = Collections.synchronizedSet(new HashSet<>()); + private final Set<String> invalidAPNSTokens = Collections.synchronizedSet(new HashSet<>()); @Inject private MPNSClient mpnsClient; @Inject - private ApnsService apns; + private ApnsClient apns; + @Value("${ios_app_id:}") + private String topic; @PostConstruct public void init() throws IOException { @@ -164,10 +172,27 @@ public class Notifications implements NotificationClientListener, AutoCloseable List<String> tokens = users.stream().flatMap(u -> u.getDevices().stream()).filter(d -> d.getType().equals("apns")) .map(DeviceRegistration::getToken).collect(Collectors.toList()); if (!tokens.isEmpty()) { + ApnsPayloadBuilder apnsPayloadBuilder = new ApnsPayloadBuilder(); + String payload = apnsPayloadBuilder.setAlertTitle("@" + jmsg.getUser().getName()) + .setAlertBody(jmsg.getText()).buildWithDefaultMaximumLength(); for (String token : tokens) { - String payload = APNS.newPayload().alertTitle("@" + jmsg.getUser().getName()).alertBody(jmsg.getText()).build(); - logger.info("APNS: {}", token); - apns.push(token, payload); + final Future<PushNotificationResponse<SimpleApnsPushNotification>> notification + = apns.sendNotification(new SimpleApnsPushNotification(token, topic, payload)); + try { + final PushNotificationResponse<SimpleApnsPushNotification> pushNotificationResponse + = notification.get(); + if (pushNotificationResponse.isAccepted()) { + logger.info("APNS accepted: {}", token); + } else { + String reason = pushNotificationResponse.getRejectionReason(); + logger.info("APNS rejected: {}", reason); + if (reason.equals("BadDeviceToken")) { + invalidAPNSTokens.add(token); + } + } + } catch (final ExecutionException | InterruptedException ex) { + logger.info("APNS exception", ex); + } } } else { logger.info("APNS: no recipients"); @@ -182,6 +207,7 @@ public class Notifications implements NotificationClientListener, AutoCloseable @Override public void close() throws Exception { + apns.close(); if (xmpp != null) xmpp.close(); @@ -227,4 +253,8 @@ public class Notifications implements NotificationClientListener, AutoCloseable break; } } + + public Set<String> getInvalidAPNSTokens() { + return invalidAPNSTokens; + } } |