Giter Club home page Giter Club logo

fcm's Introduction

Firebase Cloud Messaging PHP Client

Just another client to send push notifications via Firebase Cloud Messaging (FCM). The implementation is inspired by Paragraph1/php-fcm and its fork guigpm/php-fcm, but heavily reworked in favor of simpler code and better implementation.

Latest Version on Packagist Build Status Changelog PHP version Software License

Installation

This library relies on PSR-18 which defines how HTTP message should be sent and received. You can use any library to send HTTP messages that implements psr/http-client-implementation. Read more about PSR-18 in this blog.

To install this library with Guzzle you may run the following command:

composer require erickskrauch/fcm guzzlehttp/guzzle

Or using the curl client (you'll need to provide a PSR7 implementation such as nyholm/psr7 if not using Guzzle):

composer require erickskrauch/fcm php-http/curl-client nyholm/psr7

Usage

The component's constructor allows you to explicitly pass all dependencies. But you can also pass only the mandatory API token, and all other dependencies will be found automatically. The example below relies on auto discovery.

use ErickSkrauch\Fcm\Client;
use ErickSkrauch\Fcm\Message\Message;
use ErickSkrauch\Fcm\Message\Notification;
use ErickSkrauch\Fcm\Recipient\Device;

$client = new Client('YOUR SERVER KEY');

$notification = new Notification(, 'testing body');
$notification->setTitle('Wow, something happened...');
$notification->setBody('It just works!');

$message = new Message();
$message->setNotification($notification);
$message->setCollapseKey('collapse.key');

$recipient = new Device('your-device-token');

$result = $client->send($message, $recipient);

The library provides several implementations for the Recipient interface:

  • Device is used to send notifications to a single device.
  • Topic is used to send notifications to a single topic.
  • MultipleDevices is used to send notifications to multiple devices. The FCM documentation doesn't recommend using this method to send to 1 device.
  • MultipleTopics is used to send notifications to multiple topics combined by the operator ||.

At the moment, the library does not have a builder for complex conditions. But you can always create your own implementation of the Recipient interface:

use ErickSkrauch\Fcm\Recipient\Recipient;

class MyComplexCondition implements Recipient {

    public function getConditionParam(): string{
        return Recipient::PARAM_CONDITION;
    }
    
    public function getConditionValue(): string {
        return "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)";
    }

}

Contribute

This library in an Open Source under the MIT license. It is, thus, maintained by collaborators and contributors.

Feel free to contribute in any way. As an example you may:

  • Trying out the master code.
  • Create issues if you find problems.
  • Reply to other people's issues.
  • Review PRs.

Ensuring code quality

The project has several tools for quality control. All checks are performed in CI, but if you want to perform checks locally, here are the necessary commands:

vendor/bin/php-cs-fixer fix -v
vendor/bin/phpstan analyse
vendor/bin/phpunit
vendor/bin/infection # Try "env XDEBUG_MODE=coverage vendor/bin/infection" in case of errors

fcm's People

Contributors

erickskrauch avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.