[英]Sending notifications with Firebase cloud function
In order to use Firebase Cloud Messaging
to later send notifications, I am trying to write a cloud function called sendNotification ; 为了使用
Firebase Cloud Messaging
稍后发送通知,我试图编写一个名为sendNotification的云函数; and I meet a problem in the process. 我遇到了一个问题。 If possible, I would like to get some advice on how to deal with the issue;
如果可能的话,我想获得一些有关如何处理该问题的建议; or maybe just have somebody point out a mistake I may be doing.
或只是有人指出我可能正在做的错误。
Here is the error message I get when running the command: 这是运行命令时收到的错误消息:
firebase deploy 火力基地部署
⚠ functions[sendNotification(us-central1)]: Deployment error.
Function load error: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'serviceAccountKey.json'
at Function.Module._resolveFilename (module.js:476:15)
........
It may useful to say that the file serviceAccountKey.json is in the directory called node_modules , in other words not missing. 可以说文件serviceAccountKey.json在名为node_modules的目录中,换句话说,不要丢失。 Here is the code in index.js:
这是index.js中的代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
var serviceAccount = require('serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://myapp.firebaseio.com'
});
exports.sendNotification = functions.https.onRequest((req, res) => {
let registrationToken = "abc123.........789xyz";
var payload = {
notification: {
title: "NotifTit",
body: "The good stuff."
},
token: registrationToken
};
admin.messaging().send(payload)
.then((response) => {
console.log("Successfully sent message: ", response);
})
.catch((error) => {
console.log("Error sending message: ", error);
})
});
Here is the contents of the package.json file:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"firebase-admin": "~6.0.0",
"firebase-functions": "^2.1.0"
},
"private": true
}
You cannot load files from the node_modules
folder, since that folder is ignored when uploading your functions. 您无法从
node_modules
文件夹加载文件,因为在上传函数时该文件夹将被忽略。
Firebase dev Lauren Long commented : Firebase开发人员Lauren Long 评论 :
But one thing to keep in mind is that the entire node_modules folder is ignored during uploading of source code, so your package.json has to contain all of your dependencies and cannot reference any files inside of 'node_modules'.
但是要记住的一件事是,在上传源代码期间会忽略整个node_modules文件夹,因此package.json必须包含所有依赖项,并且不能引用“ node_modules”内部的任何文件。 In other words, your functions should still be able to work if you ran the following commands inside of the functions folder:
换句话说,如果您在functions文件夹中运行以下命令,则您的功能应该仍然可以使用:
rm -rf node_modules
npm install
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.