-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (43 loc) · 1.73 KB
/
index.js
File metadata and controls
52 lines (43 loc) · 1.73 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
// getting started: https://firebase.google.com/docs/functions/get-started
// functions: https://github.com/firebase/functions-samples
// topic messaging: https://firebase.google.com/docs/cloud-messaging/android/topic-messaging
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:pushId/original
exports.addMessage = functions.https.onRequest(async (req, res) => {
// Grab the text parameter.
const original = req.query.text;
// Push the new message into the Realtime Database using the Firebase Admin SDK.
const snapshot = await admin.database().ref('/messages').push({original: original});
// Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
res.redirect(303, snapshot.ref.toString());
});
exports.sendNotification = functions.https.onRequest(async (req, res) => {
if (req.method !== 'GET') {
return res.status(403).send('Forbidden');
}
const topic = req.query.text;
let message = {
data: {
score: '850',
time: '2:45'
},
notification: {
title: "Hello" + topic,
body: 'This is a test.'
}
};
return admin.messaging().sendToTopic(topic, message)
.then((response) => {
console.log('Successfully sent message:', response);
res.status(200).send('push successfully');
return true;
})
.catch((error) => {
console.log('Error sending message:', error);
res.status(500).send('Error');
return false;
});
});