简体   繁体   English

FCM 仅通过使用 PHP 的 Google 客户端 API 为 iOS 设备返回 401 未经授权的错误

[英]FCM returning 401 unauthorized error for iOS devices only with Google client API using PHP

I am using FCM HTTP v1 API for sending notifications to mobile devices.我正在使用 FCM HTTP v1 API 向移动设备发送通知。 The backend is written using PHP and I am using Google API client library and using a service account to authorize my requests to FCM.后端是使用 PHP 编写的,我正在使用 Google API 客户端库并使用服务帐户授权我对 FCM 的请求。 But I am facing some weird issues like notifications are being sent to all Android devices, but fcm returns 401 unauthorized error for iOS devices.但是我遇到了一些奇怪的问题,比如通知被发送到所有 Android 设备,但 fcm 为 iOS 设备返回 401 未经授权的错误。 Also, I have tested using my own iOS device but fcm always returns 200 OK when I am using my own iOS device.另外,我已经使用自己的 iOS 设备进行了测试,但是当我使用自己的 iOS 设备时,fcm 总是返回 200 OK。 I am so confused whether the issue is with my code or some configuration on the mobile device.我很困惑问题是出在我的代码上还是出在移动设备上的某些配置上。 Here is how I am sending the notifications.这是我发送通知的方式。

$client = new Google_Client();
$client->useApplicationDefaultCredentials(); 
$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
$httpClient = $client->authorize();
$message = [
    "message" => 
    [
        "token" => $device_token,
        "notification" => 
        [
            "body" => $body,
            "title" => "xxxxx"
        ],
        "apns" => 
        [
            "payload" =>
            [
                "aps" => 
                [
                  "category" =>  "NEW_MESSAGE_CATEGORY",
                  "alert" =>  [
                     "body" =>  $body
                  ],
                  "badge" =>  3,
                  "sound" =>  "default",
                  "chat" => 2

                ]
            ]  
        ]
    ]
];
$response = $httpClient->post("https://fcm.googleapis.com/v1/projects/xxxx-123/messages:send", ['json' => $message]);

What am I doing wrong in authorization?我在授权中做错了什么? Please help.请帮忙。 Service account file is already added in GOOGLE_APPLICATION_CREDENTIALS env variable.服务帐户文件已添加到 GOOGLE_APPLICATION_CREDENTIALS 环境变量中。

FCM uses APNS (Apple Push Notification Service) to delivery messages to iOS devices, in order to do it, FCM need to connect to Apple server. FCM 使用 APNS(Apple 推送通知服务)向 iOS 设备发送消息,为此,FCM 需要连接到 Apple 服务器。
So we need to configure APNS credentials in firebase console.所以我们需要在 firebase 控制台中配置 APNS 凭据。 If there is any error in this configuration, FCM can't connect to Apple server, and return 401 error.如果此配置有任何错误,FCM 将无法连接到 Apple 服务器,并返回 401 错误。

So check all configs in Settings -> Project Configuration -> Cloud Messaging -> iOS Configuration -> APNS Authentication Key In special, check if the " key code " are correct.因此,请检查设置 -> 项目配置 -> 云消息 -> iOS 配置 -> APNS 身份验证密钥中的所有配置 特别是,检查“密钥代码”是否正确。

More info about this configs can be found at:有关此配置的更多信息,请访问:

https://firebase.google.com/docs/cloud-messaging/ios/client#upload_your_apns_authentication_key https://firebase.google.com/docs/cloud-messaging/ios/client#upload_your_apns_authentication_key

https://firebase.google.com/docs/cloud-messaging/ios/certs https://firebase.google.com/docs/cloud-messaging/ios/certs

Regards问候

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

相关问题 亚马逊广告 API 抛出 HTTP 401 未经授权 - Amazon Advertising API throws HTTP 401 Unauthorized 尝试从 Google Container Registry 拉取图像时出现 401 Unauthorized 错误 - 401 Unauthorized error while trying to pull image from Google Container Registry Azure AD API 请求401未授权 - Azure AD API request 401 Unauthorized Google 表格 API 只返回不必要的信息 - Google Sheets API only returning unecessary information 当 lambda 失败时,AWS API 网关 401 未授权 - AWS API Gateway 401 Unauthorized when lambda fails PHP BigQuery 客户端错误代码:401 running long job - PHP BigQuery Client Error Code: 401 running long job GKE pod 无法访问私有 GCS 存储桶(401 未授权错误) - GKE pod not able to access private GCS Bucket (401 unauthorized error) Gmail API 服务帐户 unauthorized_client 错误,即使具有全域权限 - Gmail API service account unauthorized_client error even with domain-wide authority angular8 中的 Twilio API 未授权“401 未授权”但在 Postman 上工作正常 - Twilio API in angular8 Not Authorized " 401 Unauthorized" but working fine on Postman API 网关 Cognito 用户池授权方 - 401 未经授权 - API gateway Cognito user pool authorizer - 401 unauthorized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM