Skip to content

riftwebdev/firebase-notifications

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Firebase Notifications πŸ“©

Latest Version Total Downloads License Website

A Laravel package that lets you use the new FCM Http V1 API and send push notifications with ease.

Summary

  1. Install
  2. Usage

Install

If your Firebase project is already configured, skip this part and go to the Usage section section.

Firebase

TBD

Laravel

  1. Install via Composer
composer require riftwebdev/firebase-notifications
  1. Publish the config file
php artisan vendor:publish --tag=firebase-notifications --ansi --force
  1. Place your Firebase JSON on your project.

    image
  2. Retrieve the .env variables from your Firebase Console -> Project Settings -> General and watch firebaseConfig.

    5
  3. 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>"

Usage

Topics

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.

Subscribe

Single Token to Single Topic
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;

$token = "example token";
$topic = "exampleTopic";

FcmTokenTopic::subscribeToTopic($token, $topic);
Multiple Tokens to Single Topic
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;

$tokens = ["example token 1", "example token 2"];

$topic = "exampleTopic";

FcmTokenTopic::subscribeToTopic($tokens, $topic);
Single Token to Multiple Topics
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;

$token = "example token";
$topics = ["exampleTopic1", "exampleTopic2"];

FcmTokenTopic::subscribeToTopics($token, $topics);
Multiple Tokens to Multiple Topics
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;

$tokens = ["example token 1", "example token 2"];
$topics = ["exampleTopic1", "exampleTopic2"];

FcmTokenTopic::subscribeToTopics($tokens, $topics);

Unsubscribe

Single Token to Single Topic
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;

$token = "example token";
$topic = "exampleTopic";

FcmTokenTopic::unsubscribeToTopic($token, $topic);
Multiple Tokens to Single Topic
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;

$tokens = ["example token 1", "example token 2"];

$topic = "exampleTopic";

FcmTokenTopic::unsubscribeToTopic($tokens, $topic);
Single Token to Multiple Topics
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;

$token = "example token";
$topics = ["exampleTopic1", "exampleTopic2"];

FcmTokenTopic::unsubscribeToTopics($token, $topics);
Multiple Tokens to Multiple Topics
use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;

$tokens = ["example token 1", "example token 2"];
$topics = ["exampleTopic1", "exampleTopic2"];

FcmTokenTopic::unsubscribeToTopics($tokens, $topics);

List Token Subscribed Topics

use Riftweb\FirebaseNotifications\Facades\FcmTokenTopic;

$token = "example token 1";

FcmTokenTopic::getTopics($token);

Notification

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();

Notification Mapping

 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
                ]
            ]
        ];

Contributing 🀝

Contributions are welcome! Please follow:

  1. Fork the repository
  2. Create your feature branch
  3. Commit changes
  4. Push to the branch
  5. Open a PR

License πŸ“„

MIT License - See LICENSE for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages