paragraph2/php-fcm

copy from paragraph1/php-fcm

v1.0.0 2024-01-17 05:31 UTC

This package is not auto-updated.

Last update: 2024-05-09 05:32:12 UTC


README

PHP application server implementation for Firebase Cloud Messaging.

Setup

The recommended way of installing is using Composer.

command line

composer require paragraph2/php-fcm

composer.json

"require": {
    "paragraph2/php-fcm": "*"
}

Send to Device

also see https://firebase.google.com/docs/cloud-messaging/downstream

use paragraph2\phpFCM\Client;
use paragraph2\phpFCM\Message;
use paragraph2\phpFCM\Recipient\Device;
use paragraph2\phpFCM\Notification;

require_once 'vendor/autoload.php';

$apiKey = 'YOUR SERVER KEY';
$client = new Client();
$client->setApiKey($apiKey);
$client->injectHttpClient(new \GuzzleHttp\Client());

$note = new Notification('test title', 'testing body');
$note->setIcon('notification_icon_resource_name')
    ->setColor('#ffffff')
    ->setBadge(1);

$message = new Message();
$message->addRecipient(new Device('your-device-token'));
$message->setNotification($note)
    ->setData(array('someId' => 111));

$response = $client->send($message);
var_dump($response->getStatusCode());

Send to topic

also see https://firebase.google.com/docs/cloud-messaging/topic-messaging

use paragraph2\phpFCM\Client;
use paragraph2\phpFCM\Message;
use paragraph2\phpFCM\Recipient\Topic;
use paragraph2\phpFCM\Notification;

require_once 'vendor/autoload.php';


$apiKey = 'YOUR SERVER KEY';
$client = new Client();
$client->setApiKey($apiKey);
$client->injectHttpClient(new \GuzzleHttp\Client());

$message = new Message();
$message->addRecipient(new Topic('your-topic'));
$message->setNotification(new Notification('test title', 'testing body'))
    ->setData(array('someId' => 111));

$response = $client->send($message);
var_dump($response->getStatusCode());