From a722f11f2bd785b625c8ad92a1e7559352f55778 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 26 Oct 2018 10:26:51 +0300 Subject: notifications: do not send to sandboxed user --- .../java/com/juick/components/APNSManager.java | 83 ++++++++++------------ 1 file changed, 38 insertions(+), 45 deletions(-) (limited to 'juick-notifications') diff --git a/juick-notifications/src/main/java/com/juick/components/APNSManager.java b/juick-notifications/src/main/java/com/juick/components/APNSManager.java index 314a0bd1..4e5fc01a 100644 --- a/juick-notifications/src/main/java/com/juick/components/APNSManager.java +++ b/juick-notifications/src/main/java/com/juick/components/APNSManager.java @@ -40,11 +40,11 @@ public class APNSManager implements NotificationListener { private String teamId; @Value("${ios_key_id:}") private String keyId; + @Value("${ios_sandbox_user_id:}") + private Integer sandboxUserId; @Inject private NotificationsManager notificationsManager; - private ApnsClient sandboxApns; - @PostConstruct public void initialize() throws NoSuchAlgorithmException, InvalidKeyException, IOException { apns = new ApnsClientBuilder() @@ -52,11 +52,6 @@ public class APNSManager implements NotificationListener { .setSigningKey(ApnsSigningKey.loadFromPkcs8File(new File(p8key), teamId, keyId)) .build(); - sandboxApns = new ApnsClientBuilder() - .setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST) - .setSigningKey(ApnsSigningKey.loadFromPkcs8File(new File(p8key), - teamId, keyId)) - .build(); } @Override public void processMessageEvent(MessageEvent messageEvent) { @@ -71,14 +66,12 @@ public class APNSManager implements NotificationListener { String post = PlainTextFormatter.formatPost(jmsg); String[] parts = post.split("\n", 2); apnsPayloadBuilder.setAlertTitle(parts[0]).setAlertBody(parts[1]); - users.forEach( user -> { + users.stream().filter(u -> u.getUid() != sandboxUserId).forEach( user -> { apnsPayloadBuilder.setBadgeNumber(user.getUnreadCount()); String payload = apnsPayloadBuilder.buildWithDefaultMaximumLength(); user.getTokens().stream().filter(t -> t.getType().equals("apns")) .map(ExternalToken::getToken).forEach(token -> { - Future> notification = - user.getUid() == 3694 ? sandboxApns.sendNotification( - new SimpleApnsPushNotification(token, topic, payload)) : apns.sendNotification( + Future> notification = apns.sendNotification( new SimpleApnsPushNotification(token, topic, payload)); notification.addListener((PushNotificationResponseListener) future -> { if (future.isSuccess()) { @@ -109,47 +102,47 @@ public class APNSManager implements NotificationListener { @Override public void processMessageReadEvent(MessageReadEvent messageReadEvent) { User user = messageReadEvent.getUser(); - ApnsPayloadBuilder apnsPayloadBuilder = new ApnsPayloadBuilder(); - apnsPayloadBuilder.setBadgeNumber(user.getUnreadCount()); - String payload = apnsPayloadBuilder.buildWithDefaultMaximumLength(); - user.getTokens().stream().filter(t -> t.getType().equals("apns")) - .map(ExternalToken::getToken).forEach(token -> { - Future> notification = - user.getUid() == 3694 ? sandboxApns.sendNotification( - new SimpleApnsPushNotification(token, topic, payload)) : apns.sendNotification( - new SimpleApnsPushNotification(token, topic, payload)); - notification.addListener((PushNotificationResponseListener) future -> { - if (future.isSuccess()) { - processAPNSResponse(token, future.getNow()); - } else { - logger.warn("APNS error ", future.cause()); - } + if (user.getUid() != sandboxUserId) { + ApnsPayloadBuilder apnsPayloadBuilder = new ApnsPayloadBuilder(); + apnsPayloadBuilder.setBadgeNumber(user.getUnreadCount()); + String payload = apnsPayloadBuilder.buildWithDefaultMaximumLength(); + user.getTokens().stream().filter(t -> t.getType().equals("apns")) + .map(ExternalToken::getToken).forEach(token -> { + Future> notification = apns.sendNotification( + new SimpleApnsPushNotification(token, topic, payload)); + notification.addListener((PushNotificationResponseListener) future -> { + if (future.isSuccess()) { + processAPNSResponse(token, future.getNow()); + } else { + logger.warn("APNS error ", future.cause()); + } + }); }); - }); + } } @Override public void processTopEvent(TopEvent topEvent) { Message message = topEvent.getMessage(); - ApnsPayloadBuilder apnsPayloadBuilder = new ApnsPayloadBuilder(); - message.getUser().getTokens().stream().filter(t -> t.getType().equals("apns")) - .map(ExternalToken::getToken).forEach( token -> { - String payload = apnsPayloadBuilder.setAlertTitle("Top").setAlertBody("Your message became popular!") - .addCustomProperty("mid", message.getMid()) - .addCustomProperty("uname", message.getUser().getName()) - .buildWithDefaultMaximumLength(); - Future> notification = - message.getUser().getUid() == 3694 ? sandboxApns.sendNotification( - new SimpleApnsPushNotification(token, topic, payload)) : apns.sendNotification( - new SimpleApnsPushNotification(token, topic, payload)); - notification.addListener((PushNotificationResponseListener) future -> { - if (future.isSuccess()) { - processAPNSResponse(token, future.getNow()); - } else { - logger.warn("APNS error ", future.cause()); - } + if (message.getUser().getUid() != sandboxUserId) { + ApnsPayloadBuilder apnsPayloadBuilder = new ApnsPayloadBuilder(); + message.getUser().getTokens().stream().filter(t -> t.getType().equals("apns")) + .map(ExternalToken::getToken).forEach(token -> { + String payload = apnsPayloadBuilder.setAlertTitle("Top").setAlertBody("Your message became popular!") + .addCustomProperty("mid", message.getMid()) + .addCustomProperty("uname", message.getUser().getName()) + .buildWithDefaultMaximumLength(); + Future> notification = apns.sendNotification( + new SimpleApnsPushNotification(token, topic, payload)); + notification.addListener((PushNotificationResponseListener) future -> { + if (future.isSuccess()) { + processAPNSResponse(token, future.getNow()); + } else { + logger.warn("APNS error ", future.cause()); + } + }); }); - }); + } } @PreDestroy -- cgit v1.2.3