我使用 Kreait 发送我的 firebase 推送通知。我成功地实现了它,但是当我尝试发送请求时,我收到一个 404 url 不存在错误(客户端错误:POST https://fcm.googleapis.com/v1/projects/project-id/messages:send
导致 '404 未找到(。我的谷歌身份验证json文件与我的php文件位于同一目录中。
法典:
$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/google.json');
$firebase = (new Factory)
->withServiceAccount($serviceAccount)
// The following line is optional if the project id in your credentials file
// is identical to the subdomain of your Firebase project. If you need it,
// make sure to replace the URL with the URL of your project.
//->withDatabaseUri('https://my-project.firebaseio.com')
->create();
$messaging = $firebase->getMessaging();
$deviceToken = 'token';
$notification = Notification::create("test", "body");
$data = [
'first_key' => 'First Value',
'second_key' => 'Second Value',
];
$message = CloudMessage::withTarget('token', $deviceToken)
->withNotification($notification) // optional
->withData($data);
// error here
// 404 Client error: `POST https://fcm.googleapis.com/v1/projects/projectid/messages:send` resulted in a `404 Not Found
$messaging->send($message);
使用->withDatabaseUri('https://INSERT_YOUR_PROJECT_ID_HERE.firebaseio.com')
...虽然看起来,很可能__DIR__.'/google.json'
(通常称为google-services.json
(不存在,因此项目 URL 将是未知的。在每种情况下,URL 中的projectid
都必须替换为实际project_id
......也许用if(! file_exists(__DIR__.'/google.json')) {die('config absent.');}
检查该文件是否存在?