使用Kreait的Firebase推送通知

时间:2018-08-16 00:46:16

标签: php json firebase firebase-cloud-messaging

我正在使用Kreait发送我的Firebase推送通知。我已经成功实现了它,但是eveytime我尝试发送请求,但得到的404 URL不存在错误(客户端错误:POST https://fcm.googleapis.com/v1/projects/project-id/messages:send导致未找到404)。我的google身份验证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);

1 个答案:

答案 0 :(得分:0)

使用->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.');}检查该文件的存在?