使用用户的UID发送Firebase Cloud消息 [英] Sending Firebase Cloud Message using UID of user

查看:106
本文介绍了使用用户的UID发送Firebase Cloud消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个Android应用,允许用户向其他用户发送通知和数据消息.我只是最近才开始使用Firebase,而Firebase Cloud Messaging仍然让我感到困惑.

我已经实现了使用Firebase实例ID向特定的设备发送消息.但是,在我的应用程序中,用户可以使用其他设备注销并登录其帐户,所以这并不是我真正想要的.我阅读了文档,这非常令人困惑,他们提到将消息发送给用户组和主题(只有应用程序可以订阅,用户不能订阅).

有没有一种方法可以仅使用其UID直接向另一个用户或一组用户发送推送通知?如果没有,我还有其他方法可以实现吗?

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 device 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?

推荐答案

好的,因此,为确保用户登录时该功能可在多个设备上使用,我只是让设备订阅一个名称与用户名称相同的主题UID.现在,如果我想向用户发送通知,我只需发送有关该主题的FCM消息,其名称等于用户的UID!

示例:如果我的UID等于"asdf",则每当我使用多个设备登录时,每个设备都会自动订阅名称为"asdf"的主题.因此,现在,如果我从服务器发送有关此主题的FCM,则登录我的所有设备都将获得此通知.

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.

这篇关于使用用户的UID发送Firebase Cloud消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆