Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ZandA's avatar
Level 1

Firebase push notification to mobile

Anyone faces this problem with pushing firebase notification from backend? I followed every solution from the internet but it is still not working. Anyone could help me?

<HTML>\n<HEAD>\n<TITLE>PROJECT_NOT_PERMITTED</TITLE>\n</HEAD>\n<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n<H1>PROJECT_NOT_PERMITTED</H1>\n<H2>Error 401</H2>\n</BODY>\n</HTML>\n

here is my code

$key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$data = [
            "registration_ids" => $tokens,
            "notification"  => [
                "title"     => $data->title,
                "body"      => $data->des
            ]
        ];
        $dataString = json_encode($data);
        $headers = [
            'Authorization: key='.$key,
            'Content-Type: application/json',
        ];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
        $response = curl_exec($ch);
        return $response;
0 likes
1 reply
LaryAI's avatar
Level 58

The error message in the code snippet suggests that the server is returning a 401 Unauthorized error. This could be due to an incorrect API key or an issue with the server's authentication.

Here are a few things to check:

  1. Make sure that the API key is correct and matches the one in the Firebase console.
  2. Check that the server's IP address is whitelisted in the Firebase console.
  3. Verify that the server is using the correct HTTP headers and payload format for the Firebase Cloud Messaging (FCM) API.

Here's an updated version of the code that includes error handling and logging:

$key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$data = [
    "registration_ids" => $tokens,
    "notification"  => [
        "title"     => $data->title,
        "body"      => $data->des
    ]
];
$dataString = json_encode($data);
$headers = [
    'Authorization: key='.$key,
    'Content-Type: application/json',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
$response = curl_exec($ch);

if ($response === false) {
    $error = curl_error($ch);
    error_log("FCM request failed: $error");
    return false;
}

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode != 200) {
    error_log("FCM request returned HTTP code $httpCode");
    return false;
}

curl_close($ch);
return $response;

This code logs any errors or HTTP status codes that are returned by the FCM API.

Please or to participate in this conversation.