Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function sendNotification(userId) {
- //get the userId of the person receiving the notification because we need to get their token
- const receiverId = userId;
- //get the token of the user receiving the message
- return admin
- .database()
- .ref(`/users/${receiverId}/settings`)
- .once("value")
- .then(snapshot => {
- const token = snapshot.child("msgToken").val();
- console.log("token: ", token);
- if (typeof token !== "undefined" && token !== null) {
- //we have everything we need
- //Build the message payload and send the message
- console.log("Construction the notification message.");
- const payload = {
- notification: {
- title: "Du har precis tjänat 150 tvätt-credits!",
- body:
- "Du har en kompis som har registrerat sig med din inbjudningskod, samt genomfört sitt första köp. Se status på dina credits i appen under ”Bjud in en vän”.",
- sound: "default",
- badge: "1"
- },
- data: {
- type: "invitation"
- }
- };
- return admin
- .messaging()
- .sendToDevice(token, payload)
- .then(function(response) {
- console.log("Successfully sent message:", response);
- console.log(response.results[0].error);
- })
- .catch(function(error) {
- console.log("Error sending message:", error);
- });
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement