-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (44 loc) · 1.72 KB
/
index.js
File metadata and controls
60 lines (44 loc) · 1.72 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
//------THIS IS CODE FOR SENDING NOTIFICATION ON NEW FRIEND REQUEST--------
'use strict'
const functions = require('firebase-functions');
const admin=require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification =functions.database.
ref('/notifications/{user_id}/{notification_id}').onWrite(event => {
//--user at which friend request is sent--
const user_id=event.params.user_id;
const notification_id=event.params.notification_id;
console.log('We have a notification to send to : ', user_id);
if(!event.data.val()){
return console.log('A notification has been deleted from database : ', notification_id);
}
const fromUser = admin.database().
ref(`/notifications/${user_id}/${notification_id}`).once('value');
return fromUser.then(fromUserResult => {
const from_user_id = fromUserResult.val().from;
console.log('You have new notification from : ', from_user_id);
const userQuery = admin.database().
ref(`users/${from_user_id}/name`).once('value');
const deviceToken = admin.database().
ref(`/users/${user_id}/device_token`).once('value');
return Promise.all([userQuery,deviceToken]).then(result =>{
const userName = result[0].val();
const token_id = result[1].val();
const payload = {
notification: {
title: "New Friend Request",
body : `${userName} has sent you friend request`,
icon: "default",
click_action : "com.example.singhkshitiz.letschat_TARGET_NOTIFICATION"
},
data : {
from_user_id : from_user_id
}
};
return admin.messaging().sendToDevice(token_id,payload).then(response =>{
console.log('This was the notification feature');
return ;
});
});
});
});