【问题标题】:Unable to send notifications using firebase functions after update更新后无法使用 Firebase 功能发送通知
【发布时间】:2019-02-02 18:20:07
【问题描述】:

所以今天我更新了 firebase cli,然后部署了一个新功能。尽管 firebase 日志显示通知已发送到这么多令牌,但没有通知发生。日志中显示错误

函数返回未定义、预期的 Promise 或值

我在堆栈溢出中搜索了答案,但没有任何帮助。 另外我想在它显示一些不同的错误之前添加它

TypeError: 无法读取 null 的属性“描述”

现在突然显示函数返回未定义。

不知道出了什么问题。任何帮助表示赞赏。

索引.js

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


function token_send(admin,title_input,body_input,getBody,getDeviceTokensPromise,change){

  // Only edit data when it is first created.
  if (change.before.val()) {
    return 0;
  }

  // Exit when the data is deleted.
  if (!change.after.val()) {
    return 0;
  }



return Promise.all([getDeviceTokensPromise,getBody]).then(results => {
  const tokensSnapshot = results[0];
  const notify=results[1];

  if (!tokensSnapshot.hasChildren()) {
    return console.log('There are no notification tokens to send to.');
  }
  console.log('There are', tokensSnapshot.numChildren(), 'tokens to send notifications to.');
  var contentAlert = change.after.val();

  // Notification details.
  const payload = {
    'data': {
      'title': title_input,
      'body': body_input

    }

  };


const tokens = Object.keys(tokensSnapshot.val());



  // Send notifications to all tokens.
  return admin.messaging().sendToDevice(tokens, payload).then(response => {
    console.log("Successfully sent message:", response);
     console.log("content alert",contentAlert);
    // For each message check if there was an error.
    const tokensToRemove = [];
    response.results.forEach((result, index) => {
      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());
        }
      }
    });

    return Promise.all(tokensToRemove);
  });

});
}




exports.sendNotificationCouncil = functions.database.ref(`path/Post/{pushId}`).onWrite((change,context) => {
const getDeviceTokensPromise = admin.database().ref(`/Token/token_no`).once('value');
  const getBody=admin.database().ref(`/Post`).once('value');
  var title_input='You have new Post';
  var contentAlert = change.after.val();
  var body_input=contentAlert.description; //showing error here
  token_send(admin,title_input,body_input,getBody,getDeviceTokensPromise,change);
  });

【问题讨论】:

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


    【解决方案1】:

    你应该在sendNotificationCouncil云函数中返回promise(由token_send()返回),如下:

    exports.sendNotificationCouncil = functions.database.ref(`path/Post/{pushId}`).onWrite((change,context) => {
      const getDeviceTokensPromise = admin.database().ref(`/Token/token_no`).once('value');
      const getBody=admin.database().ref(`/Post`).once('value');
      var title_input='You have new Post';
      var contentAlert = change.after.val();
      var body_input=contentAlert.description; //showing error here
    
      return token_send(admin,title_input,body_input,getBody,getDeviceTokensPromise,change);
    });
    

    请注意,捕获函数中的错误也是一种最佳做法,在这种情况下返回 false

    【讨论】:

    • 还是一样。它现在也显示 TypeError 消息。不知道发生了什么! Firebase 在每次更新后都会不断变化,这令人沮丧。
    • 如果你这样做console.log(contentAlert)会发生什么?它是空的吗?可能发生的情况是,此时可能函数存在全局问题:还有另一个 StackOverflow 问题具有类似问题,尽管一切似乎正确,但它似乎无法正常工作。见stackoverflow.com/questions/52052200/…
    • Console.log(contentAlert) 不为空。它正在赋予价值。有没有办法解决这个问题,或者唯一的办法就是等待事情好转?
    • 那么,如果contentAlert 不为空,那么contentAlert 是否有一个名为description 的属性?你能显示console.log(contentAlert) 的输出吗?
    • 是的,它有 description 属性。您可以在编辑的问题中查看控制台的屏幕截图。
    猜你喜欢
    • 1970-01-01
    • 2018-06-01
    • 2019-02-23
    • 2021-04-04
    • 2019-12-18
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    相关资源
    最近更新 更多