firebase - 从特定应用程序发送 FCM 的云功能
问题描述
我有一个包含两个应用程序(两个目标)的 xCode 项目,我正在尝试从特定目标发送 FCM,但它总是从第一个目标发送!
在 index.js 文件中
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotificationForCompany = functions.firestore.document('test/{referenceID}').onCreate(async (snap, context) => {
var theTitle = "Not"
let payload = {
notification: {
title: theTitle,
sound: 'default',
}
};
const allTokens = await admin.firestore().collection('users').get();
const tokens4 = [];
allTokens.forEach((tokenDoc) => {
if (tokenDoc.data().instanceIdToken){
tokens4.push(tokenDoc.data().instanceIdToken)
}
});
admin.messaging().sendToDevice(tokens4, payload);
});
解决方案
您可以根据主题发送通知。例如project-one-topic-unique-user-id
和project-two-topic-unique-user-id
。所以现在根据主题发送通知
let message = {
"topic": "project-two-topic-unique-user-id"
"notification": {
title: theTitle,
sound: 'default',
}
}
}
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);
});
推荐阅读
- doxygen - 包括另一个项目的文档
- powershell - Powershell Selenium 列出所有 id 元素
- python-3.x - Tensorflow 2 对象检测 API:Numpy 版本错误
- python - 如何将数据帧转换为张量
- asp.net-identity - OIDC 与 Asp.net 核心身份?
- excel - (不是文本到列) - 用分隔符将 1 列分成许多列
- reactjs - 使用 react-pdf (@react-pdf/renderer) 在浏览器中以模式从 blob 数据显示 PDF
- javascript - 反应 onChange 不适用于功能组件内的输入
- raspberry-pi - Raspbery Pi Pico 未连接到 PC /Raspberry Pi 4
- r - 如何确定每个人的最大数字跨度?