From dd0a790d2431c6022b255ea5d02ab8d995ec6a45 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 15 Oct 2018 16:48:20 +0300 Subject: notifications: async FCM --- .../java/com/juick/components/FirebaseManager.java | 30 +++++++++++++--------- 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'juick-notifications') diff --git a/juick-notifications/src/main/java/com/juick/components/FirebaseManager.java b/juick-notifications/src/main/java/com/juick/components/FirebaseManager.java index e2ffd9d5..6bab725a 100644 --- a/juick-notifications/src/main/java/com/juick/components/FirebaseManager.java +++ b/juick-notifications/src/main/java/com/juick/components/FirebaseManager.java @@ -2,11 +2,13 @@ package com.juick.components; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.api.core.ApiFuture; +import com.google.api.core.ApiFutureCallback; +import com.google.api.core.ApiFutures; import com.google.auth.oauth2.GoogleCredentials; import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseOptions; import com.google.firebase.messaging.FirebaseMessaging; -import com.google.firebase.messaging.FirebaseMessagingException; import com.google.firebase.messaging.Message; import com.juick.ExternalToken; import com.juick.User; @@ -16,11 +18,11 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; +import org.springframework.core.task.TaskExecutor; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.io.IOException; -import java.io.InputStream; import java.util.List; import java.util.stream.Collectors; @@ -28,12 +30,12 @@ public class FirebaseManager implements NotificationListener { private static Logger logger = LoggerFactory.getLogger(FirebaseManager.class); @Inject ObjectMapper jsonMapper; - @Value("${gcm_key:}") - private String gcmKey; @Value("${fcm_database_url:}") private String fcmDatabaseUrl; @Inject private NotificationsManager notificationsManager; + @Inject + private TaskExecutor taskExecutor; @PostConstruct public void initialize() throws IOException { @@ -67,13 +69,12 @@ public class FirebaseManager implements NotificationListener { .putData("message", json); regids.forEach(token -> { messageBuilder.setToken(token); - try { - String response = FirebaseMessaging.getInstance().send(messageBuilder.build()); - // Response is a message ID string. - logger.info("Successfully sent message: " + response); - } catch (FirebaseMessagingException e) { - logger.warn("Firebase exception", e); - /* + ApiFuture response = FirebaseMessaging.getInstance().sendAsync(messageBuilder.build()); + ApiFutures.addCallback(response, new ApiFutureCallback() { + @Override + public void onFailure(Throwable t) { + logger.warn("Firebase exception", t); + /* List results = result.getResults(); for (int i = 0; i < results.size(); i++) { Result currentResult = results.get(i); @@ -87,8 +88,13 @@ public class FirebaseManager implements NotificationListener { notificationsManager.addInvalidGCMToken(currentId); } }*/ - } + } + @Override + public void onSuccess(String result) { + logger.info("Successfully sent message: " + result); + } + }, taskExecutor); }); } else { logger.info("GMS: no recipients"); -- cgit v1.2.3