【问题标题】:firebase cloud function is not being executed on onCreate event for firebase real time databasefirebase 云功能未在 firebase 实时数据库的 onCreate 事件上执行
【发布时间】:2019-11-08 09:18:29
【问题描述】:

我已经将下一个函数部署到了 firebase:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

console.log('FILE INCLUDED');

const serviceAccount = require('../cargo-tender-firebase-adminsdk-8e307-c6b82762d2.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: 'https://cargo-tender.firebaseio.com'
});

console.log('INITIALIZED');

export const onMessageCreate = functions.database
  .ref('/chat')
  .onCreate((snapshot, context) => {

    console.log('onCreate RUN');

    const token = 'myToken';
    const payload = {
        notification: {
        title: 'Title',
        body: 'come check it',
        badge: '0',
        sound: 'default',
        }
    };

    console.log('trying to send push notification');

    return admin
      .messaging()
      .sendToDevice(token, payload).then((response) => {
        console.log('Message sent');
        console.log(response);
      }).catch((error) => {
        console.error('There was an error, while sending the message');
        console.error(error);
      });
  });

  console.log('FUNCTION FINISH');

但是当我尝试在路径/user-chat 下创建记录时 - 该函数未执行,推送通知未发送。此外,函数调用未在日志中注册。

如果我从 firebase 控制台(测试功能)手动运行该功能 - 该功能执行没有错误。收到推送通知。我看到日志中的一切都很好:

3:07:56.902 PM onMessageCreate FILE INCLUDED
3:07:57.112 PM onMessageCreate INITIALIZED
3:07:57.113 PM onMessageCreate FUNCTION FINISH
3:07:57.273 PM onMessageCreate onCreate RUN
3:07:57.273 PM onMessageCreate trying to send push notification
3:07:57.757 PM onMessageCreate Message sent
3:07:57.759 PM onMessageCreate { results: [ { messageId: '0:1561550877745153%be2b376ebe2b376e' } ],
  canonicalRegistrationTokenCount: 0,
  failureCount: 0,
  successCount: 1,
  multicastId: 5684437879493655000 }
3:07:57.771 PM onMessageCreate Function execution took 1008 ms, finished with status: 'ok'

当我在指定路径 (/user-chat) 下的火力库中添加记录时,我不明白为什么不触发 onCreate。我也尝试使用其他路径,例如/chat,但这并没有成功。

对此的任何建议将不胜感激。

谢谢!

【问题讨论】:

    标签: typescript firebase firebase-realtime-database google-cloud-functions firebase-admin


    【解决方案1】:

    已修复! 我不得不添加一个面具,例如/user-chat/{anything}

    【讨论】:

      猜你喜欢
      • 2020-05-15
      • 2017-11-23
      • 1970-01-01
      • 2020-07-12
      • 2018-11-17
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多