-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (41 loc) · 2.08 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* eslint-disable promise/no-nesting */
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.firestore.document('/Users/{user_id}/Notifications/{notification_id}').onWrite((data, context) => {
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
return admin.firestore().collection("Users").doc(user_id).collection("Notifications").doc(notification_id).get().then(response => {
const from_user_id = response.data().from;
const from_message = response.data().message;
// console.log(from_user_id);
const from_data = admin.firestore().collection("Users").doc(from_user_id).get();
const to_data = admin.firestore().collection("Users").doc(user_id).get();
// const message = admin.firestore().doc('/Users/{user_id}/Notifications/{notification_id}/').onSnapshot();
// console.log(message);
// eslint-disable-next-line promise/no-nesting
return Promise.all([from_data, to_data]).then(result => {
const from_name = result[0].data().name;
const to_name = result[1].data().name;
const token_id = result[1].data().token_id;
// eslint-disable-next-line no-unreachable
// const message = admin.database().ref(`/Users/{user_id}/Notifications/{notification_id}/message`).once('value');
const payload = {
notification: {
title: from_name,
body: from_message,
icon: "default",
click_action: "in.tvac.akshaye.lapitchat_TARGET_NOTIFICATION"
},
data: {
from_user_id: from_user_id
}
};
// eslint-disable-next-line promise/always-return
return admin.messaging().sendToDevice(token_id, payload).then(result => {
console.log("Notification Send.");
});
});
});
});