firebase - 您如何获得服务器上所有令牌的值?
问题描述
我想从我的 Firebase 数据库中的这条路径收集我所有的 FCM 用户 iOS 设备令牌:
BootCamp/Notifications/iOS
在此位置,将创建一个 autoIDChild 以将用户的设备令牌存储为"deviceToken"。
我一直在尝试遵循此链接上的 cloud_functions 示例,但由于我的用例不同,因此很难弄清楚。这是我在 JS 中的云功能代码:
exports.iOSPush = functions.database.ref('/BootCamp/Bulletins/date').onWrite((snapShot, context) =>{
let tokensSnapShot
let tokens
//here, I attempt to get access to all iOS tokens on my server
const getTokens = admin.database().ref('/BootCamp/Notifications/iOS/{key}').once('value');
return Promise.all([getTokens]).then( (results) => {
tokensSnapShot = results[0]
tokens = Object.keys(tokensSnapShot)
const payload = {
notification:{
title: 'congrats it works',
body: 'Cloud function noti for ios',
sound: 'default',
badge: '1'
}
};
//tokens value in the console log is: "node_,ref_,index_". I was expecting an array of tokens:/
return admin.messaging().sendToDevice(tokens, payload)
})
});
如何在我的服务器上获取这些 iOS 令牌?
解决方案
请检查这个例子:
return Promise.all([admin.database().ref(`/users/${user}/account/tokensArray`).once('value')]).then(results => {
const tokens = results[0];
if (!tokens.hasChildren()) return null;
let payload = {
notification: {
title: 'title',
body: 'message',
icon: 'icon-192x192.png'
}
};
const tokensList = Object.keys(tokens.val());
return admin.messaging().sendToDevice(tokensList, payload);
});
推荐阅读
- php - 从 N 长度数组中每个循环获取 2 个元素
- javascript - 防止 youtube iframe api 限制移动设备上的播放质量(分辨率)
- asp.net-core - migrationBuilder 如何确定要创建哪些索引?
- javascript - Nuxt,将 Vuex 存储拆分为单独的文件会出现错误:未知突变类型:登录
- sql-server - 如何使用 Django 连接到 MsSQL 服务器
- javascript - 如何插入文本
作为使用 jquery 的标记属性的值? - python - base64的熊猫字符串列到十六进制
- javascript - Ag-grid 还是我们自己的用于过滤和排序的自定义表格?
- angular - 显示静态注入器错误而不是加载页面
- html - 如何在css中选择当前打开的链接?