Provides Firebase integration for Symfony Notifier.
FIREBASE_DSN=firebase://<CLIENT_EMAIL>@default?project_id=<PROJECT_ID>&private_key_id=<PRIVATE_KEY_ID>&private_key=<PRIVATE_KEY>
All 4 parameters are required. All 4 parameters are located inside your firebase json private key.
IMPORTANT: Make sure that PRIVATE_KEY is safely url encoded. Get more information in Notifier documentation or use the script below.
Steps for getting your private key:
- Log into the firebase console.
- Click on your project
- Click on the gear icon next to "Project Overview" and click on "Project settings"
- Click on "Service accounts" tab
- Click on "Generate new private key" button at the bottom
- A JSON file with your private key will be downloaded
The downloaded private key is a JSON file which should contain the following keys:
typeproject_idprivate_key_idprivate_keyclient_emailclient_idauth_uritoken_uriauth_provider_x509_cert_urlclient_x509_cert_urluniverse_domain
You can then use the following script to convert your JSON private key to DSN format:
<?php
if (!isset($argv[1])) {
echo 'Usage: php ' . $argv[0] . ' path_to_your_key.json'.PHP_EOL;
exit(1);
}
$file = file_get_contents($argv[1]);
if (false === $file) {
echo 'Could not open file at path: '.$argv[1].PHP_EOL.PHP_EOL;
exit(1);
}
$key = json_decode($file, true);
if (false === $key) {
echo 'Unable to load JSON content of the file at path: '.$argv[1].PHP_EOL;
exit(1);
}
foreach (['client_email', 'project_id', 'private_key_id', 'private_key'] as $param) {
if (!isset($key[$param])) {
echo 'Missing param '.$param.' inside the JSON key.'.PHP_EOL;
exit(1);
}
}
echo sprintf(
'FIREBASE_DSN=firebase://%s@default?project_id=%s&private_key_id=%s&private_key=%s',
$key['client_email'],
$key['project_id'],
$key['private_key_id'],
urlencode($key['private_key']),
).PHP_EOL;The script then can be used as follows:
php script_name.php path/to/your/firebase/key.jsonWith a Firebase message, you can use the FirebaseOptions class to add
platform specific message options.
Specifying Firebase options for a message:
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseOptions;
$chatMessage = new ChatMessage('Hello, you should contribute to Symfony.');
// Specify options for Firebase
$firebaseOptions = (new FirebaseOptions('/topics/news'))
->title('New message!')
->body('This will overwrite the subject from ChatMessage object.')
->image('https://path-to-your-image.png')
->data([
'key1' => 'value1',
'key2' => 'value2',
])
// ...
;
// Add the custom options to the chat message and send the message
$chatMessage->options($androidOptions);
$chatter->send($chatMessage);Firebase offers 3 different types of targets to send the message to:
- token
- topic
- condition
How to specify different targets for firebase messages:
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseOptions;
use Symfony\Component\Notifier\Bridge\Firebase\TargetType;
// Use a topic as a target
$topicOptions = new FirebaseOptions('/topics/news', targetType: TargetType::Topic);
// Use a token as a target
$tokenOptions = new FirebaseOptions('dU5e3nFJf9bE:APA91bH3Kd/exampleToken', targetType: TargetType::Token);
// Use a condition as a target
$conditionOptions = new FirebaseOptions('\'news\' in topics', targetType: TargetType::Condition);Firebase also allows specifying platform specific options. They can be specified as follows:
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseOptions;
use Symfony\Component\Notifier\Bridge\Firebase\TargetType;
$detailedOptions = (new FirebaseOptions('dU5e3nFJf9bE:APA91bH3Kd/exampleToken', targetType: TargetType::Token))
// Basic options
->title('New message!')
->data([
'key1' => 'value1',
'key2' => 'value2',
])
// Android specific options
->android([
'notification' => [
'color' => '#4538D5',
],
])
// WebPush specific options
->webpush([
'notification' => [
'icon' => 'https://path-to-an-icon.png',
],
])
// APNS specific options
->apns([
'payload' => [
'aps' => [
'sound' => 'default',
],
],
])
// ...
;For all available options please refer to the firebase documentation.
This package is looking for a backer.
Help Symfony by sponsoring its development!