获取接收方的设备令牌,以便在Firebase中发送通知 [英] Get receiver's device token to send notification in Firebase

查看:629
本文介绍了获取接收方的设备令牌,以便在Firebase中发送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在学习如何使用Firebase发送设备到设备通知,并且我看到了答案发送通知,这似乎很简单。
现在,我知道要获取发件人的令牌,它应该如下所示: -

$ p $ FirebaseInstanceId.getInstance() .getToken();

但我无法为我的生活找到如何获得其他需要的令牌。我如何获得接收者的令牌,以便消息直接传递给他们? 解决方案

接收者也是如此。



来自文档: $ b


在您的应用程序初次启动时,FCM SDK为客户端应用程序实例生成注册令牌。如果您想要定位单个设备或创建设备组,您需要通过扩展FirebaseInstanceIdService来访问此令牌。



注册令牌可能会在以下情况下发生变化:



该应用程序删除实例ID



该应用程序已在新设备上恢复



用户卸载/重新安装应用程序

用户清除应用程序数据。


因此,要获取任何设备(接收者或发件人)的令牌,您需要使用以下命令:

  FirebaseInstanceId。 。的getInstance()为gettoken(); 

上面的代码会为您生成一个令牌,当用户安装应用程序时它更好地执行代码在他的设备上(开始时)。

现在接收器的令牌已经生成,接下来要做的就是将令牌存储在数据库中以便能够使用该令牌并向该特定用户发送通知,因为没有其他方式可以让您知道其他设备的令牌。



存储它只需执行此操作:

  String token = FirebaseInstanceId.getInstance()。getToken(); 
DatabaseReference ref = FirebaseDatabase.getInstance()。getReference()。child(Tokens);
ref.child(user.getUid())。setValue(token);

现在这只是一个例子,您可以随意存储它。我使用了用户ID,因为用户很容易获得,因为用户是使用他的手机的用户。



这样,您将获得接收器的令牌,然后如果需要,您可以使用云端功能向该特定令牌发送通知。



更多信息: https://firebase.google.com/docs/cloud-messaging/admin/send-messages#send_to_individual_devices

令牌正在发送到设备,因此令牌与设备相关。现在要获取uid,那么用户需要登录并进行身份验证,然后才能在数据库中使用uid。



如果用户有许多设备,那么您还需要其他设备的令牌。 与设备不相关的是

但是,您使用数据库中的uid来知道此令牌是为谁设计的。
$ b 摘要:


  1. 令牌与设备无关用户

  2. userid仅用于在db中识别用户

  3. 如果用户有多个设备,那么您需要生成每个设备的标记(与上面的代码相同)。
  4. 通过数据库,我的意思是firebase-database


So I'm learning on how to use firebase to send device-to-device notifications and I saw this answer to send the notification and it seemed straight forward. Now, I know to get the sender's token, it should look like this:-

FirebaseInstanceId.getInstance().getToken();

but I can't for the life of me seem to find how to get the other needed token. how do I get the receiver's token so that the message goes straight to them?

解决方案

It is the same also for the receiver.

From the docs:

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you'll need to access this token by extending FirebaseInstanceIdService.

The registration token may change when:

The app deletes Instance ID

The app is restored on a new device

The user uninstalls/reinstall the app

The user clears app data.

So to get the token of the any device(reciever or sender) you need use this:

  FirebaseInstanceId.getInstance().getToken();

The above will generate a token for you, its better to execute the code when the user installs the app on his device(at the beginning).

Now that a token for the receiver is generated, the next thing to do is to store the token in your database to be able to use the token and send a notification to that specific user as there is no other way for you to know the token of the other device.

To store it simply do this:

 String token=FirebaseInstanceId.getInstance().getToken();
 DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Tokens");
 ref.child(user.getUid()).setValue(token);

Now this is just an example, you can store it as you want. I used the user id as it is easy to obtain since the user is the one that will be using his phone.

This way you will have the token of the receiver then you can send a notification to that specific token using cloud functions if you want.

For more info: https://firebase.google.com/docs/cloud-messaging/admin/send-messages#send_to_individual_devices

The token is being sent to the device, so the token is related to the device. Now to get the uid, then the user needs to be logged in and authenticated and then you can use the uid in the database.

If the user has many devices, then you need the token of other devices also. Again it is related to the device not to the user.

But you use the uid in the database to know this token is for who.

Summary:

  1. Token is related to device not user
  2. userid is just used to identify the user in db
  3. If user has multiple devices, then you need to generate the token of each device(same as above code).
  4. By database, I meant firebase-database

这篇关于获取接收方的设备令牌,以便在Firebase中发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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