我需要有关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);
});
});
答案 0 :(得分:0)
由于您使用的是以下语法onWrite((change, context)...)
,因此我假设您使用的Cloud Functions版本> = 1.0。
对于版本大于等于1.0的版本,您应初始化firebase-admin
,不使用任何参数,如下所示:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
请参见https://firebase.google.com/docs/functions/beta-v1-diff#new_initialization_syntax_for_firebase-admin
答案 1 :(得分:0)
感谢您的答复,此问题现已解决。
我首先删除了以前的功能,然后重新启动,然后在全球范围内安装了最新的firebase工具,并像通常在启动时一样更新了npm工具。然后,我在Firestore中使用了此代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
//const firestore = new Firestore();
//const settings = {/* Date... */ timestampsInSnapshots: true};
//firestore.settings(settings);
exports.sendNotification =
functions.firestore.document('Users/{userId}/Notifications/{notificationId}')
.onWrite((c hange, context) =>{
const userId = context.params.userId;
const notificationId = context.params.notificationId;
console.log('The User id is : ', userId);
console.log('The Notification id is : ', notificationId);
// ref to the parent document
return admin.firestore().collection("Users").doc(userId).collection("Token").doc(userId).get().then(queryResult => {
const tokenId = queryResult.data().deviceToken;
//const toUser = admin.firestore().collection("Users").doc(userId).collection("Notifications").doc(notificationId).get();
const notificationContent = {
notification:{
title: "/*App name */",
body: "You have a new Comment!",
icon: "default",
click_action: "/*Package */_TARGET_NOTIFICATION"
}
};
return admin.messaging().sendToDevice(tokenId, notificationContent).then(result => {
console.log("Notification sent!");
//admin.firestore().collection("notifications").doc(userEmail).collection("userNotifications").doc(notificationId).delete();
});
});
});