问题描述
我想为我的用户订阅不同的语言"根据他们的喜好选择主题,以便他们以所需的语言接收通知
i wanna subscribe my users to different "language" topics according to their preference so they receive notifications in the language they want
在我的Firestore中,我在其文档中有一个名为Notifications的集合(默认),我还有另外两个集合..英语和阿拉伯语.
in my firestore i have a collection called notifications inside its document(default) i have two more collections .. english and arabic.
现在在我的购物应用程序中,我使用户适应英语主题以测试其是否有效
now in my shopping app i subbed the user to english topic to test if it works
_fcm.subscribeToTopic('english');
这是我针对云功能的index.js代码:
and here's my index.js code for cloud fuctions:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var newData;
exports.messageTrigger = functions.firestore.document('notifications/default/{languageId}/{messagesId}').onCreate(async (snapshot, context) => {
newData = snapshot.data();
const payload = {
notification: {
title: newData.message,
body: newData.body,
},
data: {
click_action: 'FLUTTER_NOTIFICATION_CLICK',
message: newData.message,
}
};
if (context.param.languageId === "english") {
await admin.messaging().sendToTopic('english', payload);
}
else if (context.param.languageId=== "arabic") {
await admin.messaging().sendToTopic('arabic', payload);
}
});
但是当我在通知集合内的英语集合中创建文档时,它不起作用.有人知道为什么吗?
but when i create a document in the english collection inside notifications collection it doesnt work. anybody know why?
推荐答案
我通过使我的函数固定如下来解决了这个问题:
i fixed it by making my functions like this:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var newData;
exports.messageTrigger = functions.firestore.document('notifications/{notificationsId}').onCreate(async (snapshot, context) => {
newData = snapshot.data();
const payload = {
notification: {
title: newData.message,
body: newData.body,
},
data: {
click_action: 'FLUTTER_NOTIFICATION_CLICK',
message: newData.message,
}
};
if (newData.language === 'english'){
await admin.messaging().sendToTopic('english', payload);
}else if (newData.language === 'arabic'){
await admin.messaging().sendToTopic('arabic', payload);
}
});
这篇关于颤动:在一个集合中为不同的主题订阅用户以获取推送通知fcm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!