diff options
Diffstat (limited to 'juick-notifications/src/main/java/com/juick')
-rw-r--r-- | juick-notifications/src/main/java/com/juick/components/Notifications.java | 82 |
1 files changed, 50 insertions, 32 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 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<User> 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<List<User>>() { }).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<List<User>>() { + }).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<List<User>>() { - }).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<List<User>>() { - }).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<List<User>>() { + }).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<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); - } - }); + .map(ExternalToken::getToken).forEach(token -> + processAPNSResponse(token, apns.sendNotification( + new SimpleApnsPushNotification(token, topic, payload)))); + }); + } + private void serviceMessageReceived(@Nonnull com.juick.Message jmsg) { + // iOS + List<User> users = rest.exchange(String.format("http://api.juick.com/notifications?uid=%d", + jmsg.getUser().getUid()), + HttpMethod.GET, null, new ParameterizedTypeReference<List<User>>() { + }).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<PushNotificationResponse<SimpleApnsPushNotification>> notification) { + 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); + } + } + public void addInvalidGCMToken(String token) { synchronized (invalidGCMTokens) { invalidGCMTokens.add(token); |