【发布时间】:2021-04-19 02:17:48
【问题描述】:
我正在尝试使用 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
)
});
我的问题在于获取,并且在云功能日志中收到此错误:
所以看起来问题是在写到 expo 时发生的。我做错了那部分吗?感谢任何帮助,这是我第一次实现推送通知
【问题讨论】:
标签: javascript react-native google-cloud-functions expo