kedniko/firebase-cloud-messaging-http-v1-php

Firebase cloud messaging http v1 php

v0.0.2 2023-12-07 09:24 UTC

This package is auto-updated.

Last update: 2024-05-07 10:18:42 UTC


README

Firebase Cloud Messaging Http v1

Firebase Cloud Messaging Http v1 for PHP

Installation

composer require kedniko/firebase-cloud-messaging-http-v1-php

Send a message

<?php

$authKeyContent = json_decode(file_get_contents(__DIR__ . '/appname-30xfgre76.json'), true);
$projectID = 'my-project-1';
$body = [
    'message' => [
        'token' => '<token:string>',
        'notification' => [
            'title' => 'Breaking News',
            'body' => 'New news story available.',
        ],
        'data' => [
            'story_id' => 'story_12345',
        ],
    ],
];

$bearerToken = FCM::getBearerToken($authKeyContent);

FCM::send($bearerToken, $projectID, $body);

Send multiple messages

<?php

$authKeyContent = json_decode(file_get_contents(__DIR__ . '/appname-30xfgre76.json'), true);
$projectID = 'my-project-1';
$tokens = [
    '<token1:string>',
    '<token2:string>',
    '<token3:string>',
];

$bearerToken = FCM::getBearerToken($authKeyContent);

foreach ($tokens as $token) {
    $body = [
        'message' => [
            'token' => $token,
            'notification' => [
                'title' => 'Breaking News',
                'body' => 'New news story available.',
            ],
            'data' => [
                'story_id' => 'story_12345',
            ],
        ],
    ];

    FCM::send($bearerToken, $projectID, $body);
}

Subscribe to topic

<?php

$authKeyContent = json_decode(file_get_contents(__DIR__ . '/appname-30xfgre76.json'), true);
$tokens = [
    '<token1:string>',
    '<token2:string>',
    '<token3:string>',
];

$bearerToken = FCM::getBearerToken($authKeyContent);
FCM::subscribeToTopic($bearerToken, 'my-topic-1', $tokens);

Unsubscribe from topic

<?php

$authKeyContent = json_decode(file_get_contents(__DIR__ . '/appname-30xfgre76.json'), true);
$tokens = [
    '<token1:string>',
    '<token2:string>',
    '<token3:string>',
];

$bearerToken = FCM::getBearerToken($authKeyContent);
FCM::unsubscribeFromTopic($bearerToken, 'my-topic-1', $tokens);

Credits: https://github.com/lkaybob/php-fcm-v1