I'm trying to set up push notifications, but I'm getting a few errors in firebase logs and I can't seem to figure out why. Could someone help fix my code.
Errors I'm getting are:
Uncaught exception
Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array
exports.observeFollowing = functions.database.ref('/follwoing/{uid}/{followingId}').onCreate((snapshot,context) => {
var uid = context.params.uid;
var followingId = context.params.trustingId;
console.log('User: ' + uid + 'is trusting: ' + followingId);
admin.database().ref('/users/' + followingId).once('value', snapshot => {
var userWeAreFollowing = snapshot.val();
admin.database().ref('/users/' + uid).once('value',snapshot => {
var userDoingTheFollowing = snapshot.val();
var payload = {
notification: {
title: "Someone new has followed you",
body: "User XYZ has started trusting you...",
sound: 'default'
}
}
admin.messaging().sendToDevice(userWeAreFollowing.fcmToken, payload)
.then((response) => {
console.log('Successfully sent message:', response);
return response
})
.catch((error) => {
console.log('Error sending message:', error);
});
})
})
})