【发布时间】:2019-01-20 17:48:47
【问题描述】:
因此,当向我的数据库中的通知添加值时,我试图从 firebase 运行以下函数。但是,即使在路径中写入了新值,我也不觉得该函数被触发运行。该函数的日志仍然为空。
由于我无法在此处添加图像,因此我可以尽我所能来描述它。在数据库中我们通知。在通知中,当有人尝试添加朋友时,它会使用接收请求的那个人的 user_id 填充它。然后在那个人的 user_id 中添加一个随机生成的 notification_id ,在该部分中它同时具有发送请求的用户和通知的类型。下面是一张小图
- 通知
- --->user_id
- ------>notification_id
- ------>来自
- --------->类型
index.js
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/Notifications/{user_id}/{notification_id}').onWrite(event => {
const user_id = event.params.user_id;
const notification_id = event.params.notification_id;
console.log('We have a notification to send to : ', user_id);
if(!event.data.val()){
return console.log('A notification has been deleted from the database : ', notification_id);
}
const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');
return deviceToken.then(result => {
const token_id = result.val();
const payload = {
notification: {
title : "Friend Request",
body : "You-ve recieved a new Friend Request",
icon : "default",
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response => {
console.log('This was the notification feature');
});
});
});
.runtimeconfig.json
{
"firebase": {
"projectId": "lapitchat-c1aab",
"databaseURL": "https://lapitchat-c1aab.firebaseio.com",
"storageBucket": "lapitchat-c1aab.appspot.com",
"cloudResourceLocation": "us-central"
}
}
package.json
{
"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": "~5.13.0",
"firebase-functions": "^2.0.0"
},
"private": true
}
我的代码中是否有遗漏或任何错误?
【问题讨论】:
标签: javascript firebase google-cloud-functions