Firebase Firestore Functions Notification Android



我需要消防商店功能的帮助。我正在开发一个需要通知的应用程序,并且我使用 Firebase 作为后端,我对云功能功能完全陌生。

所以我想在将文档添加到集合时向用户发送通知,我尝试为函数设置一些东西,但由于我不知道的原因它不起作用,我的 Node Js 是更新版本,Firebase 工具已更新,但我仍然收到错误。

这是我的索引.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);
});

}(;

错误: 在命令行中出现错误

感谢您的回复,问题现已解决。

我首先删除了我的旧功能,然后重新开始,然后我在全球范围内安装了最新的 firebase 工具,并像在初创公司中一样更新了 npm 工具。然后我为我的火库使用了这个代码:

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((change, 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();
});
});
});

由于您使用以下语法onWrite((change, context)...)因此我假设您使用的是云函数版本>= 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

最新更新