2017 © Pedro PelĂĄez
 

library php-firebase-cloud-messaging

PHP API for Firebase Cloud Messaging from Google

image

granam/php-firebase-cloud-messaging

PHP API for Firebase Cloud Messaging from Google

  • Thursday, April 19, 2018
  • by jaroslavtyc
  • Repository
  • 1 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 72 Forks
  • 0 Open issues
  • 13 Versions
  • 14 % Grown

The README.md

PHP Firebase Cloud Messaging

PHP API for Firebase Cloud Messaging from Google., (*1)

This library supports Messages/Notifications via HTTP only., (*2)

Requires PHP 7.1, if you have to rely on lower version, use original PHP FCM library or some clone of it., (*3)

Send message to Device or Topic

<?php
use granam\FirebaseCloudMessaging\FcmClient;
use granam\FirebaseCloudMessaging\FcmMessage;
use granam\FirebaseCloudMessaging\Target\FcmTopicTarget;
use granam\FirebaseCloudMessaging\Target\FcmDeviceTarget;
use granam\FirebaseCloudMessaging\AndroidFcmNotification;

$client = new FcmClient(new \GuzzleHttp\Client(), '_YOUR_SERVER_KEY_');
$message = new FcmMessage(new FcmDeviceTarget('_YOUR_DEVICE_TOKEN_'));
$message->setPriority('high')
    ->setNotification(new AndroidFcmNotification('A message from Foo!', 'Hi Bar, how are you?'))
    ->setData(['photo' => 'https://example.com/photos/Foo.png']);
// you can NOT combine multiple recipient types as topic and device in a single message
$messageForTopic = new FcmMessage(new FcmTopicTarget('_YOUR_TOPIC_'));
$messageForTopic
    ->setNotification(new AndroidFcmNotification('Foo is looking for you!', 'Here you are!'))
    ->setData(['map' => 'https://example.com/maps/foo']);

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

Send silent push notification to a device

App will have to process the notification by its own as system will not show it., (*4)

<?php
use granam\FirebaseCloudMessaging\FcmClient;
use granam\FirebaseCloudMessaging\FcmMessage;
use granam\FirebaseCloudMessaging\Target\FcmDeviceTarget;

$client = new FcmClient(new \GuzzleHttp\Client(), '_YOUR_SERVER_KEY_');
$message = new FcmMessage(new FcmDeviceTarget('_YOUR_DEVICE_TOKEN_'));
$message->setData([
        'title' => 'A message from Foo!',
        'message' => 'Hi Bar, how are you?',
        'photo' => 'https://example.com/photos/Foo.png'
    ]);
$response = $client->send($message);
var_dump($response);

Send message to multiple Devices

<?php
use granam\FirebaseCloudMessaging\FcmMessage;
use granam\FirebaseCloudMessaging\Target\FcmDeviceTarget;
use granam\FirebaseCloudMessaging\IosFcmNotification;

$message = new FcmMessage(new FcmDeviceTarget('_YOUR_DEVICE_TOKEN_'));
$message->addTarget(new FcmDeviceTarget('_YOUR_DEVICE_TOKEN_2_'))
    ->addTarget(new FcmDeviceTarget('_YOUR_DEVICE_TOKEN_3_'))
    ->setPriority('high')
    ->setNotification(new IosFcmNotification('You are wanted', 'We got some issue here, where are you? We need you.'))
    ->setData(['attachment' => 'data:image/gif;base64,FooBar==']);
// ...

Send message to multiple Topics

See Firebase documentation for sending to combinations of multiple topics., (*5)

<?php
use granam\FirebaseCloudMessaging\FcmMessage;
use granam\FirebaseCloudMessaging\JsFcmNotification;
use granam\FirebaseCloudMessaging\Target\FcmTopicTarget;

$message = new FcmMessage(new FcmTopicTarget('_YOUR_TOPIC_'));
$message->addTarget(new FcmTopicTarget('_YOUR_TOPIC_2_'))
    ->addTarget(new FcmTopicTarget('_YOUR_TOPIC_3_'))
    ->setPriority('high')
    ->setNotification(new JsFcmNotification('some title', 'some body'))
    ->setData(['key' => 'value'])
    // will send to devices subscribed to topic 1 AND topic 2 or 3
    ->setCondition('%s && (%s || %s)');

Subscribe user to the topic

<?php
use granam\FirebaseCloudMessaging\FcmClient;

$client = new FcmClient(new \GuzzleHttp\Client(), '_YOUR_SERVER_KEY_');
$response = $client->subscribeToTopic('_SOME_TOPIC_', ['_FIRST_DEVICE_TOKEN_', '_SECOND_DEVICE_TOKEN_']);
var_dump($response->getStatusCode());
var_dump($response->getBody()->getContents());

Remove user subscription from the topic

<?php
use granam\FirebaseCloudMessaging\FcmClient;

$client = new FcmClient(new \GuzzleHttp\Client(), '_YOUR_SERVER_KEY_');

$response = $client->unsubscribeFromTopic('_SOME_TOPIC_', ['_FIRST_DEVICE_TOKEN_', '_SECOND_DEVICE_TOKEN_']);
var_dump($response->getStatusCode());
var_dump($response->getBody()->getContents());

Install

composer require granam/php-firebase-cloud-messaging

The Versions

19/04 2018

2.0.x-dev

2.0.9999999.9999999-dev https://github.com/jaroslavtyc/php-firebase-cloud-messaging

PHP API for Firebase Cloud Messaging from Google

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Sngrl

php cloud google ios android firebase chrome notifications fcm

19/04 2018

2.2.0

2.2.0.0 https://github.com/jaroslavtyc/php-firebase-cloud-messaging

PHP API for Firebase Cloud Messaging from Google

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Sngrl

php cloud google ios android firebase chrome notifications fcm

19/04 2018

dev-master

9999999-dev https://github.com/jaroslavtyc/php-firebase-cloud-messaging

PHP API for Firebase Cloud Messaging from Google

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Sngrl

php cloud google ios android firebase chrome notifications fcm

19/04 2018

2.1.0

2.1.0.0 https://github.com/jaroslavtyc/php-firebase-cloud-messaging

PHP API for Firebase Cloud Messaging from Google

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Sngrl

php cloud google ios android firebase chrome notifications fcm

18/04 2018

2.0.0

2.0.0.0 https://github.com/jaroslavtyc/php-firebase-cloud-messaging

PHP API for Firebase Cloud Messaging from Google

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Sngrl

php cloud google ios android firebase chrome notifications fcm

18/04 2018

1.2.x-dev

1.2.9999999.9999999-dev https://github.com/jaroslavtyc/php-firebase-cloud-messaging

PHP API for Firebase Cloud Messaging from Google

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Sngrl

php cloud google ios android firebase chrome notifications fcm

18/04 2018

1.2.1

1.2.1.0 https://github.com/jaroslavtyc/php-firebase-cloud-messaging

PHP API for Firebase Cloud Messaging from Google

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Sngrl

php cloud google ios android firebase chrome notifications fcm

27/03 2018

1.2.0

1.2.0.0 https://github.com/jaroslavtyc/php-firebase-cloud-messaging

PHP API for Firebase Cloud Messaging from Google

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Sngrl

php cloud google ios android firebase chrome notifications fcm

26/03 2018

1.0.x-dev

1.0.9999999.9999999-dev https://github.com/sngrl/php-firebase-cloud-messaging

PHP API for Firebase Cloud Messaging from Google

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Sngrl

php cloud google ios android firebase chrome notifications fcm

26/03 2018

1.0.1

1.0.1.0 https://github.com/sngrl/php-firebase-cloud-messaging

PHP API for Firebase Cloud Messaging from Google

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Sngrl

php cloud google ios android firebase chrome notifications fcm

26/03 2018

1.1.x-dev

1.1.9999999.9999999-dev https://github.com/sngrl/php-firebase-cloud-messaging

PHP API for Firebase Cloud Messaging from Google

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Sngrl

php cloud google ios android firebase chrome notifications fcm

26/03 2018

1.1.0

1.1.0.0 https://github.com/sngrl/php-firebase-cloud-messaging

PHP API for Firebase Cloud Messaging from Google

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Sngrl

php cloud google ios android firebase chrome notifications fcm

08/03 2018

1.0.0

1.0.0.0 https://github.com/sngrl/php-firebase-cloud-messaging

PHP API for Firebase Cloud Messaging from Google

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Sngrl

php cloud google ios android firebase chrome notifications fcm