Skip to content

Laravel wraper around Firabase Cloud Messaging via HTTP v1 API PHP SDK

License

Notifications You must be signed in to change notification settings

pixwell-dev/laravel-fcm

Repository files navigation

Laravel FCM (Firebase Cloud Messaging) Notification Channel

Latest Version on Packagist Software License StyleCI SensioLabsInsight Quality Score Total Downloads

This package makes it easy to send notifications using Firebase Cloud Messaging (FCM) via new FCM HTTP v1 API with Laravel 5.3, 5.4, and 5.5.

Contents

Installation

Install this package with Composer:

composer require pixwell-dev/laravel-notification-channel-fcm

Register the ServiceProvider in your config/app.php (Skip this step if you are using Laravel 5.5):

NotificationChannels\Fcm\FcmServiceProvider::class,

Setting up the FCM service

You need to register for a server key from Firebase for your application. Start by creating a project here: https://console.firebase.google.com

Once you've registered and set up your project, add the firebase auth json file key to your configuration in config/broadcasting.php

'connections' => [
    ....
    'fcm' => [
        'json_file_path' => env('FIREBASE_CREDENTIALS'),
    ],
    ...
]

Usage

You can now send notifications via FCM by creating an FcmNotification and an FcmMessages:

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

    public function toFcm($notifiable)
    {
        // The FcmNotification holds the notification parameters
        $fcmNotification = FcmNotification::create()
            ->withTitle('Your account has been activated')
            ->withBody('Thank you for activating your account.');
            
        // The FcmMessage contains other options for the notification
        return FcmMessage::create()
            ->setNotification($fcmNotification)
            ->setData([])
            ->setApns($apnsConfig)
            ->setAndroid($androidConfig)
            ->setWebpush($webpushConfig);
    }
}

Available Message methods

The Message object always contains attributes (AndroidConfig | ApnsConfig | WebPushConfig) for differ between different operating systems (Android, iOS, and Chrome). In this perspective, a Message object is available for each platform which extends the FcmMessage object.

All the methods below are explained and defined in the Firebase Cloud Messaging documentation found here: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages This package wrap Firebase SDK for PHP package to laravel https://firebase-php.readthedocs.io/en/latest/cloud-messaging.html. You can use all available functions.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email balunjozef@gmail.com 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.