我需要在fcm中通过主题实现广播通知。我正在使用firebase-admin发送这些文件。任何人都可以发布通过node.js发送这些通知的代码片段吗?
发布于 2021-01-15 18:30:52
首先,您需要为用户订阅给定的主题
// These registration tokens come from the client FCM SDKs.
var registrationTokens = [
'YOUR_REGISTRATION_TOKEN_1',
// ...
'YOUR_REGISTRATION_TOKEN_n'
];
// Subscribe the devices corresponding to the registration tokens to the
// topic.
admin.messaging().subscribeToTopic(registrationTokens, topic)
.then(function(response) {
// See the MessagingTopicManagementResponse reference documentation
// for the contents of response.
console.log('Successfully subscribed to topic:', response);
})
.catch(function(error) {
console.log('Error subscribing to topic:', error);
});,然后您可以使用广播通知
// The topic name can be optionally prefixed with "/topics/".
var topic = 'highScores';
var message = {
data: {
score: '850',
time: '2:45'
},
topic: topic
};
// Send a message to devices subscribed to the provided topic.
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});你可以阅读更多关于它的here
发布于 2021-01-15 18:19:33
您将需要Firebase项目设备注册令牌firebase云函数
firebase通知的工作原理:设备到设备通知:从设备1 -firebase数据库(函数在写入数据库时触发)-firebase云函数(云函数将向dev 2发送通知)- device2
发布于 2021-06-02 09:52:57
const topic = "test";
const payload = {
notification: {
title: "New news",
body: "2:45",
},
};
admin
.messaging()
.sendToTopic(topic, payload)
.then((response2) => {
// Response2 is a message ID string.
console.log("Successfully sent message:", response2);
})
.catch((error) => {
console.log("Error sending message:", error);
});https://stackoverflow.com/questions/65732931
复制相似问题