简体   繁体   English

Firebase:在所有设备上向用户发送推送通知

[英]Firebase: Send push notification to user on all their devices

I am building an app similar to Instagram. 我正在构建一个类似于Instagram的应用程序。 User uploads a post and other others can like it. 用户上传帖子,其他人都可以喜欢。 When people like the post, I want the post author to get a push notification using FCM (Firebase Cloud Messaging). 当人们喜欢该帖子时,我希望该帖子的作者使用FCM(Firebase Cloud Messaging)获得推送通知。

My problem is that the refreshToken (that is generated using FirebaseInstanceId.getInstance().getToken(); ) is specific to the device. 我的问题是, refreshToken (使用FirebaseInstanceId.getInstance().getToken(); )是特定于设备的。 So if the same user logs into the app from a different device, I want to make sure I send a push notification to all the devices. 因此,如果同一用户从另一台设备登录到应用程序,则要确保我向所有设备发送推送通知。 So here is my proposed solution, I want to know if it's the right way to do it: 所以这是我建议的解决方案,我想知道这是否是正确的方法:

Every time user logs into a device, I store the refreshToken into the database and when I want to generate a push notification, I can iterate over all those refreshTokens and send a push notification to all those devices. 每次用户登录到设备时,我都会将refreshToken存储到数据库中,并且当我想要生成推送通知时,可以遍历所有这些refreshTokens并将推送通知发送到所有这些设备。

Is there a better way to accomplish this? 有没有更好的方法可以做到这一点?

Note: I am aware of Device Group Messaging and I can't use that because I don't have an app server. 注意:我知道设备组消息传递,因此无法使用它,因为我没有应用程序服务器。 And I don't want to do it on the client side because that requires the client to have a Google account. 而且我不想在客户端执行此操作,因为这需要客户端拥有Google帐户。

Yes, the implementation is okay. 是的,实施还可以。

Every time user logs into a device, I store the refreshToken into the database. 每次用户登录设备时,我都会将refreshToken存储到数据库中。

This is actually not necessary to send the device push registration id (refreshToken in your case) each time user logs into your application from a device. 每次用户从设备登录到您的应用程序时,实际上不必发送设备推送注册ID(在您的情况下为refreshToken)。 You need to send the push registration id (ie refreshToken) from your device only when the device is not known to the server yet (ie new device). 仅当服务器尚不知道该设备(即新设备)时,才需要从您的设备发送推送注册ID(即refreshToken)。

Is there a better way to accomplish this? 有没有更好的方法可以做到这一点?

I think no. 我觉得不行。 This is the perfect way that you have implemented. 这是您实施的理想方式。 However, we might always think of several optimization points like, tracking which devices are logged off, so that you do not need to send the push notification to that device any more. 但是,我们可能总是会想到几个优化点,例如跟踪哪些设备已注销,这样您就不再需要向该设备发送推送通知。 So in case of logging off from a device, send another invalidate refreshToken request to the server from that device, so that it does not get any further push notification. 因此,如果从设备注销,请从该设备向服务器发送另一个无效的refreshToken请求,这样它就不会再收到任何推送通知。 This is an improvement over the current implementation. 这是对当前实现的改进。

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

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