【发布时间】:2019-02-13 21:22:57
【问题描述】:
我需要有关 Firestore 功能的帮助。我正在开发一个需要通知的应用程序,并且我使用 firebase 作为后端,我对云功能特性完全陌生。
所以我想在将文档添加到集合时向用户发送通知,我尝试为这些功能设置一些东西,但由于我不知道的原因它不起作用,我的 Node Js 是更新版本,firebase 工具已更新,但仍然出现错误。
这是我的 index.js 文件和错误消息。感谢您的帮助。
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.firestore.document('Users/{userId}/Notifications/{notificationId}').onWrite((change, context) =>{
const userId = context.params.userId;
const notificationId = context.params.notificationId;
console.log('The User id is : ', userId);
if(!context.data.val()){
return console.log('A notification has been deleted from the database');
}
// ref to the parent document
const device_token = admin.firestore.doc('Users/${userId}/Token/${userId}/deviceToken').once('value');
return device_token.then(result => {
const token_id = result.val();
const payLoad = {
notification:{
title: "Qubbe",
body: "You have a new comment!",
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payLoad).then(response => {
console.log('This is the notification feature');
});
});
device_token.catch(error => {
console.log(error);
});
});
【问题讨论】:
-
我已经在我的一个 tutorials 中逐步说明了如何使用
Cloud Firestore将 notifications 发送给特定用户和Node.js。你也可以看看我这个 post 的回答。
标签: android firebase notifications google-cloud-firestore google-cloud-functions