Search code examples
phpfirebasefirebase-cloud-messaging

Firebase push notifications php curl returning null


I am making a flutter app and I am using Firebase to do push notifications. I am not sure what is going wrong here. It looks like it should work I think but I get no response back. Am I missing something?

Here is my code:


$fcmUrl = 'https://fcm.googleapis.com/fcm/send';

$token = $_POST['token'];

$notification = [
    'title' => 'title',
    'body' => 'body of message.',
    'icon' => 'myIcon',
    'sound' => 'mySound'
];
$extraNotificationData = ["message" => $notification, "moredata" => 'dd'];

$fcmNotification = [
    //'registration_ids' => $tokenList, //multple token array
    'to' => $token, //single token
    'notification' => $notification,
    'data' => $extraNotificationData
];

$headers = [
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
];


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fcmUrl);
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_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
$result = curl_exec($ch);
curl_close($ch);


echo $result;

Solution

  • It was within my flutter app. It had to do with how I was reading the incoming push. I wasn't doing anything with the data coming in while the app was active so of course it wouldn't work. It wasn't until I ran the script with the app closed that I saw the notification.