aboutsummaryrefslogtreecommitdiff
path: root/juick-notifications/src/main/java/com/juick/components/Notifications.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-05-10 13:00:13 +0300
committerGravatar Vitaly Takmazov2018-05-10 13:00:13 +0300
commit98eeee9a6e4b008fed5ed1aab58209b252d86b10 (patch)
tree90322d894a0fa2b2987d3c6161efccd2344ec9ec /juick-notifications/src/main/java/com/juick/components/Notifications.java
parentfb540605626e1c7bb1544a518a254e6a820afcb3 (diff)
notifications: update APNS badge number on message read event
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.java82
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);