我想在用户的“通知”集合中添加文档时,通过“云消息传递”发送“推送通知”。根据文档中的建议,我将用户的“令牌”保存在名为“messagingTokens”的字段的“arrayUnion”中。
此外,只有当名为“isPushEnabled”的字段为true时,才应发送通知。根据这些信息,我构建了这个“云函数”,并成功部署了它:
就像我说的那样,部署成功了。但是当一个文档被添加到“notifications”集合中时,我从日志中得到了这个信息:
这是我的
此外,只有当名为“isPushEnabled”的字段为true时,才应发送通知。根据这些信息,我构建了这个“云函数”,并成功部署了它:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const { onDocumentCreated } = require("firebase-functions/v2/firestore");
const { setGlobalOptions } = require("firebase-functions/v2");
admin.initializeApp();
// Set the maximum instances to 10 for all functions
setGlobalOptions({ maxInstances: 10 });
const notificationPath = "allUsers/{userId}/notifications/{notificationId}";
exports.sendNotification = onDocumentCreated(notificationPath, async (event) => {
functions.logger.log("New notificatin document was created");
const data = event.data.after.data();
const senderUid = data.senderUid;
const receiverUid = data.receiverUid;
const notificationOption = data.option;
functions.logger.log("Retrieved notification fields");
if (notificationOption === "receivedFriendRequest") {
functions.logger.log("Is option: receivedFriendRequest");
await onSentFriendRequest(senderUid, receiverUid);
}
});
async function onSentFriendRequest(ownerId, userId) {
// Get the owners details
const owner = await admin.firestore().collection("allUsers").doc(ownerId).get();
// Get the users details
const user = await admin.firestore().collection("allUsers").doc(userId).get();
const userHasNotificationsEnabled = user.data().isPushEnabled;
functions.logger.log("If this does not print, then your function is not being called");
if (!userHasNotificationsEnabled) {
functions.logger.log("User does not have push enabled");
return;
}
functions.logger.log("User has push enabled");
// Listing all tokens as an array.
tokens = user.data().messagingTokens;
// Send message to all tokens
const response = await admin.messaging().sendEachForMulticast({
tokens: tokens,
notification: {
title: "Neue Freundschaftsanfrage",
body: `${owner.data().username} möchte mit dir befreundet sein.`,
},
data: {
ownerId: ownerId,
userId: userId,
notificationOption: "receivedFriendRequest",
},
});
// For each message check if there was an error.
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
functions.logger.error(
'Failure sending notification to',
tokens[index],
error
);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
});
return Promise.all(tokensToRemove);
}
就像我说的那样,部署成功了。但是当一个文档被添加到“notifications”集合中时,我从日志中得到了这个信息:
在 Firebase 控制台
中,我看到函数被触发了,但是我遇到了一个 TypeError
。
我在这里做错了什么?如果需要更多信息,请告诉我。
编辑
关于 maxScale
,我有一个错误,我可以通过在我的 index.js
文件中添加这行代码来修复:
setGlobalOptions({maxInstances: 10})
这是我的
firebase.json
文件中的内容: "frameworksBackend": {
"region": "us-central1",
"maxInstances": 10
}
data
未定义。最后打印的日志信息是什么?这应该有助于确定哪个数据未定义。 - Peter Obiechinadata
未定义。最后一条打印的日志信息是什么?这应该有助于确定哪个数据未定义。 - Peter Obiechina