Advertisement
Guest User

nodejs firebase function

a guest
Jan 21st, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function sendNotification(userId) {
  2.   //get the userId of the person receiving the notification because we need to get their token
  3.   const receiverId = userId;
  4.   //get the token of the user receiving the message
  5.   return admin
  6.     .database()
  7.     .ref(`/users/${receiverId}/settings`)
  8.     .once("value")
  9.     .then(snapshot => {
  10.       const token = snapshot.child("msgToken").val();
  11.       console.log("token: ", token);
  12.  
  13.       if (typeof token !== "undefined" && token !== null) {
  14.         //we have everything we need
  15.         //Build the message payload and send the message
  16.         console.log("Construction the notification message.");
  17.         const payload = {
  18.           notification: {
  19.             title: "Du har precis tjänat 150 tvätt-credits!",
  20.             body:
  21.               "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”.",
  22.             sound: "default",
  23.             badge: "1"
  24.           },
  25.           data: {
  26.             type: "invitation"
  27.           }
  28.         };
  29.         return admin
  30.           .messaging()
  31.           .sendToDevice(token, payload)
  32.           .then(function(response) {
  33.             console.log("Successfully sent message:", response);
  34.             console.log(response.results[0].error);
  35.           })
  36.           .catch(function(error) {
  37.             console.log("Error sending message:", error);
  38.           });
  39.       }
  40.     });
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement