简体   繁体   中英

Sending Firebase Cloud Message using UID of user

I want to build an Android app that allows a user to send notification and data messages to other users. I've started using Firebase only recently, and Firebase Cloud Messaging is still pretty confusing to me.

I have already implemented sending a message to a specific using the Firebase Instance Id. 发送消息的方法。 However, in my app, a user can log out and log into their accounts using different devices, so this isn't really what I want. I read the documentation and it's pretty confusing, they mention sending messages to user groups, and topics (which only apps can subscribe to, not users).

Is there a way to send a push notification directly to another user or a group of users using only their UIDs? If not, is there any other way I can implement this?

I did it storing the FCM Token in a device structure by user. When the user login, add the deviceData to the userToken structure. You need clear deviceData when the user logout. This way, you will only send notifications to logged devices.

Ex (This is not like mine structure, but it can help you to wondering a good way to do this):

-userToken
  -idUser1
     -device
         -idDevice1
             -fcmToken: "xxxxx"
         -idDevice2
             -fcmToken: "YYYYY"

Hope that I helped you

Alright, so to make sure this works across multiple devices when a user logs in, I just make the device subscribe to a topic with the name equal to the user's UID. Now if I want to send a notification to a user, I just send an FCM message on the topic with the name equal to the user's UID!

Example: If my UID is equal to "asdf", whenever I login using multiple devices, each of the devices automatically subscribes to the topic with name "asdf". So now, if I send an FCM from the server on this topic, all the devices from which I am logged on to obtain this notification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM