-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleCloudNotificationService.java
110 lines (104 loc) · 3.77 KB
/
GoogleCloudNotificationService.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
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
/*
* Copyright (C) 2013 UniCoPA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package unicopa.copa.server.notification;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import unicopa.copa.base.UserSettings;
import unicopa.copa.base.event.SingleEvent;
import unicopa.copa.base.event.SingleEventUpdate;
import unicopa.copa.server.CopaSystemContext;
import unicopa.copa.server.database.ObjectNotFoundException;
import unicopa.copa.server.gcm.GoogleCloudMessagingService;
/**
* This NotificationService informs Android devices about updates by sending
* Google Cloud Messages.
*
* @author Felix Wiemuth, Philip Wendland
*/
public class GoogleCloudNotificationService extends NotificationService {
private GoogleCloudMessagingService gcmService;
public static final Logger LOG = Logger
.getLogger(GoogleCloudNotificationService.class.getName());
public GoogleCloudNotificationService(CopaSystemContext context) {
super(context);
this.gcmService = new GoogleCloudMessagingService(context);
}
/**
* Notify all users on their Android devices where the user has subscribed
* to get the specified SingleEventUpdate and has added the GCM key for the
* device to his UserSettings.
*
* @param update
* the SingleEventUpdate to inform about
*/
@Override
public void notifyClients(SingleEventUpdate update) {
try {
// determine the single event ID
// Note: We have to get the Event-ID from the old single event,
// because
// the new single event might be null
int eventID;
if (!update.isSingleEventCreation()) {
int oldID = update.getOldSingleEventID();
SingleEvent oldSingleEvent = super.getContext().getDbservice()
.getSingleEvent(oldID);
eventID = oldSingleEvent.getEventID();
} else {
eventID = update.getUpdatedSingleEvent().getEventID();
}
// determine the users that should be informed about the update
List<Integer> subUsers = super.getContext().getDbservice()
.getSubscribedUserIDs(eventID);
// determine GCM keys of the users
Set<String> gcmKeys = new HashSet<>();
for (Integer userID : subUsers) {
UserSettings settings = super.getContext().getDbservice()
.getUserSettings(userID);
Set<String> usrKeys = settings.getGCMKeys();
gcmKeys.addAll(usrKeys);
}
// send using service
this.gcmService.notify(gcmKeys, "SINGLE_EVENT_UPDATE");
} catch (ObjectNotFoundException ex) {
LOG.log(Level.SEVERE, "Database error.", ex);
}
}
/**
* Uses GoogleCloudMessagingService to notify the client.
*
* The message will be sent to every device which is registered.
*
* @param event
* @param userID
*/
@Override
public void notifyClient(NotificationEvent event, int userID) {
try {
UserSettings settings = super.getContext().getDbservice()
.getUserSettings(userID);
Set<String> usrKeys = settings.getGCMKeys();
// send using service
this.gcmService.notify(usrKeys, event.toString());
} catch (ObjectNotFoundException ex) {
LOG.log(Level.SEVERE, "Database error.", ex);
}
}
}