[英]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.