-
Notifications
You must be signed in to change notification settings - Fork 0
/
FCMNotificationManager.java
58 lines (50 loc) · 1.86 KB
/
FCMNotificationManager.java
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
package dissertation.backend.notification;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.*;
import dissertation.backend.database.Controller;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
public class FCMNotificationManager {
private final static Logger logger = LogManager.getLogger(FCMNotificationManager.class);
static {
try {
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.getApplicationDefault())
.setProjectId("dissertation-31cc0")
.build();
FirebaseApp.initializeApp(options);
} catch (IOException e) {
logger.error(e.getMessage());
}
}
public static void sendNotification(String message, String token) {
Message fcmMessage = Message.builder()
.putData("he-server-message", message)
.setApnsConfig(ApnsConfig.builder().setAps(Aps.builder().setContentAvailable(true).build()).build())
.setToken(token)
.build();
try {
FirebaseMessaging.getInstance().send(fcmMessage);
} catch (FirebaseMessagingException e) {
logger.error(e.getMessage());
}
}
public static void sendContactNotification(String userId, String endIsolation) {
String token = Controller.getTokenForUser(userId);
if (token == null) {
logger.error("Could not find token for user " + userId);
}
sendNotification(endIsolation, token);
}
public static void sendDataNotifications() {
for (String token : Controller.getInfectedUsersThatNeedToHalfDecrypt()) {
sendNotification("new partial data", token);
}
for (String token : Controller.getUsersThatNeedToDownloadDistances()) {
sendNotification("new data", token);
}
}
}