向多个设备发送云消息(FCM)单用户 [英] send cloud message (FCM) multiple devices single user

查看:55
本文介绍了向多个设备发送云消息(FCM)单用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已构建的应用程序.现在,我想使用FCM在服务器和用户之间发送消息.据我了解,每个设备都有自己的令牌,可以用来发送消息,并且可以从

I have an application which is already built. Now I want to use FCM to send messages between server and users. As I understand each device has its own token which I can use to send message, and this token can be retrieved from

FirebaseInstanceId.getInstance().getInstanceId()

纠正我,如果我错了.

应用程序和支持的服务器运行良好,并且可以简化我的工作流程

The application and backed server works fine and bellow my workflow

1-用户打开android应用.

1- User open the android app.

2- Android应用将用户令牌ID用于身份验证,并将设备令牌用于发送消息并将其发送到服务器.

2- Android app take the user token ID for authentication and device token for messageing and send them to the server.

3-后端服务器通过

decodedToken = FirebaseAuth.getInstance().verifyIdToken(login.getToken()); 

然后保存用于发送消息的令牌.

then it save the token for sending the message.

在直到遇到以下情况之前,发送邮件都可以正常工作.

Sending message working fine until I stuck with the following scenario.

该应用程序允许多个设备使用同一用户登录,这意味着1个USER可以具有多个令牌来进行消息传递.
在这种情况下,我想向该用户登录的所有设备发送消息.

The application allows multiple devices to login with same user, which means for 1 USER can have multiple tokens for messaging.
In this case I want to send message to all devices logged in with this user.

那么如何在不更改我的应用程序当前设计的情况下做到这一点

So how I can do this without changing the current design of my application

推荐答案

您需要更改数据库并添加保存一个用户的所有设备令牌的功能.然后,您需要调用此Firebase api https://fcm.googleapis.com/fcm/send令牌用户拥有的次数.这里是官方文档.

You need to change your DB and add ability to save all device tokens from one user. Then you need to call this Firebase api https://fcm.googleapis.com/fcm/send as many times as the tokens user have. Here the official documentation.

https://firebase.google.com/docs/cloud-messaging/http-server-ref

要在客户端更改设备特定的令牌时更新该令牌,您还必须将该令牌与deviceID绑定,以便在传递Firebase令牌时需要将DeviceId传递给服务器.由于deviceID可以提供Android唯一的deviceID,或在客户端 FirebaseInstanceId.getInstance().id 中提供Firebase的ID.这里是如何获取Android deviceID

To update device specific token when it is changed on client side, you also have to bound that token with the deviceID, so to have that you need to pass the DeviceId to the server when passing the Firebase token. As deviceID can serve Android unique deviceID, or the id that provides Firebase in client side FirebaseInstanceId.getInstance().id. And here how to get Android deviceID

是否有唯一的Android设备ID?

如果您使用 access_token 登录用户,作为另一种选择,您可以将Firebase令牌与用户 access_token 绑定,因为它对于用户登录的任何设备都是唯一的

If you use access_token for login the user, as another alternative, you can bound firebase token with user access_token, as it is unique for any device the user logged in.

这篇关于向多个设备发送云消息(FCM)单用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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