Last active
April 20, 2021 19:40
-
-
Save mulieriq/5812419c989e48ecd80be94a0f9fd053 to your computer and use it in GitHub Desktop.
Video Call Notification : Cloud Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.sendVideoCallNotification = functions.firestore | |
.document(`personalvideocalls/{recepientId}`) | |
.onCreate((snap, context) => { | |
console.log("----------------start function--------------------"); | |
const doc = snap.data(); | |
console.log(doc); | |
const idFrom = doc.senderuid; | |
const idTo = doc.receiveruid; | |
//const contentMessage = doc.content | |
admin | |
.firestore() | |
.collection("users") | |
.where("userId", "==", idTo) | |
.get() | |
.then(querySnapshot => { | |
querySnapshot.forEach(userTo => { | |
console.log(`Found user to: ${userTo.data().userName}`); | |
console.log(`Found User to ${userTo.data().pushToken}`); | |
if (userTo.data().pushToken && userTo.data().userId !== idFrom) { | |
// Get info user from (sent) | |
admin | |
.firestore() | |
.collection("users") | |
.where("userId", "==", idFrom) | |
.get() | |
.then(querySnapshot2 => { | |
querySnapshot2.forEach(userFrom => { | |
console.log(`Found user from: ${userFrom.data().userName}`); | |
const payload = { | |
notification: { | |
title: "Incoming Video Call From:", | |
body: `${userFrom.data().userName}`, | |
click_action: 'FLUTTER_NOTIFICATION_CLICK', | |
badge: "1", | |
sound: "default" | |
}, | |
data: { | |
sendername: "Incoming Video Call From:", | |
message: userFrom.data().userName, | |
} | |
}; | |
// Let push to the target device | |
admin | |
.messaging() | |
.sendToDevice(userTo.data().pushToken, payload) | |
.then(response => { | |
console.log("Successfully sent message:", response); | |
}) | |
.catch(error => | |
console.log("Error sending message:", error) | |
); }); | |
}) | |
.catch(error => Console.log(error)); | |
} else { | |
console.log("Can not find pushToken target user"); | |
} | |
}); | |
}) | |
.catch(e => Console.log(e)); | |
return null; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment