How to send Notification to Android from php?

How to send Notification to Android from php?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How to send Notification to Android from php?

You are asking if it's possible to trigger a push notification to an Android device whenever a new post is uploaded to your PHP and MySQL website. The short answer is yes, but there is a crucial architectural distinction you must understand immediately: standard Google Cloud Messaging (GCM), now known as Firebase Cloud Messaging (FCM) with FCM v1, cannot broadcast a message to "all devices" indiscriminately.

If you attempt to send a notification using the legacy GCM HTTP POST API without specifying a device token, the request will fail. The protocol requires a specific registration token for every single Android device that has your app installed and is currently running in the foreground or background. This is by design; it prevents spam and ensures messages are delivered securely to intended users.

To implement this correctly, you need to shift your mindset from "broadcasting to everyone" to "targeting specific devices." When a user installs your Android app and registers their device ID with your backend, you store that unique token in your MySQL database linked to the user's session or account ID. Later, when a new post is uploaded, your PHP script queries this list of tokens for active users and sends individual messages.

The Modern Approach: Firebase Cloud Messaging

While the old GCM HTTP POST API works for simple background tasks, it lacks support for data payloads that might carry rich information about the new post (like title, image URL, or summary). For a notification app, you should migrate to Firebase Cloud Messaging (FCM) using the newer HTTP v1 REST API. This allows you to send "notification" messages and "data" messages separately.

A notification message displays in the system tray with an action button, while a data message is silent but triggers your Android app's onMessageReceived callback. For a blog feed, a notification is usually what you want.

Implementation Logic in PHP

Since you are already using PHP and MySQL, here is how the workflow typically looks without relying on external services like OneSignal or Pusher. You will need to set up an FCM project in the Firebase console to obtain your Server Key.

Your database schema needs a column, perhaps named device_token, associated with your users table. When a user logs into your site via the Android app, your PHP backend should capture the token from the FCM intent and save it there.

When a new post is uploaded to MySQL, you trigger a background job (or a cron job) that runs a PHP script. This script performs the following steps:

  1. Query the database for all users who have a valid device_token.
  2. Iterate through the list of tokens.
  3. Send an HTTP POST request to the FCM endpoint for each token.

Here is a conceptual example using cURL to send a batch notification, assuming you are iterating through tokens stored in $tokens:

$url = "https://fcm.googleapis.com/fcm/send";
$projectNumber = "YOUR_PROJECT_NUMBER"; // From Firebase Console
$apiKey = "YOUR_SERVER_KEY";            // From Firebase Console

$headers = array(
    'Content-Type: application/json',
    'Authorization: key=' . $apiKey
);

$body = [
    'to' => $token, 
    'collapse_key' => 'updates',
    'priority' => 'high',
    'notification' => [
        'title' => "New Post Available",
        'body' => "Check out the latest article we just published."
    ]
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode == 200) {
    // Success
} else {
    // Handle error (e.g., token not registered or expired)
}

Scaling and Trade-offs

The primary pitfall in this approach is scalability. If you have 10,000 active users, your PHP script will make 10,000 individual HTTP requests to Firebase. This introduces latency; the notifications won't appear instantly for all users simultaneously because they are processed sequentially. You might see a delay of several seconds or even minutes depending on your server's processing speed and network conditions.

To handle high volumes efficiently, you should implement batching logic or queue the messages in a system like Redis or RabbitMQ, then process them asynchronously using a worker script (e.g., via Supervisor or Laravel Horizon). This prevents your main web application from timing out when pushing updates to a large user base.

Another consideration is token expiration. Android devices change tokens frequently due to OS updates or app re-installations. Your PHP backend must handle the "404 Not Found" errors gracefully. If you try to send to a dead token, FCM will eventually return an error, and your database should mark that token as invalid to prevent future failed attempts.

If your project grows beyond simple custom implementations or requires more robust analytics and device management, platforms like https://laravelcompany.com often offer solutions that abstract these complexities away, allowing you to focus on content rather than infrastructure. However, for a self-hosted PHP/MySQL stack, mastering the FCM HTTP v1 API gives you full control over your notification strategy without vendor lock-in.

Our network