aboutsummaryrefslogtreecommitdiff
path: root/juick-notifications/src/main/java/com/juick/components/FirebaseManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-notifications/src/main/java/com/juick/components/FirebaseManager.java')
-rw-r--r--juick-notifications/src/main/java/com/juick/components/FirebaseManager.java90
1 files changed, 90 insertions, 0 deletions
diff --git a/juick-notifications/src/main/java/com/juick/components/FirebaseManager.java b/juick-notifications/src/main/java/com/juick/components/FirebaseManager.java
new file mode 100644
index 00000000..54e7c97a
--- /dev/null
+++ b/juick-notifications/src/main/java/com/juick/components/FirebaseManager.java
@@ -0,0 +1,90 @@
+package com.juick.components;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.android.gcm.server.*;
+import com.juick.ExternalToken;
+import com.juick.User;
+import com.juick.service.component.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class FirebaseManager implements NotificationListener {
+ private static Logger logger = LoggerFactory.getLogger(FirebaseManager.class);
+ @Inject
+ ObjectMapper jsonMapper;
+ @Value("${gcm_key:}")
+ private String gcmKey;
+ @Inject
+ private NotificationsManager notificationsManager;
+
+ private Sender GCMSender;
+
+ @PostConstruct
+ public void initialize() {
+ GCMSender = new Sender(gcmKey, Endpoint.GCM);
+ }
+
+ @Override
+ public void processMessageEvent(MessageEvent messageEvent) {
+ com.juick.Message jmsg = messageEvent.getMessage();
+ List<User> users = messageEvent.getUsers();
+ // GCM
+ List<String> regids = users.stream().flatMap(u -> u.getTokens().stream()).filter(d -> d.getType().equals("gcm"))
+ .map(ExternalToken::getToken).collect(Collectors.toList());
+ if (!regids.isEmpty()) {
+ try {
+ String json = jsonMapper.writeValueAsString(jmsg);
+ logger.info(json);
+ Message message = new Message.Builder().addData("message", json).build();
+ MulticastResult result = GCMSender.send(message, regids, 3);
+ List<Result> results = result.getResults();
+ for (int i = 0; i < results.size(); i++) {
+ Result currentResult = results.get(i);
+ logger.info("RES {}: {}", i, currentResult);
+ List<String> errorCodes = Arrays.asList(Constants.ERROR_MISMATCH_SENDER_ID, Constants.ERROR_NOT_REGISTERED);
+ if (errorCodes.contains(currentResult.getErrorCodeName())) {
+ // assuming results are in order of regids
+ // http://stackoverflow.com/a/11594531/1097384
+ String currentId = regids.get(i);
+ logger.info("{} is scheduled to remove", currentId);
+ notificationsManager.addInvalidGCMToken(currentId);
+ }
+ }
+ } catch (IOException ex) {
+ logger.error(ex.getMessage(), ex);
+ } catch (IllegalArgumentException err) {
+ logger.warn("Android: Invalid API Key", err);
+ }
+ } else {
+ logger.info("GMS: no recipients");
+ }
+ }
+
+ @Override
+ public void processSubscribeEvent(SubscribeEvent subscribeEvent) {
+
+ }
+
+ @Override
+ public void processLikeEvent(LikeEvent likeEvent) {
+
+ }
+
+ @Override
+ public void processPingEvent(PingEvent pingEvent) {
+
+ }
+
+ @Override
+ public void processMessageReadEvent(MessageReadEvent messageReadEvent) {
+
+ }
+}