0

I am writing an android application where I need to send a notification based on some condition.

enter image description here

For example, when notiType = home then send other message in notification. If notiType = inBetween then send another message

I have written the cloud function for this but getting an error while deploying.

Here is the cloud function :

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');

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

/* Listens for new messages added to /messages/:pushId and sends a notification to users */
exports.pushNotification = functions.database.ref('/Notifications/{user_id}/{notification_id}').onWrite(event => {
  console.log('Push notification event triggered');
  /* Grab the current value of what was written to the Realtime Database 
   */
  const userId = event.params.user_id;
  const notificationId = event.params.notification_id;


  const deviceToken = admin.database().ref(`Notifications/${userId}/${notificationId}/deviceToken`).once('value');
  const childName = admin.database().ref(`Notifications/${userId}/${notificationId}/childName`).once('value');
  const notificationType = admin.database().ref(`Notifications/${userId}/${notificationId}/type`).once('value');

  return Promise.all([deviceToken, childName, notificationType]).then(result => {

    const token = result[0].val();
    const name = result[1].val();
    const type = result[2].val();

    /* Create a notification and data payload. They contain the notification information, and message to be sent respectively */

    const payload;

    switch (type) {
      case "home":
        payload = {
          notification: {
            title: 'App Name',
            body: `${name} is reached at home`,
            sound: "default"
          }
        };
        break;
      case "between":
        payload = {
          notification: {
            title: 'App Name',
            body: `${name} stuck on the way for some reason`,
            sound: "default"
          }
        };
        break;
      case "school":
        payload = {
          notification: {
            title: 'App Name',
            body: `${name} reached at school`,
            sound: "default"
          }
        };
        break;
    };

    return admin.messaging().sendToDevice(token, payload).then(response => {
      return null;
    });
  });
});

Getting this error :

Getting this error

Please correct me where I am going wrong. Using Firebase -tools version 5.0.1

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
xaif
  • 553
  • 6
  • 27
  • change `const payload;` to `let payload={};`. This is SyntaxError. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let, and https://stackoverflow.com/questions/22308071/what-is-the-difference-between-let-and-const-ecmascript-2015-es6 – zkohi Jul 10 '19 at 22:49

1 Answers1

1

JavaScript is telling you this line is invalid:

const payload;

You can't declare a const variable without also giving it a value immediately. Since you are conditionally giving it a value later, perhaps you should use let payload; instead.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441