在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]"
Run Code Online (Sandbox Code Playgroud)
因此,当创建新消息时,我需要向所有 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),
});
})
Run Code Online (Sandbox Code Playgroud)
这是我已经开始的代码,但它不起作用。
const functions = require('firebase-functions');
var fetch = require('node-fetch')
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendPushNotification = …Run Code Online (Sandbox Code Playgroud) push-notification firebase react-native expo google-cloud-firestore