forked from Jsaiao/tutorial-chat-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (30 loc) · 823 Bytes
/
index.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
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.pushes = functions.firestore
.document('notifications/{token}')
.onCreate((snap, context) => {
const document = snap.data();
console.log('document is', document);
var registrationToken = context.params.token;
var message = {
data: {
title: document.fromName,
body: document.text,
sender: document.fromId
},
token: registrationToken
}
admin.messaging().send(message)
.then((response) => {
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
})
admin.firestore()
.collection("notifications")
.doc(registrationToken)
.delete();
return Promise.resolve(0);
});