问题
i wanna subscribe my users to different "language" topics according to their preference so they receive notifications in the language they want
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');
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?
回答1:
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);
}
});
来源:https://stackoverflow.com/questions/62550957/flutter-subscribing-user-to-different-topics-in-one-collection-for-push-notific