From 6df778ed5299edc18bd65e2af8a5e0ae60b8191c Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 8 May 2018 23:49:08 +0300 Subject: notifications: add badge counter to APNS payload --- .../java/com/juick/components/Notifications.java | 28 ++++++++++------------ 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'juick-notifications/src/main/java/com/juick') 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 9f62fd4d..ea5362c1 100644 --- a/juick-notifications/src/main/java/com/juick/components/Notifications.java +++ b/juick-notifications/src/main/java/com/juick/components/Notifications.java @@ -182,17 +182,17 @@ public class Notifications extends TextWebSocketHandler implements NotificationC } /*** iOS ***/ - List tokens = users.stream().flatMap(u -> u.getTokens().stream()).filter(d -> d.getType().equals("apns")) - .map(ExternalToken::getToken).collect(Collectors.toList()); - if (!tokens.isEmpty()) { - ApnsPayloadBuilder apnsPayloadBuilder = new ApnsPayloadBuilder(); - apnsPayloadBuilder.addCustomProperty("mid", jmsg.getMid()); - apnsPayloadBuilder.addCustomProperty("uname", jmsg.getUser().getName()); - String post = PlainTextFormatter.formatPost(jmsg); - String[] parts = post.split("\n", 2); - String payload = apnsPayloadBuilder.setAlertTitle(parts[0]) - .setAlertBody(parts[1]).buildWithDefaultMaximumLength(); - for (String token : tokens) { + ApnsPayloadBuilder apnsPayloadBuilder = new ApnsPayloadBuilder(); + apnsPayloadBuilder.addCustomProperty("mid", jmsg.getMid()); + apnsPayloadBuilder.addCustomProperty("uname", jmsg.getUser().getName()); + String post = PlainTextFormatter.formatPost(jmsg); + String[] parts = post.split("\n", 2); + apnsPayloadBuilder.setAlertTitle(parts[0]).setAlertBody(parts[1]); + users.forEach( user -> { + apnsPayloadBuilder.setBadgeNumber(user.getUnreadCount()); + String payload = apnsPayloadBuilder.buildWithDefaultMaximumLength(); + user.getTokens().stream().filter(t -> t.getType().equals("apns")) + .map(ExternalToken::getToken).forEach(token -> { final Future> notification = apns.sendNotification(new SimpleApnsPushNotification(token, topic, payload)); try { @@ -210,10 +210,8 @@ public class Notifications extends TextWebSocketHandler implements NotificationC } catch (final ExecutionException | InterruptedException ex) { logger.info("APNS exception", ex); } - } - } else { - logger.info("APNS: no recipients"); - } + }); + }); } public void addInvalidGCMToken(String token) { -- cgit v1.2.3