这是index.js代码
const admin = require('firebase-admin');
const functions = require('firebase-functions');
admin.initializeApp();
exports.notifFunc = functions.database.ref("/collegeData/{collegeId}/notices").onCreate((snapshot, context) => {
console.log(snapshot.val());
return admin.messaging().sendToTopic('notices', {notification: {title: "Title", body: "Does this
work?", clickAction: "FLUTTER_NOTIFICATION_CLICK"}});
});
部署成功,输出如下。
=== Deploying to '*************'...
i deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
functions@ lint C:\Users\kenre\Documents\GitHub\Android Project\************\functions
eslint .
+ functions: Finished running predeploy script.
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
+ functions: required API cloudfunctions.googleapis.com is enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (37.51 KB) for uploading
+ functions: functions folder uploaded successfully
i functions: updating Node.js 8 (Deprecated) function notifFunc(us-central1)...
+ functions[notifFunc(us-central1)]: Successful update operation.
! functions: Warning: Node.js 8 functions are deprecated and will stop running on 2021-03-15. Please upgrade to Node.js 10 or greater by adding an entry like this to your package.json:
{
"engines": {
"node": "12"
}
}
The Firebase CLI will stop deploying Node.js 8 functions in new versions beginning 2020-12-15, and deploys from all CLI versions will halt on 2021-02-15. For additional information, see: https://firebase.google.com/support/faq#functions-runtime
+ Deploy complete!
Project Console: https://console.firebase.google.com/project/*************/overview
我的应用程序使用用于Firebase实时数据库的Rest API在指定路径(“ collegeData / {collegeId} / notices”)中成功创建/发布了新的键值对条目。但是日志永远不会出现(在编写此日志之前要等待4个小时),并且也不会发送通知。 有什么理由吗?我知道它要求我升级到节点版本12,但为了避免使用信用卡,我不这样做。
答案 0 :(得分:1)
尝试各种随机事物后,我似乎偶然发现了解决方案。 尽管Firebase文档说它侦听指定路径下所有条目中的写入,但实际上它需要将路径指定为 collegeData / {collegeId} / notices / {noticeId} 来侦听新的通知。用新的ID密钥发布。
答案 1 :(得分:0)
如果功能被成功触发,则控制台getDocuments
中将有一条日志。因此,如果您从未收到日志,则意味着该功能没有被触发。
在您的情况下,我可能会尝试使用一个简单的函数来测试该函数在没有连接信用卡的情况下是否可以调用,尽管我认为如果您调用它并且它想要一种可以在日志中显示的付款方式。这可能不是问题,但可能是问题。
功能日志在功能执行后约20-60秒出现。您的函数对我来说看起来不错,如果引用正确,则应调用该函数。上载数据库图片,这样我们就可以看到没有错误,并测试一个简单的函数以确保任何函数在onCreate之前先起作用,然后尝试从flutter中调用http可调用函数:
Function Execution Started
在Flutter中:
exports.areMyFunctionConfigured = functions.https.onCall(async(data, context) => {
console.log('they work');
});