aboutsummaryrefslogtreecommitdiff
path: root/juick-notifications/src/main/java/com
diff options
context:
space:
mode:
Diffstat (limited to 'juick-notifications/src/main/java/com')
-rw-r--r--juick-notifications/src/main/java/com/juick/components/FirebaseManager.java79
1 files changed, 52 insertions, 27 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 24bb43d1..a150c010 100644
--- a/juick-notifications/src/main/java/com/juick/components/FirebaseManager.java
+++ b/juick-notifications/src/main/java/com/juick/components/FirebaseManager.java
@@ -1,7 +1,13 @@
package com.juick.components;
+import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
-import com.google.android.gcm.server.*;
+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;
import com.juick.service.component.*;
@@ -12,7 +18,7 @@ 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.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;
@@ -22,14 +28,22 @@ public class FirebaseManager implements NotificationListener {
ObjectMapper jsonMapper;
@Value("${gcm_key:}")
private String gcmKey;
+ @Value("${fcm_database_url:}")
+ private String fcmDatabaseUrl;
@Inject
private NotificationsManager notificationsManager;
- private Sender GCMSender;
-
@PostConstruct
- public void initialize() {
- GCMSender = new Sender(gcmKey, Endpoint.GCM);
+ public void initialize() throws IOException {
+ InputStream serviceAccount =
+ ClassLoader.class.getResourceAsStream("serviceAccount.json");
+
+ FirebaseOptions options = new FirebaseOptions.Builder()
+ .setCredentials(GoogleCredentials.fromStream(serviceAccount))
+ .setDatabaseUrl(fcmDatabaseUrl)
+ .build();
+
+ FirebaseApp.initializeApp(options);
}
@Override
@@ -40,29 +54,40 @@ public class FirebaseManager implements NotificationListener {
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()) {
+ String json = null;
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);
+ json = jsonMapper.writeValueAsString(jmsg);
+ } catch (JsonProcessingException e) {
+ logger.warn("JSON exception", e);
}
+ logger.info(json);
+ Message.Builder messageBuilder = Message.builder()
+ .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);
+ /*
+ 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);
+ }
+ }*/
+ }
+
+ });
} else {
logger.info("GMS: no recipients");
}