-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (56 loc) · 2.15 KB
/
index.js
File metadata and controls
64 lines (56 loc) · 2.15 KB
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
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
/**
* Triggers when a user gets a new follower and sends a notification.
*
* Followers add a flag to `/followers/{followedUid}/{followerUid}`.
* Users save their device notification tokens to `/users/{followedUid}/notificationTokens/{notificationToken}`.
*/
exports.sendNotification = functions.database.ref('/Sessions/{session_name}/joinedUsers/{Uid}/state')
.onWrite((change, context) => {
const Uid = context.params.Uid;
const session_name = context.params.session_name;
if (!change.after.val()) {
return console.log('User with ', Uid, 'state save corrupted');
}
console.log('New state of user with UID : ', Uid, 'is', change.after.val());
const state = change.after.val();
if(state > 9){
return console.log('State is good!');
}
// Get the list of device notification tokens.
const getDeviceTokensPromise = admin.database()
.ref(`Students/${Uid}/token`).once('value');
const notification = {
time: '10',
status: 'Enabled',
comment: 'Attentiveness Alert',
state: `${state}`,
};
const alertPromise = admin.database().ref(`/Sessions/${session_name}/alerts/${Uid}/sentAlerts`).push().set(notification);
let token;
return Promise.all([getDeviceTokensPromise, alertPromise]).then(results => {
token = results[0];
console.log('FCM token is ', token.val());
// Notification details.
const payload = {
notification: {
title: 'Attentiveness Alert',
body: `Your state is ${state}`,
},
token: token.val()
};
// Send notifications to all tokens.
return admin.messaging().send(payload);
}).then((response) => {
const arr = [];
// Response is a message ID string.
console.log('Successfully sent message:', response);
return Promise.all(arr);
})
.catch((error) => {
console.log('Error sending message:', error);
});
});