-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
33 lines (30 loc) · 833 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
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.fcmSend = functions.database
.ref('/messages/{userId}/{messageId}')
.onCreate(event => {
const message = event.after.val();
const userId = event.params.userId;
const payload = {
notification: {
title: message.title,
body: message.body,
icon: 'https://placeimg.com/250/250/people'
}
};
admin
.database()
.ref(`/fcmTokens/${userId}`)
.once('value')
.then(token => token.val())
.then(userFcmToken => {
return admin.messaging().sendToDevice(userFcmToken, payload);
})
.then(res => {
console.log('Sent Successfully', res);
})
.catch(err => {
console.log(err);
});
});