flutter: subscribing user to different topics in one collection for push notifications fcm

不羁岁月 提交于 2020-07-10 04:50:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!