gpt4 book ai didi

firebase - 在 React Native 中使用 Firebase 云功能的博览会推送通知

转载 作者:行者123 更新时间:2023-12-04 09:46:27 24 4
gpt4 key购买 nike

https://www.youtube.com/watch?v=R2D6J10fhA4 上看到本教程,我正在尝试实现发送推送通知(Expo + Firebase Cloud Functions)

我正在尝试编写一个云函数,如果添加了另一个数据库条目,它将向我的数据库中的所有设备发送推送通知

故障是...

我的用户数据库的结构是这样的 - 任何帮助或正确方向的点都将不胜感激,因为我仍在学习整个过程。

myFirestoreRealTimeDatabase
|--messages
|------fjdhfsjkf(**Unquiqe Message id**)
|------------message: Test Push Notification
|
|--users
|------EIDdxqEkm6YVRqV3p1x9FDXuX4C2 (**Unquiqe user id**)
|------------email: myemail@email.com
|------------expoToken: "ExponentPushToken[5jxdvMLq123JhBNBCg9gDH0w]"
|
|------JFJdjksfVRqV3p1x9FDXJFJS (**Unquiqe user id**)
|------------email: myemail2@email.com
|------------expoToken: "ExponentPushToken[34jxdfdgkdsjBCg9gDH0w]"

所以我需要在创建新消息时向所有 expoToken 发送通知,然后需要运行一个函数来使用 expos 推送服务器,就像这样。
 .then((messages) => {
// console.log(messages)
fetch("https://exp.host/--/api/v2/push/send", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(messages),
});
})

这是我已经开始的代码,但它不起作用。
 const functions = require('firebase-functions');
var fetch = require('node-fetch')
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);


exports.sendPushNotification = functions.database.ref('contacts/').onCreate(event => {
const root = event.data.ref.root
var messages = []
//return the main promise
return root.child('/users').once('value').then(function (snapshot) {
snapshot.forEach(function (childSnapshot) {
var expoPushToken = childSnapshot.val().expoPushToken;
console.log(expoPushToken);
messages.push({
"to": expoPushToken,
"sound": "default",
"body": "New Note Added"
});
});
//firebase.database then() respved a single promise that resolves
//once all the messages have been resolved
return Promise.all(messages)
})
.then(messages => {
// console.log(messages)
fetch('https://exp.host/--/api/v2/push/send', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(messages)
});
})
.catch(reason => {
console.log(reason)
})
});

最佳答案

我在访问根时出错了

而不是 const root = event.data.ref.root
我应该写 const root = event.ref.root
由于最新版本的 Firebase 已更改语法

关于firebase - 在 React Native 中使用 Firebase 云功能的博览会推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62086219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com