flutter:如何通过 firebase 云函数发送多语言通知
flutter: How to send multilingual notifications via firebase cloud functions
在我的购物应用程序中,用户做的第一件事是基于三个按钮在三种语言之间进行选择,以使用英语、阿拉伯语等来操作应用程序。在我的主页的 initState 中,我可以做一个 if声明如下:
if (chosenLanguage == "english") {
fcm.subscribeToTopic("messages");
}
else if (chosenLanguage == "arabic") {
fcm.subscribeToTopic("messagesArabic");
}
等等..
然而,在我的 index.js 创建自动推送通知时,我只将用户替换为“消息”(即英语)集合,并且我知道如何根据用户的选择。如何在我的 index.js?
中将子语言写成阿拉伯语,将子语言写成英语和其他语言
这是我的云函数的代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var newData;
exports.messageTrigger = functions.firestore.document('messages/{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,
}
};
admin.messaging().sendToTopic('messages', payload);
});
要订阅多个集合,您可以在触发函数的路径中添加另一个参数:
exports.messageTrigger = functions.firestore.document('{languageId}/{messagesId}').onCreate(async (snapshot, context) => {
然后可以通过代码中的collectionId
参数来确定发送什么语言:
if (context.param.languageId == "english") {
await admin.messaging().sendToTopic('messages', payload);
}
else if (context.param.languageId == "arabic") {
await admin.messaging().sendToTopic('arabic', payload);
}
请注意,您可能希望将所有推送通知集合放在一个特定的集合中,否则您的代码将触发写入 any 集合,即使一个无关紧要的集合有通知。
例如:
exports.messageTrigger = functions.firestore.document('notifications/default/{languageId}/{messagesId}').onCreate(async (snapshot, context) => {
所以这里 notifications
是所有通知所在的顶级目录,default
是其中的一个(可能是唯一的)文档)。在此之下,您将拥有每种语言的集合。
我通过使我的函数 index.js 像这样修复它:
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);
}
});
在我的购物应用程序中,用户做的第一件事是基于三个按钮在三种语言之间进行选择,以使用英语、阿拉伯语等来操作应用程序。在我的主页的 initState 中,我可以做一个 if声明如下:
if (chosenLanguage == "english") {
fcm.subscribeToTopic("messages");
}
else if (chosenLanguage == "arabic") {
fcm.subscribeToTopic("messagesArabic");
}
等等..
然而,在我的 index.js 创建自动推送通知时,我只将用户替换为“消息”(即英语)集合,并且我知道如何根据用户的选择。如何在我的 index.js?
中将子语言写成阿拉伯语,将子语言写成英语和其他语言这是我的云函数的代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var newData;
exports.messageTrigger = functions.firestore.document('messages/{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,
}
};
admin.messaging().sendToTopic('messages', payload);
});
要订阅多个集合,您可以在触发函数的路径中添加另一个参数:
exports.messageTrigger = functions.firestore.document('{languageId}/{messagesId}').onCreate(async (snapshot, context) => {
然后可以通过代码中的collectionId
参数来确定发送什么语言:
if (context.param.languageId == "english") {
await admin.messaging().sendToTopic('messages', payload);
}
else if (context.param.languageId == "arabic") {
await admin.messaging().sendToTopic('arabic', payload);
}
请注意,您可能希望将所有推送通知集合放在一个特定的集合中,否则您的代码将触发写入 any 集合,即使一个无关紧要的集合有通知。
例如:
exports.messageTrigger = functions.firestore.document('notifications/default/{languageId}/{messagesId}').onCreate(async (snapshot, context) => {
所以这里 notifications
是所有通知所在的顶级目录,default
是其中的一个(可能是唯一的)文档)。在此之下,您将拥有每种语言的集合。
我通过使我的函数 index.js 像这样修复它:
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);
}
});