4

So I'm learning on how to use firebase to send device-to-device notifications and I saw this answer to send the notification and it seemed straight forward. Now, I know to get the sender's token, it should look like this:-

FirebaseInstanceId.getInstance().getToken();

but I can't for the life of me seem to find how to get the other needed token. how do I get the receiver's token so that the message goes straight to them?

Peter Haddad
  • 65,099
  • 21
  • 108
  • 107
Aria
  • 341
  • 1
  • 6
  • 20

1 Answers1

7

It is the same also for the receiver.

From the docs:

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you'll need to access this token by extending FirebaseInstanceIdService.

The registration token may change when:

The app deletes Instance ID

The app is restored on a new device

The user uninstalls/reinstall the app

The user clears app data.

So to get the token of the any device(reciever or sender) you need use this:

  FirebaseInstanceId.getInstance().getToken();

The above will generate a token for you, its better to execute the code when the user installs the app on his device(at the beginning).

Now that a token for the receiver is generated, the next thing to do is to store the token in your database to be able to use the token and send a notification to that specific user as there is no other way for you to know the token of the other device.

To store it simply do this:

 String token=FirebaseInstanceId.getInstance().getToken();
 DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Tokens");
 ref.child(user.getUid()).setValue(token);

Now this is just an example, you can store it as you want. I used the user id as it is easy to obtain since the user is the one that will be using his phone.

This way you will have the token of the receiver then you can send a notification to that specific token using cloud functions if you want.

For more info: https://firebase.google.com/docs/cloud-messaging/admin/send-messages#send_to_individual_devices

The token is being sent to the device, so the token is related to the device. Now to get the uid, then the user needs to be logged in and authenticated and then you can use the uid in the database.

If the user has many devices, then you need the token of other devices also. Again it is related to the device not to the user.

But you use the uid in the database to know this token is for who.

Summary:

  1. Token is related to device not user
  2. userid is just used to identify the user in db
  3. If user has multiple devices, then you need to generate the token of each device(same as above code).
  4. By database, I meant firebase-database
Community
  • 1
  • 1
Peter Haddad
  • 65,099
  • 21
  • 108
  • 107
  • when you mean database do you mean on firebase? or the database associated with my application? also, and idk if you could answer this, but if a user is logged into the application on multiple devices, would it still be able to use one token and send to all? and do I also generate the tokens only after the user is logged in? – Aria Feb 02 '18 at 19:48
  • yes in firebase database. You can generate the token whenever you want, if the user logins then u can get his uid to be able to use it in the database. So after login, then you can get the uid. if multiple devices, no it wont since each device has it's own token – Peter Haddad Feb 02 '18 at 19:52
  • 2
    thank you so much. I'll try this out and if it works, I'll mark it as the answer. thank you – Aria Feb 02 '18 at 20:10
  • Hi, I have a question on this too, hope you can answer. I have successfully save user device token in my mongodb database, and I can retrieve the token based on each userid, but I still cannot send notification if I pass the token to fcm-node send. Do I need to store the device token in firebase database too to work? – Thomas Kim Jan 19 '19 at 03:54