I have the below code where I am trying to send FCM push notification to an android app targetting specific device. In terminal upon running php push.php I get no result and no errors too.
Please help me what is wrong here, I am able to send using the legacy FCM APIs but I could not using HTTP v1
<?php
$serviceAccount = json_decode(file_get_contents('privatekey.json'), true);
$serverKey = $serviceAccount['private_key'];
$projectId = $serviceAccount['project_id'];
$data = [
'message' => [
'token' => 'd6dYpnaLSP2zRk4o6dFfFZ:APA91bG-ZB7XFB-01K54cEVAiyMuqbKyBGhYX-XeiR2QcC3QKv1-MXI4wAy_cwL-d1TLNOUkF-pqt0WQed4b2VcL8OeBxXOPVwrMiWv69TkDRRidOPUw_YjA-ISsgCNoxlWUIb5V_PV6',
'notification' => [
'title' => 'Your Title',
'body' => 'Your Message',
],
],
];
$headers = [
'Authorization: Bearer ' . $serverKey,
'Content-Type: application/json',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/v1/projects/notifizy/messages:send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
I have tried all, legacy FCM API works but not this.
2
Answers
// Android app targeting specific device you need RECIVER_TOKEN
// and get your SERVER API KEY and follow below code
Simplest way to implement Push notification in PHP