Giter VIP home page Giter VIP logo

fcm's Introduction

Laravel FCM (Firebase Cloud Messaging) Notification Channel

Latest Version on Packagist Software License StyleCI Quality Score Total Downloads

This package makes it easy to send notifications using Firebase Cloud Messaging (FCM) with Laravel.

Contents

Installation

Install this package with Composer:

composer require laravel-notification-channels/fcm

Setting up the FCM service

This package now uses the laravel-firebase library to authenticate and make the API calls to Firebase. Follow the configuration steps specified in their readme before using this.

After following their configuration steps, make sure that you've specified your FIREBASE_CREDENTIALS in your .env file.

Usage

After setting up your Firebase credentials, you can now send notifications via FCM by a Notification class and sending it via the FcmChannel::class. Here is an example:

use Illuminate\Notifications\Notification;
use NotificationChannels\Fcm\FcmChannel;
use NotificationChannels\Fcm\FcmMessage;
use NotificationChannels\Fcm\Resources\AndroidConfig;
use NotificationChannels\Fcm\Resources\AndroidFcmOptions;
use NotificationChannels\Fcm\Resources\AndroidNotification;
use NotificationChannels\Fcm\Resources\ApnsConfig;
use NotificationChannels\Fcm\Resources\ApnsFcmOptions;

class AccountActivated extends Notification
{
    public function via($notifiable)
    {
        return [FcmChannel::class];
    }

    public function toFcm($notifiable)
    {
        return FcmMessage::create()
            ->setData(['data1' => 'value', 'data2' => 'value2'])
            ->setNotification(\NotificationChannels\Fcm\Resources\Notification::create()
                ->setTitle('Account Activated')
                ->setBody('Your account has been activated.')
                ->setImage('http://example.com/url-to-image-here.png'))
            ->setAndroid(
                AndroidConfig::create()
                    ->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
                    ->setNotification(AndroidNotification::create()->setColor('#0A0A0A'))
            )->setApns(
                ApnsConfig::create()
                    ->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios')));
    }

    // optional method when using kreait/laravel-firebase:^3.0, this method can be omitted, defaults to the default project
    public function fcmProject($notifiable, $message)
    {
        // $message is what is returned by `toFcm`
        return 'app'; // name of the firebase project to use
    }
}

You will have to set a routeNotificationForFcm() method in your notifiable model. For example:

class User extends Authenticatable
{
    use Notifiable;

    ....

    /**
     * Specifies the user's FCM token
     *
     * @return string|array
     */
    public function routeNotificationForFcm()
    {
        return $this->fcm_token;
    }
}

You can also return an array of tokens to send notifications via multicast to different user devices.

class User extends Authenticatable
{
    use Notifiable;

    ....

    /**
     * Specifies the user's FCM tokens
     *
     * @return string|array
     */
    public function routeNotificationForFcm()
    {
        return $this->getDeviceTokens();
    }
}

Once you have that in place, you can simply send a notification to the user by doing the following:

$user->notify(new AccountActivated);

Available Message methods

The FcmMessage class contains the following methods for defining the payload. All these methods correspond to the available payload defined in the FCM API documentation. Refer to this link to find all the available data you can set in your FCM notification.

setName(string $name)
setData(array $data)
setNotification(\NotificationChannels\Fcm\Resources\Notification $notification)
setAndroid(NotificationChannels\Fcm\Resources\AndroidConfig $androidConfig)
setApns(NotificationChannels\Fcm\Resources\ApnsConfig $apnsConfig)
setWebpush(NotificationChannels\Fcm\Resources\WebpushConfig $webpushConfig)
setFcmOptions(NotificationChannels\Fcm\Resources\FcmOptions $fcmOptions)
setTopic(string $topic)
setCondition(string $condition)

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

fcm's People

Contributors

chrisbjr avatar dwightwatson avatar atymic avatar marvelefe avatar ankurk91 avatar awkwardusername avatar mathieumaas avatar timleland avatar dododedodonl avatar ahmedsayedabdelsalam avatar hklsk avatar danieljaniga avatar danielebuso avatar erik-ropez avatar maisonsport-elukos avatar gregorip02 avatar professor93 avatar reinink avatar jozefbalun avatar koenhoeijmakers avatar cmanish049 avatar marcroberts avatar maximal avatar immeyti avatar moaalaa avatar mgsmus avatar ramytamer avatar ryzr avatar saundersb avatar laravel-shift avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.