当前位置 : 主页 > 网页制作 > Nodejs >

node.js – 如何验证服务器上的FCM注册令牌?

来源:互联网 收集:自由互联 发布时间:2021-06-16
我获得了用于网络推送的Firebase Cloud Messaging注册令牌. 然后我将它发送到我的服务器以保存在数据库中以便稍后推送. 但是,如何验证此令牌是有效还是假的? 我已经尝试过this,但我认为
我获得了用于网络推送的Firebase Cloud Messaging注册令牌.
然后我将它发送到我的服务器以保存在数据库中以便稍后推送.
但是,如何验证此令牌是有效还是假的?

我已经尝试过this,但我认为这是针对Auth令牌而不是网络推送.

其他人可以向我的服务器发送随机假令牌的请求.我想在保存在db之前阻止这个.

编辑:它已经解决了,我写了一个简单的类来快速使用FCM进行Web推送.
https://github.com/emretekince/fcm-web-push

发送到无效的注册令牌时,您应该收到 200 + error:InvalidRegistration:

Check the format of the registration token you pass to the server. Make sure it matches the registration token the client app receives from registering with Firebase Notifications. Do not truncate or add additional characters.

当您尝试发送一个简单的cURL请求时,这是响应,其中注册令牌是随机发生的:

curl --header "Authorization: key=$[your_server_key_here]" \
       --header Content-Type:"application/json" \
       https://fcm.googleapis.com/fcm/send \
       -d "{\"registration_ids\":[\"ABC\"]}"

请注意,我在registration_ids参数中添加了“ABC”.如果它是有效的注册令牌,但与您的项目无关,您可能会收到200 + error:NotRegistered.

您可以尝试从服务器发送测试消息以查看响应,而无需使用dry_run参数向设备发送实际消息:

This parameter, when set to true, allows developers to test a request without actually sending a message.

网友评论