问题
I have an application which is already built. Now I want to use FCM to send messages between server and users. As I understand each device has its own token which I can use to send message, and this token can be retrieved from
FirebaseInstanceId.getInstance().getInstanceId()
Correct me If I am wrong.
The application and backed server works fine and bellow my workflow
1- User open the android app.
2- Android app take the user token ID for authentication and device token for messageing and send them to the server.
3- The back end server verify the user via
decodedToken = FirebaseAuth.getInstance().verifyIdToken(login.getToken());
then it save the token for sending the message.
Sending message working fine until I stuck with the following scenario.
The application allows multiple devices to login with same user, which means for 1 USER can have multiple tokens for messaging.
In this case I want to send message to all devices logged in with this user.
So how I can do this without changing the current design of my application
回答1:
You need to change your DB and add ability to save all device tokens from one user. Then you need to call this Firebase api https://fcm.googleapis.com/fcm/send as many times as the tokens user have. Here the official documentation.
https://firebase.google.com/docs/cloud-messaging/http-server-ref
To update device specific token when it is changed on client side, you also have to bound that token with the deviceID, so to have that you need to pass the DeviceId to the server when passing the Firebase token. As deviceID can serve Android unique deviceID, or the id that provides Firebase in client side FirebaseInstanceId.getInstance().id
. And here how to get Android deviceID
Is there a unique Android device ID?
If you use access_token
for login the user, as another alternative, you can bound firebase token with user access_token
, as it is unique for any device the user logged in.
来源:https://stackoverflow.com/questions/61107459/send-cloud-message-fcm-multiple-devices-single-user