PHP code example of redjanym / php-firebase-cloud-messaging

1. Go to this page and download the library: Download redjanym/php-firebase-cloud-messaging library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

redjanym / php-firebase-cloud-messaging example snippets


use RedjanYm\FCM\Client;
use RedjanYm\FCM\Notification;
use RedjanYm\FCM\Recipient\Device;

$serviceAccountPath = '/path/to/service-account.json';

$client = new Client($serviceAccountPath);
$recipient = new Device('your-device-token');
$notification = new Notification($recipient, 'Title', 'Body', ['key' => 'value']);

$response = $client->send($notification);

use RedjanYm\FCM\Client;
use RedjanYm\FCM\Notification;
use RedjanYm\FCM\Recipient\Topic;

$serviceAccountPath = '/path/to/service-account.json';

$client = new Client($serviceAccountPath);
$recipient = new Topic('news');
$notification = new Notification($recipient, 'Title', 'Body', ['key' => 'value']);

$response = $client->send($notification);

$notification = new Notification($recipient, 'Title', 'Body');

// Android
$notification->androidPriority = 'high';
$notification->androidChannelId = 'my_channel';
$notification->icon = 'ic_notification';
$notification->color = '#FF0000';

// APNs (iOS)
$notification->apnsPriority = '10';
$notification->badge = 5;
$notification->sound = 'default';
$notification->contentAvailable = true;

// General
$notification->image = 'https://example.com/image.png';
$notification->ttl = '3600s';
$notification->clickAction = 'OPEN_ACTIVITY';
$notification->analyticsLabel = 'campaign_123';

// Additional platform-specific settings via extra arrays
$notification->extraNotificationSettings = ['tag' => 'my-tag'];
$notification->extraAPNSHeadersSettings = ['apns-collapse-id' => 'campaign'];
$notification->webPushHeadersSettings = ['Urgency' => 'high'];
json
""redjanym/php-firebase-cloud-messaging": "2.*"
}
bash
vendor/bin/phpunit tests/NotificationTest.php