[英]How can I send a firebase cloud messaging notification from javascript?
[英]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.