简体   繁体   English

Firebase云设备到设备推送通知设备令牌与主题

[英]Firebase cloud device to device push notifications device token vs topics for

I just got firebase function push notifications working for the first time. 我刚得到firebase函数推送通知,这是第一次工作。 I'm monitoring a change with : 我正在使用监视更改:

exports.observeNotifications = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((change, context) => {

Later, I use : 以后,我用:

const userQuery = admin.database().ref(`users/${from_user_id}/username`).once('value');
const deviceToken = admin.database().ref(`/users/${user_id}/device_token`).once('value');

to retrieve the sender's userUID and device token which is later used with sendToDevice() 检索发送者的userUID和设备令牌,该令牌随后与sendToDevice()

This works fine and is the method I see recommended everywhere, but I'm wondering why to do this over using topics in a user to user scenario. 这很好用,是我在各处推荐的方法,但是我想知道为什么要这样做而不是在用户到用户方案中使用主题。

In the example of a chat application, every time a chat is sent, a value would be sent to notifications/users/{uid} ... and trigger the event. 在聊天应用程序的示例中,每次发送聊天时,都会向notifications/users/{uid} ...发送一个值并触发事件。 Then the value calls have to be made, promise handling has to be done, and then the push payload can be configured and sent. 然后必须进行值调用,必须完成承诺处理,然后可以配置和发送推送有效负载。

With topics, when the user first loads up the application for the firs time, you could subscribe the user to a topic like "chat_notifications_usersIUID". 使用主题时,当用户首次在第一时间加载应用程序时,可以为该用户订阅一个主题,例如“ chat_notifications_usersIUID”。 This negates the need to fetch device tokens and go through the process of using promises and greatly simplifies the process of sending a notification to a specific user down to just pushing to a certain topic that is specific to the recipients UID. 这样就不需要获取设备令牌并经历使用承诺的过程,并且大大简化了将通知发送给特定用户的过程,简化为仅推送到特定于收件人UID的特定主题。

Are there any downsides to using topics over firebase function observes when sending a push notification from user to user(s). 在每个用户之间发送推送通知时,在Firebase函数上使用主题有任何不利之处。

Topics are publicly accessible. 主题可以公开访问。 So even if you create a topic for each user, all users can subscribe to each of those topics. 因此,即使您为每个用户创建一个主题,所有用户都可以订阅每个主题。

For that reason you should only use topics in this way, if the messages are consider public. 因此,如果消息被认为是公开的,则仅应以这种方式使用主题。 Eg in a public chat room scenario this would probably be fine, since everyone can already see the messages in the public chat room anyway. 例如,在公共聊天室中,这可能会很好,因为每个人都已经可以在公共聊天室中看到消息了。 But sending private messages via a topic, means that other users can intercept them when they know the topic for a user. 但是通过主题发送私人消息意味着其他用户在为用户了解主题时可以拦截它们。

If your messages are not meant to be public, you will need to send to the individual tokens. 如果您的消息不打算公开,则需要发送给各个令牌。 Your should only hit a rate limit in extreme cases. 您只应在极端情况下达到速率限制。 If that happens for you, reach out to Firebase support for personalized help in troubleshooting 如果您遇到这种情况,请联系Firebase支持以获取故障排除方面的个性化帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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