A Laravel package that lets you use the new FCM Http V1 API and send push notifications with ease.
If your Firebase project is already configured, skip this part and go to the Usage section section.
TBD
- Install via Composer
composer require riftwebdev/firebase-notifications
- Publish the config file
php artisan vendor:publish --tag=firebase-notifications --ansi --force
-
Place your Firebase JSON on your project.
-
Retrieve the .env variables from your Firebase Console -> Project Settings -> General and watch firebaseConfig.
-
Assign values to the .env variables
FCM_API_KEY="<Firebase apiKey>"
FCM_AUTH_DOMAIN="<Firebase authDomain>"
FCM_PROJECT_ID="<Firebase projectId>"
FCM_STORAGE_BUCKET="<Firebase storageBucket>"
FCM_MESSAGING_SENDER_ID="<Firebase messagingSenderId>"
FCM_APP_ID="<Firebase appId>"
FCM_JSON="<path of the JSON file from Firebase>"
Topics are used to make groups of device tokens. They will allow you to send notification directly to the topic where users are registered in.
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;
$token = "example token";
$topic = "exampleTopic";
FcmTokenTopic::subscribeToTopic($token, $topic);
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;
$tokens = ["example token 1", "example token 2"];
$topic = "exampleTopic";
FcmTokenTopic::subscribeToTopic($tokens, $topic);
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;
$token = "example token";
$topics = ["exampleTopic1", "exampleTopic2"];
FcmTokenTopic::subscribeToTopics($token, $topics);
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;
$tokens = ["example token 1", "example token 2"];
$topics = ["exampleTopic1", "exampleTopic2"];
FcmTokenTopic::subscribeToTopics($tokens, $topics);
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;
$token = "example token";
$topic = "exampleTopic";
FcmTokenTopic::unsubscribeToTopic($token, $topic);
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;
$tokens = ["example token 1", "example token 2"];
$topic = "exampleTopic";
FcmTokenTopic::unsubscribeToTopic($tokens, $topic);
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;
$token = "example token";
$topics = ["exampleTopic1", "exampleTopic2"];
FcmTokenTopic::unsubscribeToTopics($token, $topics);
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;
$tokens = ["example token 1", "example token 2"];
$topics = ["exampleTopic1", "exampleTopic2"];
FcmTokenTopic::unsubscribeToTopics($tokens, $topics);
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;
$token = "example token 1";
FcmTokenTopic::getTopics($token);
You can send notification to specific tokens or to topics, and both at same time.
Beware, if you set topics and tokens at same time, both will receive (whoever is subscribed to the topics + the extra tokens
use Riftweb\FirebaseNotifications\Facades\FcmNotification;
FcmNotification::create()
->setTitle('Example Notification')
->setBody('Lorem ipsum dolores')
->setImage(asset('images/example.png'))
->setCategory('develop') // Compatible with iOS
->setSound('blingbling') // Compatible with iOS
->setFlutterClickAction() // Only use this if you're using flutter - Will set click_action as FLUTTER_NOTIFICATION_CLICK, otherwise use setClickAction()
->setLink('https://example.com') // Used for webpush notifications
->setData([
'user_id' => 1,
'foo' => 'bar'
]) // Extra Data
->setTopics(['topic 1', 'topic 2']) // Set this notification to be sent for certain topics
->setTokens(['token 1', 'token 2']) // Specify which tokens will receive this notification
->send();
return [
"notification" => [
"title" => $this->title,
"body" => $this->body,
'image' => $image, // Used by Android/iOS
],
"data" => $this->data,
"android" => [
"notification" => [
"click_action" => $this->click_action
]
],
"apns" => [
"payload" => [
"aps" => [
"category" => $this->category,
"sound" => $this->sound
]
],
"fcm_options" => [
"image" => $image,
]
],
"webpush" => [
"notification" => [
"title" => $this->title,
"body" => $this->body,
"icon" => $image,
],
"fcm_options" => [
"link" => $this->link
]
]
];
Contributions are welcome! Please follow:
- Fork the repository
- Create your feature branch
- Commit changes
- Push to the branch
- Open a PR
MIT License - See LICENSE for details.