/php-fcm

A PHP library for sending Firebase Cloud Messages and managing user topic subscriptions, device groups and devices.

Primary LanguagePHPMIT LicenseMIT

Information

This is a Fork (EdwinHoksberg/php-fcm) that includes FCMController.

You can use the function sendNotification() to send a simple notification.

PHP-FCM Build Status Coverage Status Documentation

A PHP library for sending Firebase Cloud Messages and managing user topic subscriptions, device groups and devices.

Installation

Installation with composer:

composer require edwinhoksberg/php-fcm

Installation with composer: Add to composer.json file:

  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/turboveja/php-fcm"
    }
  ]

And:

  "require": {
    "edwinhoksberg/php-fcm": "dev-master",
  }

Quickstart

You can use the original code to send custom notifications, or the fork method to send simply notifications with the parameters

Fork function:

$apiKey = 'Firebase key code';

$title = 'Notification title';

$body = 'Notification body'; 

$data = [
  'extra_data' => '',
  'example_id' => '',
];

$topics = [
  'topic1', 'topic2' //optional
];

$recipients = [
  'recipient1', 'recipient2' //optional
];

//Send this notification
$fcm = (new FCMController)->sendNotification($apiKey, $title, $body, $data, $topics, $recipients);

Original implementation:

// Instantiate the client with the project api_token and sender_id.
$client = new \Fcm\FcmClient($apiToken, $senderId);

// Instantiate the push notification request object.
$notification = new \Fcm\Push\Notification();

// Enhance the notification object with our custom options.
$notification
    ->addRecipient($deviceId)
    ->setTitle('Hello from php-fcm!')
    ->setBody('Notification body')
    ->addData('key', 'value');

// Send the notification to the Firebase servers for further handling.
$client->send($notification);

Full documentation

Read the documentation here or look in the docs directory.

Tests

Run the unit tests with PHPUnit:

vendor/bin/phpunit -c phpunit.dist.xml

License

MIT