aboutsummaryrefslogtreecommitdiff
path: root/juick-notifications/src/main/java/com/juick/components/FirebaseManager.java
blob: 1feee45872c1de54ac7880bb455d1e45f452fb1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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;
import com.juick.service.component.*;
import org.slf4j.Logger;
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.util.List;
import java.util.stream.Collectors;

public class FirebaseManager implements NotificationListener {
    private static Logger logger = LoggerFactory.getLogger(FirebaseManager.class);
    @Inject
    ObjectMapper jsonMapper;
    @Value("${fcm_database_url:}")
    private String fcmDatabaseUrl;
    @Inject
    private NotificationsManager notificationsManager;
    @Inject
    private TaskExecutor taskExecutor;

    @PostConstruct
    public void initialize() throws IOException {
        Resource serviceAccount =
                new ClassPathResource("serviceAccount.json");

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount.getInputStream()))
                .setDatabaseUrl(fcmDatabaseUrl)
                .build();

        FirebaseApp.initializeApp(options);
    }

    @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()) {
            String json = null;
            try {
                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);
                ApiFuture<String> response = FirebaseMessaging.getInstance().sendAsync(messageBuilder.build());
                ApiFutures.addCallback(response, new ApiFutureCallback<String>() {
                    @Override
                    public void onFailure(Throwable t) {
                        if (t instanceof FirebaseMessagingException) {
                            FirebaseMessagingException e = (FirebaseMessagingException) t;
                            logger.warn("FirebaseMessaging error: {}", e.getErrorCode());
                            if (e.getErrorCode().equals("invalid-argument")
                                    || e.getErrorCode().equals("registration-token-not-registered")
                                    || e.getErrorCode().equals("invalid-registration-token")) {
                                // invalid token
                                logger.info("{} is scheduled to remove", token);
                                notificationsManager.addInvalidGCMToken(token);
                            } else {
                                logger.warn("Unhandled FirebaseMessaging exception", t);
                            }
                        } else {
                            logger.warn("Unhandled FCM exception", t);
                        }
                    }

                    @Override
                    public void onSuccess(String result) {
                        logger.info("Successfully sent message: " + result);
                    }
                }, taskExecutor);
            });
        } 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) {

    }

    @Override
    public void processTopEvent(TopEvent topEvent) {

    }
}