PHP code example of granam / php-firebase-cloud-messaging
1. Go to this page and download the library: Download granam/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/ */
granam / php-firebase-cloud-messaging example snippets
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);
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);
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==']);
// ...
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)');
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());
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());
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.