简体   繁体   中英

Send notifications conditionally using Firebase cloud functions?

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

在此处输入图片说明

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 :

收到此错误

Please correct me where I am going wrong. Using Firebase -tools version 5.0.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM