zandor300/fcmframework

PHP framework for Firebase push notifications interaction

1.1.1 2025-09-11 14:40 UTC

This package is auto-updated.

Last update: 2025-09-11 14:40:31 UTC


README

PHP framework for easy interaction with the Apple Push Notification Service.

Install

composer require zandor300/fcmframework

Usage

Obtain Firebase service account file

To obtain the Firebase service account file, follow these steps:

  1. Go to the Firebase Console.
  2. Select your project.
  3. Click on the gear icon next to "Project Overview" and select "Project settings".
  4. Navigate to the "Service accounts" tab.
  5. Click on "Generate new private key".
  6. Confirm by clicking "Generate key".

A JSON file containing your service account credentials will be downloaded. Save this file securely, as it will be used to authenticate your server with Firebase.

Creating FCM object.

use FCMFramework;

use FCMFramework\FCM;

$fcm = null;
try {
    $serviceAccountFilePath = "";
    $fcm = new FCM($serviceAccountFilePath);
} catch (FCMException $e) {
    echo "Error: " . $e->getMessage();
    // Handle exception
}

Creating a basic notification

$notification = new FCMNotification();

$notification->setTitle("Example Notification");
$notification->setBody("This is an example notification!");

Creating a token object in React Native

import messaging from "@react-native-firebase/messaging";
import { PermissionsAndroid } from "react-native";

// Request notification permission
try {
    await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
    );
    checkNotificationPermission();
} catch (err) {
    console.warn(err);
}

(async () => {
    await checkNotificationPermission();
    if (isNotificationPermissionGranted) {
        saveTokenToDatabase();
    }
    }
)();

// Check notification permission
const granted = await PermissionsAndroid.check(
    PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
);
setIsNotificationPermissionGranted(granted);

// Create token
const token = await messaging().getToken();

Sending the notification

global $fcm;
try {
    $fcm->sendFCMNotification($notification, $this->getFCMToken());
} catch(FCMDeviceTokenInactive $e) {
    $this->delete();
}