Skip to content

Commit

Permalink
Merge branch 'master' into heroanimation
Browse files Browse the repository at this point in the history
  • Loading branch information
Deepanshu-Rohilla committed Jun 8, 2020
2 parents 9905b14 + 01958de commit 20359b1
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 146 deletions.
4 changes: 1 addition & 3 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
"source" : "functions"
}
}
46 changes: 46 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);


exports.myFunction = functions.firestore.document('chatroom/{chatroomid}/chats/{chatsId}').onCreate(async (snapshot, context) => {
var docId;
var tokens = [];
var usersChat = [];
// console.log(usersChat);
docId = context.params.chatroomid;
// console.log(docId);
var roomDetails = await admin.firestore().collection('chatroom').doc(docId).get();
// console.log(roomDetails);
usersChat = roomDetails.data().users;
// console.log(usersChat);
var i = 0;
// console.log(snapshot.data().userId);
while (i < usersChat.length) {
if(usersChat[i] != snapshot.data().userId){
var userDetails = await admin.firestore().collection('userdetails').doc(usersChat[i]).get();
tokens.push(userDetails.data().device_token);
}
// console.log('hello');
// console.log(usersChat.length);
i++;
};
// console.log(tokens);
var payload = {
notification: {
title: snapshot.data().name,
body: snapshot.data().text,
clickAction: 'FLUTTER_NOTIFICATION_CLICK'
}
}
try {
return admin.messaging().sendToDevice(tokens, payload);
// console.log('Notfication sent successfully');
} catch (err) {
console.log('Error sending Notification');
}
// return admin.messaging().sendToDevice(tokens, payload);

});


202 changes: 65 additions & 137 deletions functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 20359b1

Please sign in to comment.