Firebase功能-推送通知

时间:2020-08-17 13:46:53

标签: javascript firebase google-cloud-functions firebase-cloud-messaging

您好,这是我第一次使用Firebase推送通知,我正尝试将测试推送通知发送到特定设备,并且在检查功能时遇到错误。这是我的代码和错误,非常感谢您的帮助。

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

exports.testPushNotifications = functions.https.onRequest((req, res) => {

res.send("Trying to send a message")
console.log("LOGGER --- trying to send a push notification..");

const uid = 'O5cN65bAe3Mf7uMr7DCF8GlvsHA3'
const fcmToken = 'cUQ-IcReLTQ:APA91bGgDRIQfFi7k4AIo45b4sXr.........'

return admin.database().ref('/users/' + uid).once('value', snapshot => {

  const user = snapshot.val();

  console.log("Username is " + user.userName);

  const payload = {
    notification: {
      title: 'Push Notification',
      body: 'Test Notification message'
    }
  }

  admin.messaging().sendToDevice(fcmToken, payload)
    .then(function(response) => {        // 27:30  error  Parsing error: Unexpected token =>
    console.log('Successfully sent message:', response);
  })
    .catch(function(error) => {
      console.log('Error sending message:', error);
    });
  });
})

如果我删除=>,则会收到以下警告和错误:

  admin.messaging().sendToDevice(fcmToken, payload)
    .then(function(response) {
    console.log('Successfully sent message:', response);
  })
    .catch(function(error) {
      console.log('Error sending message:', error);
    });

27:11警告意外的函数表达式preferred-arrow-callback

27:11错误每个then()应该返回一个值或抛出promise / always-return

30:12警告意外的函数表达式preferred-arrow-callback

0 个答案:

没有答案