更新后无法使用Firebase功能发送通知 [英] Unable to send notifications using firebase functions after update

查看:76
本文介绍了更新后无法使用Firebase功能发送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,今天我更新了firebase cli,然后部署了新功能.尽管Firebase日志显示通知已发送到这么多令牌,但不会发生任何通知.日志中显示错误

So Today I updated the firebase cli and after that deployed a new function. Although the firebase log shows that notifications has been sent to this many tokens, no notification occurs. An error shows in the log

函数返回了未定义的预期承诺或值

Function returned undefined, expected Promise or value

我在堆栈溢出中搜索答案,但没有任何帮助. 我还要补充一点,在它显示出其他错误之前

I searched for answers in stack overflow but nothing helped. Also I would like to add that before it was showing some different error

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

TypeError: Cannot read property 'description' of null

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

and now suddenly it is showing function returned undefined.

不确定什么地方出了问题.感谢您的帮助.

Not sure what is wrong. Any help is appreciated.

Index.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);
  });

推荐答案

您应该在sendNotificationCouncil Cloud Function中返回诺言(由token_send()返回),如下所示:

You should return the promise (returned by token_send()) in the sendNotificationCouncil Cloud Function, as follows:

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.

Note that it is also a best practice to catch the errors in your Function and in this case return false.

这篇关于更新后无法使用Firebase功能发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆