簡體   English   中英

如何向各種主題發送通知?

[英]How can I send notification to various topics?

請在此處閱讀:

情況:

Person A想向他領導的許多主題發送通知,所以有一天他想在第二天向主題database發送主題math等等。

Person B是另一位領導者,他也想做同樣的事情,將通知發送到主題English和 next day 到French

主題包含注冊到這些主題的各種設備。

這是數據庫的一部分:

{
 "Class" : {
    "push_id_here" : {
          "name" : "English",
          "teachid" : "teacher_id_here"
    },
    "push_id_here1" : {
        "name" : "math",
         "teachid" : "teachers_id_here"
   }
 },
"messages" : {
"-KzuGjz90g8gAjgVG68O" : {
  "message" : "bye",
  "title" : "Good"
  }
}
}

在應用程序中是這樣的, Person A單擊items列表示例(單擊英語),並且有一個學生列表,里面有英語,然后 A 必須向該列表發送通知。

我嘗試了什么:

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);



exports.pushNotification = 
functions.database.ref('/messages/{pushId}').onWrite( event => {

console.log('Push notification event triggered');


 var valueObject = event.data.val();


//Create a notification
const payload = {
    notification: {
        title:valueObject.name,
        body: valueObject.text,
        sound: "default"
    },
  };

const options = {
    priority: "high",
    timeToLive: 60 * 60 * 24
};


return admin.messaging().sendToTopic("English", payload, options);
});

上述cloud functions有效,但只發送English 我不想在函數中寫出主題的名稱。 我想要做的是,如果Person A輸入了注冊到主題database的學生列表,則會向這些人發送通知。 如果Person A進入了注冊主題的學生列表Math通知將發送給這些人。 如果Person B進入注冊主題的學生列表, french通知將發送給這些人,依此類推。

您是否嘗試過類似的方法:

['topic1', 'topic2', 'topic3', 'topic4'].map (topic => admin.messaging().sendToTopic(topic, payload, options));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM