title: Firebase Cloud Functions and FCM: how to send device to device messages? (JavaScript, Swift 3)
body: <p>I'm trying to send device to device messages in Firebase using Cloud Functions. I've never coded in JavaScript before and am using a template from this GitHub page: <a href="https://github.com/firebase/functions-samples/tree/master/fcm-notifications" rel="nofollow noreferrer">https://github.com/firebase/functions-samples/tree/master/fcm-notifications</a>. That's been recommended in the official documentation. However, I was trying to rewrite the code to make it fit my needs in a chat application. </p>
  
  <p>What am I already doing and what should the function do?</p>
  
  <ul>
  <li>I'm saving the device token from FIRInstanceID to the database to make it easily retrievable from the cloud function </li>
  <li>I'm uploading any new message with the text, receiverID and senderUserName to a dedicated path in the database</li>
  <li>The cloud function observes this database path and is called every time someone uploads a new notification</li>
  <li>The cloud function fetches the data and sends it in a notification to the user that belongs to the receiver id after having retrieved his device token</li>
  </ul>
  
  <p>That's my current cloud function:</p>
  
  <pre><code>const functions = require('firebase-functions');
  const admin = require('firebase-admin');
  admin.initializeApp(functions.config().firebase);
  
  exports.sendFollowerNotification = functions.database.ref('/notifications/notificationsToBeProcessed').onWrite(event =&gt; {
    const data = event.data.val();
    const text = data["text"];
    const receiverID = data["receiverID"];
    const senderUserName = data["senderUserName"];
  
    const payload = {
      notification: {
        title: 'Hey!',
        body: text
      }
    };
  
    var rootRef = firebase.database().ref('/notification-tokens/{receiverID}');
    rootRef.once("value")
      .then(function(snapshot) {
  
        var token = snapshot.child("{receiverID}")
  
        return admin.messaging().sendToDevice(token, payload).then(response =&gt; {
          // For each message check if there was an error.
          const tokensToRemove = [];
          response.results.forEach((result, index) =&gt; {
            const error = result.error;
            if (error) {
              console.error('Failure sending notification to', tokens[index], error);
              // Cleanup the tokens who are not registered anymore.
              if (error.code === 'messaging/invalid-registration-token' ||
                  error.code === 'messaging/registration-token-not-registered') {
                tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
              }
            }
          });
        });
  
      });
  
    })
  
  })
  </code></pre>
  
  <p>And I'm getting this error when I try to deploy:</p>
  
  <pre><code>SyntaxError: Unexpected token }
  at createScript (vm.js:56:10)
  at Object.runInThisContext (vm.js:97:10)
  at Module._compile (module.js:542:28)
  at Object.Module._extensions..js (module.js:579:10)
  at Module.load (module.js:487:32)
  at tryModuleLoad (module.js:446:12)
  at Function.Module._load (module.js:438:3)
  at Module.require (module.js:497:17)
  at require (internal/module.js:20:19)
  at /Users/jonas/.npm-global/lib/node_modules/firebase-tools/lib/triggerParser.js:18:11
  </code></pre>
  
  <p>Can someone with a little more experience in JavaScript tell me where my mistake is? Thanks!</p>