簡體   English   中英

FCM 僅通過使用 PHP 的 Google 客戶端 API 為 iOS 設備返回 401 未經授權的錯誤

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

我正在使用 FCM HTTP v1 API 向移動設備發送通知。 后端是使用 PHP 編寫的,我正在使用 Google API 客戶端庫並使用服務帳戶授權我對 FCM 的請求。 但是我遇到了一些奇怪的問題,比如通知被發送到所有 Android 設備,但 fcm 為 iOS 設備返回 401 未經授權的錯誤。 另外,我已經使用自己的 iOS 設備進行了測試,但是當我使用自己的 iOS 設備時,fcm 總是返回 200 OK。 我很困惑問題是出在我的代碼上還是出在移動設備上的某些配置上。 這是我發送通知的方式。

$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]);

我在授權中做錯了什么? 請幫忙。 服務帳戶文件已添加到 GOOGLE_APPLICATION_CREDENTIALS 環境變量中。

FCM 使用 APNS(Apple 推送通知服務)向 iOS 設備發送消息,為此,FCM 需要連接到 Apple 服務器。
所以我們需要在 firebase 控制台中配置 APNS 憑據。 如果此配置有任何錯誤,FCM 將無法連接到 Apple 服務器,並返回 401 錯誤。

因此,請檢查設置 -> 項目配置 -> 雲消息 -> iOS 配置 -> APNS 身份驗證密鑰中的所有配置 特別是,檢查“密鑰代碼”是否正確。

有關此配置的更多信息,請訪問:

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

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

問候

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM