I am trying to implement push notifications for users, using expo tokens, firebase cloud functions, and a react native frontend.
This is how I did things on the frontend, which calls the function correctly:
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);
});
Here is the cloud function:
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
)
});
My issue is with the fetch, and am getting this error in the cloud function logs:
So it looks like the issue occurs when writing to expo. Am I doing that part wrong? Any help appreciated, this is my first time implementing push notifications