我正在尝试为用户实现推送通知,使用expo令牌、firebase云函数和react原生前端。
这就是我在前端做事情的方式,它正确地调用了函数:
const writeNotification = Firebase.functions().httpsCallable('writeNotification');
writeNotification({
type: 0,
senderUID: this.state.likerUID,
recieverUID: this.state.posterUID,
postID: this.state.postID,
likerUsername: this.state.likerUsername
})
.then((result) => {
console.log(result)
})
.catch((error) => {
console.log(error);
});云函数如下:
exports.writeNotification = functions.https.onCall((data, context) => {
//Get the information of the user who is going to recieve the notification
admin.firestore()
.collection('users')
.doc(data.recieverUID)
.get()
.then(function(doc) {
if (doc.exists) {
//Find out if the user will accept push notifications
if (doc.data().pushStatus) {
var messages = []
//Write the notification and add it to messages
messages.push({
"to": doc.data().token,
"sound": "default",
"title":"you got a like!",
"body": data.likerUsername + " liked your post!"
});
//Post it to expo
fetch('https://exp.host/--/api/v2/push/send', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(messages)
});
return (
"notification written"
)
}
else {
console.log("doesnt accept push notifications: " + doc.data().username)
return
}
} else {
// doc.data() will be undefined in this case, so this wont even come up honestly
console.log("No such document!");
return
}
})
.catch(function(error) {
console.error("Error finding user: ", error);
});
return (
true
)
});我的问题是fetch,我在云函数日志中得到了这个错误:

所以看起来这个问题是在写expo的时候出现的。这部分我做错了吗?感谢任何帮助,这是我第一次实现推送通知
发布于 2021-01-14 11:02:42
我忘了将这个添加到云函数文件中:
const fetch = require('node-fetch');代码现在可以工作了,获取推送通知:)
https://stackoverflow.com/questions/65712600
复制相似问题