【发布时间】:2022-07-24 16:49:22
【问题描述】:
我正在尝试实现一个使用 FirebaseCloudMessaging 发送通知的功能,我得到了 Auth 部分,但似乎 GuzzleHttp\Client 以及它如何通过 data 部分存在问题
这是我的代码
public function send(){
putenv('GOOGLE_APPLICATION_CREDENTIALS=<path-to-json-file>.json');
$scopes = ['https://www.googleapis.com/auth/firebase.messaging'];
$middleware = ApplicationDefaultCredentials::getMiddleware($scopes);
$stack = HandlerStack::create();
$stack->push($middleware);
// create the HTTP client
$client = new Client([
'handler' => $stack,
'base_uri' => 'https://fcm.googleapis.com',
'auth' => 'google_auth'
]);
$data = json_encode(
[
"message" => [
"topic" => "availableProviders",
"notification" => [
"title" => "New Order",
"body" => "New Order to accept",
],
"data" => [
"service_id" => '1'
],
],
"validateOnly" => false,
],
);
$response = $client->post('/v1/projects/<Project-id>/messages:send', [
[
'json' => $data,
'headers' => [
'accept' => 'application/json',
'Content-Type' => 'application/json',
],
]
]);
dd($response);
}
我不断收到响应 400,INVALID_ARGUMENT “消息未设置。”
Client error: POST https://fcm.googleapis.com/v1/projects//messages:sendresulted in a400 错误请求 response: { "error": { "code": 400, "message": "The message is not set.", "status": "INVALID_ARGUMENT", "details (truncated...)
虽然我的数据结构作为documented 传递,但我在这里缺少什么? 注意:我使用 Laravel,我隐藏了 等。
【问题讨论】:
标签: php firebase-cloud-messaging