[英]Sending notifications with Firebase cloud function
為了使用Firebase Cloud Messaging
稍后發送通知,我試圖編寫一個名為sendNotification的雲函數; 我遇到了一個問題。 如果可能的話,我想獲得一些有關如何處理該問題的建議; 或只是有人指出我可能正在做的錯誤。
這是運行命令時收到的錯誤消息:
火力基地部署
⚠ 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)
........
可以說文件serviceAccountKey.json在名為node_modules的目錄中,換句話說,不要丟失。 這是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
}
您無法從node_modules
文件夾加載文件,因為在上傳函數時該文件夾將被忽略。
Firebase開發人員Lauren Long 評論 :
但是要記住的一件事是,在上傳源代碼期間會忽略整個node_modules文件夾,因此package.json必須包含所有依賴項,並且不能引用“ node_modules”內部的任何文件。 換句話說,如果您在functions文件夾中運行以下命令,則您的功能應該仍然可以使用:
rm -rf node_modules
npm install
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.