Firebase云设备到设备推送通知设备令牌与主题 [英] Firebase cloud device to device push notifications device token vs topics for

查看:165
本文介绍了Firebase云设备到设备推送通知设备令牌与主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚获得了Firebase函数推送通知,这是第一次使用. 我正在监视:

I just got firebase function push notifications working for the first time. I'm monitoring a change with :

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

后来,我使用:

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

检索发送者的userUID和设备令牌,该令牌随后与sendToDevice()

to retrieve the sender's userUID and device token which is later used with 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.

在聊天应用程序的示例中,每次发送聊天时,都会将值发送到notifications/users/{uid} ...并触发事件.然后必须进行值调用,必须完成承诺处理,然后可以配置和发送推送有效负载.

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. Then the value calls have to be made, promise handling has to be done, and then the push payload can be configured and sent.

对于主题,当用户第一次加载应用程序时,您可以为该用户订阅诸如"chat_notifications_usersIUID"之类的主题.这消除了获取设备令牌并经历使用承诺的过程的需要,并且大大简化了向特定用户发送通知的过程,简化为仅推送到特定于收件人UID的特定主题.

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". 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.

在用户之间发送推送通知时,在Firebase功能上使用主题是否有不利之处.

Are there any downsides to using topics over firebase function observes when sending a push notification from user to user(s).

推荐答案

主题可以公开访问.因此,即使您为每个用户创建一个主题,所有用户都可以订阅这些主题中的每个主题.

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. E.g. 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.

如果您的消息不打算公开,则需要发送给各个令牌.您只应在极端情况下达到速率限制.如果您遇到这种情况,请获得Firebase支持,以获得个性化的故障排除帮助

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云设备到设备推送通知设备令牌与主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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