diff options
author | 2018-10-15 16:48:20 +0300 | |
---|---|---|
committer | 2018-10-15 16:48:40 +0300 | |
commit | dd0a790d2431c6022b255ea5d02ab8d995ec6a45 (patch) | |
tree | 29182aaab26f1597f3d855e592068446b3701308 | |
parent | 76112fd865a3af137dad3e1f3aae2f660f9d83d4 (diff) |
notifications: async FCM
-rw-r--r-- | juick-notifications/src/main/java/com/juick/components/FirebaseManager.java | 30 |
1 files changed, 18 insertions, 12 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 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<String> response = FirebaseMessaging.getInstance().sendAsync(messageBuilder.build()); + ApiFutures.addCallback(response, new ApiFutureCallback<String>() { + @Override + public void onFailure(Throwable t) { + logger.warn("Firebase exception", t); + /* List<Result> 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"); |