1

I'm very new with push notifications and FCM (Firebase Cloud Messaging) in particular.

I have an app where the user can log in and I want to send push notification to this specific user. The user can be logged in on multiple devices at the same time so the notification must be sent to all this devices.

I know how to send a notification to a specific device as I've followed the tutorial from the official documentation, and I have a server where the user is registered.

So I was thinking of saving the token (FirebaseInstanceId.getInstance().getToken()) to my server to then send a push notification to all the tokens saved under this user.

But there are two issues:

  • What happen if the user logs out and another user logs in as the token is gonna be the same, so it can be a source of errors?
  • As the token can expires or not be relevant anymore (the user has changed device or has deleted the app), how can I know which tokens to keep in my database?

That's why I'm thinking that my approach is maybe wrong and there is probably a better way to do that.

I've seen that you can manage users directly in Firebase, by authenticate your user using Firebase Authentication. So I'm wondering if this is what I need to do?

AL.
  • 36,815
  • 10
  • 142
  • 281
Eselfar
  • 3,759
  • 3
  • 23
  • 43

1 Answers1

3

What happen if the user logs out and another user logs in as the token is gonna be the same, so it can be a source of errors?

Handle the logout properly.

As the token can expires or not be relevant anymore (the user has changed device or has deleted the app), how can I know which tokens to keep in my database?

You can check it from your server side.

AL.
  • 36,815
  • 10
  • 142
  • 281
  • Since you asked two questions in a single post, it's a hard to tag it as a duplicate, but in order of the questions, I'll go ahead and tag your post as a duplicate to the first one. Cheers! – AL. Jun 22 '18 at 12:29
  • Thank for answer. My question was more on how to manage multiple users on a single device for the correct notification to be sent. But if I understand correctly, my implementation is correct and I don't need to use Firebase Authentication? – Eselfar Jun 22 '18 at 13:08
  • 1
    In order to handle multiple users, if the user logs out, you invalidate the token immediately, which would in turn generate a new token via `onTokenRefresh()`. After a new user signs in, you could then pair that new token to that user. – AL. Jun 22 '18 at 16:29
  • 1
    Thanks for your help, I think I now have a better understanding on how it works :) – Eselfar Jun 22 '18 at 16:34