如何在php/laravel中验证来自Firebase的设备收到的令牌?

huangapple go评论58阅读模式
英文:

How to verify received token from firebase for device in php/laravel?

问题

我有一个移动应用程序,我从Firebase获取设备令牌,通过用户的设备令牌向设备发送通知。

我没有使用Firebase/Google身份验证等。

我只是使用云消息传递来发送通知,我需要设备令牌,以便向特定用户发送通知。现在,我需要在将令牌存储到数据库之前验证接收到的令牌。

如何验证从Firebase接收到的设备令牌,以确保该令牌存在/真实?

英文:

I have a mobile application which I'm getting device token from firebase, to send notification to device by user's device token.

I'm not using firebase/google authentication or etc.

I'm just using cloud messaging to send notification, which I need device token, to send notifications to specific users and right now, I need to validate the token before application store token in DB.

How to verify received device token from firebase to make sure the token is exist/real?

答案1

得分: 0

以下是翻译好的部分:

最简单的方法是使用令牌发送一个基本的空消息。

如果输入错误的令牌,你会收到以下响应:

成功响应的情况下,你的结果将是:

所以一般来说,需要解码 JSON 响应并检查结果。

英文:

Easiest way was to send a basic and empty message using the token.

$response = Http::withHeaders(['Authorization' => 'key=token'])
   ->post('https://fcm.googleapis.com/fcm/send', [
            'registration_ids' => [ tokens here ],
        ]);

If you enter wrong token, you'll get response like below :

{
    "multicast_id": xxxxxxxxxxxxxxxxx,
    "success": 0,
    "failure": 1,
    "canonical_ids": 0,
    "results": [
        {
            "error": "InvalidRegistration"
        }
    ]
}

Which in case of success response your result will be :

{
    "multicast_id": xxxxxxxxxxxxxxxxx,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "x:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        }
    ]
}

So in general, need to decode json response and just check the result.

huangapple
  • 本文由 发表于 2023年4月20日 01:19:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定