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?