-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectHawksnestMessagingServer.js
52 lines (46 loc) · 1.35 KB
/
ProjectHawksnestMessagingServer.js
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
// Initialisation
var admin = require("firebase-admin");
var serviceAccount = require("./project-hawksnest-firebase-adminsdk-ff34s-2ea5bd7319.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://project-hawksnest.firebaseio.com/",
databaseAuthVariableOverride: {
uid: "messagingServer"
}
});
var db = admin.database();
var attractionsRef = db.ref("attractions");
var refreshRate;
// Listen for database changes
attractionsRef.on("child_changed", function(snapshot) {
if (snapshot.val().open) {
console.log("\n"+ new Date);
send(snapshot.ref.key, snapshot.val().waitingTime);
}
});
db.ref("refreshRate").on("value", function(snapshot) {
refreshRate = snapshot.val();
});
// Send a message to all topics
function send(attraction, waitingTime) {
// Send a message to devices subscribed to the provided topic.
var payload = {
notification: {
title: attraction.toString(),
body: waitingTime.toString()
}
};
var options = {
priority: "high",
timeToLive: refreshRate
};
admin.messaging().sendToTopic(attraction, payload, options)
.then(function(response) {
// See the MessagingTopicResponse reference documentation for the
// contents of response.
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
}