【发布时间】:2019-07-01 06:31:47
【问题描述】:
我正在使用 React-Native 和 Expo。 我正在为我的应用程序做推送通知。 当用户收到新任务时,会通过 expo 显示通知。
这是 Firebase index.js 的代码
const functions = require('firebase-functions');
var fetch = require('node-fetch')
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
//send the push notification
exports.sendPushNotification =
functions.database.ref('Task/').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 expoToken = childSnapshot.val().expoToken;
messages.push({
"to": expoToken,
"sound": "default",
"body": "New Task 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)
})
});
我在执行“firebase deploy”时收到以下错误
谁能帮帮我,谢谢。
【问题讨论】:
标签: firebase react-native firebase-realtime-database google-cloud-functions expo