From 98eeee9a6e4b008fed5ed1aab58209b252d86b10 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 10 May 2018 13:00:13 +0300 Subject: notifications: update APNS badge number on message read event --- .../java/com/juick/components/Notifications.java | 82 +++++++++++++--------- 1 file changed, 50 insertions(+), 32 deletions(-) (limited to 'juick-notifications/src/main/java/com') 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 ea5362c1..4e6743d4 100644 --- a/juick-notifications/src/main/java/com/juick/components/Notifications.java +++ b/juick-notifications/src/main/java/com/juick/components/Notifications.java @@ -87,31 +87,31 @@ public class Notifications extends TextWebSocketHandler implements NotificationC private String topic; @PostConstruct - public void init() throws IOException { + public void init() { mpnsClient.setListener(this); closeFlag.set(false); } public void messageReceived(@Nonnull com.juick.Message jmsg) { + if (jmsg.isService()) { + serviceMessageReceived(jmsg); + } User pmTo = jmsg.getTo(); - final List users = new ArrayList<>(); if (MessageUtils.isPM(jmsg)) { users.addAll(rest.exchange(String.format("http://api.juick.com/notifications?uid=%d", pmTo.getUid()), HttpMethod.GET, null, new ParameterizedTypeReference>() { }).getBody()); + } else if (MessageUtils.isReply(jmsg)) { + users.addAll(rest.exchange(String.format("http://api.juick.com/notifications?uid=%d&mid=%d&rid=%d", + jmsg.getUser().getUid(), jmsg.getMid(), jmsg.getRid()), + HttpMethod.GET, null, new ParameterizedTypeReference>() { + }).getBody()); } else { - if (MessageUtils.isReply(jmsg)) { - users.addAll(rest.exchange(String.format("http://api.juick.com/notifications?uid=%d&mid=%d&rid=%d", - jmsg.getUser().getUid(), jmsg.getMid(), jmsg.getRid()), - HttpMethod.GET, null, new ParameterizedTypeReference>() { - }).getBody()); - } else { - users.addAll(rest.exchange(String.format("http://api.juick.com/notifications?uid=%s&mid=%s", - jmsg.getUser().getUid(), jmsg.getMid()), - HttpMethod.GET, null, new ParameterizedTypeReference>() { - }).getBody()); - } + users.addAll(rest.exchange(String.format("http://api.juick.com/notifications?uid=%s&mid=%s", + jmsg.getUser().getUid(), jmsg.getMid()), + HttpMethod.GET, null, new ParameterizedTypeReference>() { + }).getBody()); } // GCM @@ -192,28 +192,46 @@ public class Notifications extends TextWebSocketHandler implements NotificationC 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 { - final PushNotificationResponse 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); - } - }); + .map(ExternalToken::getToken).forEach(token -> + processAPNSResponse(token, apns.sendNotification( + new SimpleApnsPushNotification(token, topic, payload)))); + }); + } + private void serviceMessageReceived(@Nonnull com.juick.Message jmsg) { + // iOS + List users = rest.exchange(String.format("http://api.juick.com/notifications?uid=%d", + jmsg.getUser().getUid()), + HttpMethod.GET, null, new ParameterizedTypeReference>() { + }).getBody(); + ApnsPayloadBuilder apnsPayloadBuilder = new ApnsPayloadBuilder(); + 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 -> + processAPNSResponse(token, apns.sendNotification( + new SimpleApnsPushNotification(token, topic, payload)))); }); } + private void processAPNSResponse(String token, Future> notification) { + try { + final PushNotificationResponse 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); + } + } + public void addInvalidGCMToken(String token) { synchronized (invalidGCMTokens) { invalidGCMTokens.add(token); -- cgit v1.2.3