iansltx/gcm-client

A client for Firebase Cloud Messaging for Android's message API (via Legacy endpoints)

1.0.4 2021-08-23 18:40 UTC

This package is auto-updated.

Last update: 2024-10-30 01:38:40 UTC


README

Author Latest Version Software License

gcm-client is a PHP library for interacting with Google Cloud Messaging for Android's HTTP API. It can be used to send messages to both Android Registration IDs and Notification Keys (for user-based multi-device messaging). It can also create and manipulate Notification Keys.

This library should conform to PSRs 1, 2, and 4, and requires PHP 7.2 or newer.

Install

Via Composer

$ composer require iansltx/gcm-client

If you don't want Composer, you may download the source zipball directly from GitHub and load it using a PSR-4 compliant autoloader. If you don't have such an autoloader, require autoload.php to get one that works for this library.

Usage

<?php

require "vendor/autoload.php";

$client = new iansltx\GCMClient\Client(YOUR_GCM_API_KEY);
$message = new iansltx\GCMClient\Message(['title' => 'Notification', 'message' => 'Hello World!']);

// send directly to one or more Registration IDs
$regIdResult = $client->sendToRegIds($message, ['regId1', 'regId2']);
$singleRegIdResult = $client->sendToRegIds($message, 'regId3');

// create a Notification Key for user-based messaging and send to that
$nkClient = $client->withProjectId('myProjectId'); // a project ID is required for notification key manipulation
$key = $nkClient->createNotificationKey('myUniqueKeyName', ['regId1', 'regId2']);
$nkClient->addToNotificationKey($key, ['regId3'], 'myUniqueKeyName'); // returns the notification key
$nkClient->removeFromNotificationKey($key, ['regId1'], 'myUniqueKeyName'); // returns the notification key
$nKeyResult = $client->sendToNotificationKey($key, $message); // could use $nkClient to send as well

Take a look at the docblocks of Client and Message for more information.

Google changed the preferred location of the notification key field for their message-sending endpoint awhile back. This library has been updated to the new, non-deprecated, location. Additionally, it looks like they're requiring names when updating notification keys now; this used to be optional, hence its placement as the last parameter in the calls above.

As of v1.0, this library uses Google's non-deprecated FCM legacy endpoints (their words, not mind), rather than the old GCM endpoints that'll go offline on or after May 29th, 2019. So, despite the name, this library can be used with Firebase Cloud Messaging after May 2019.

Testing

$ composer test

PHPUnit is currently used for testing.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email iansltx@gmail.com instead of using the issue tracker.

Credits

License

This library is BSD 2-clause licensed. Please see License File for more information.