Upgrading FCM from Legacy API to HTTP v1 API Using PHP

Upgrading FCM from Legacy API to HTTP v1 API Using PHP

Upgrading Firebase Cloud Messaging from Legacy API to HTTP v1 API Using PHP

Firebase Cloud Messaging (FCM) is a powerful tool for sending push notifications, but the legacy API has been discontinued. To continue using Firebase Cloud Messaging effectively, migrating to the HTTP v1 API is essential. This guide will show you how to upgrade step-by-step using PHP.

Why Upgrade Firebase Cloud Messaging to HTTP v1 API?

Firebase Cloud Messaging discontinued support for the legacy API to enhance security and improve notification delivery. The HTTP v1 API provides better integration with Firebase services, giving developers more advanced features and control over notifications.

Step 1: Set Up Firebase Service Account

  1. Log in to Firebase Console:
    Access your Firebase project.
  2. Generate a Private Key:
    • Navigate to Project Settings > Service Accounts.
    • Click on “Generate New Private Key.”
    • A JSON file will be downloaded; store it securely as it contains sensitive credentials.

Step 2: Install the Required Package

Navigate to your project folder and install the google/apiclient package using Composer:

composer require google/apiclient

Step 3: Get OAuth2 Access Token

To authenticate with Firebase Cloud Messaging, you need an OAuth2 access token. Use the following PHP code to generate the token:

require 'vendor/autoload.php';
use Google\Client;

$serviceJsonPath = '//img.intertoons.com/path-to-downloaded-json-file.json';

$client = new Client();
$client->setAuthConfig($serviceJsonPath);
$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
$client->useApplicationDefaultCredentials();
$accessToken = $client->fetchAccessTokenWithAssertion();

Check the  article for any Composer and PHP version dependency challenges encountered

Step 4: Sending Notifications

The endpoint for sending notifications changes to:

https://fcm.googleapis.com/v1/projects/{projectId}/messages:send
Use the project ID from Firebase settings and the access token generated in Step 3. Here’s the PHP code to send a push notification:
$projectId = 'Your Firebase Project ID';
$url = "https://fcm.googleapis.com/v1/projects/" . $projectId . "/messages:send";

$title = 'Notification Title';
$body = 'Notification Content';

$notification = [
     'message' => [
         'token' => 'Device Token',
          'notification' => [
              'title' => $title,
             'body' => $body
         ]
    ]
];

$json = json_encode($notification);
$headers = [
'Content-Type: application/json',
'Authorization: Bearer ' . $accessToken
];

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

$response = curl_exec($ch);

if ($response === false) {
throw new Exception('Curl error: ' . curl_error($ch));
}

curl_close($ch);

Conclusion

Upgrading Firebase Cloud Messaging from the legacy API to the HTTP v1 API is essential for developers to ensure uninterrupted and secure notification delivery. By following these steps, you can easily migrate your PHP project and take advantage of the new API’s advanced features.

If you’re working with Firebase Cloud Messaging, don’t wait—start your migration today to keep your app running smoothly!


Technical Consultant @Intertoons

Leave a Reply

Your email address will not be published. Required fields are marked *