1

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);
      });
})
})
})
KENdi
  • 7,576
  • 2
  • 16
  • 31
user
  • 345
  • 2
  • 8
  • 32
  • did you try logging fcmToken? It seems that its empty. – GreedyAi Oct 30 '18 at 06:30
  • I'm using the UID instead of the fcmToken. As I have he fcmToken stored for each user in my database. – user Oct 30 '18 at 06:58
  • Well probably you should be using fcmToken instead. I am not sure in what you are doing but I the error says clearly that Token should not be empty. – GreedyAi Oct 30 '18 at 07:08
  • https://stackoverflow.com/questions/39157236/firebase-cloud-messaging-auth-tokens-vs-registration-token check the link its says. if you sign in to an app on two different devices, you will get the same authentication UID (although the access token will be different, each time you sign in on a device). – GreedyAi Oct 30 '18 at 07:09
  • 1
    It turns out it was because I was using my emulator and not my physical device the error was occurring but now that I use my actual device it's working fine – user Oct 30 '18 at 11:58

0 Answers0