nbulaienko/laravel-fcm-notification


Laravel FCM (Firebase Cloud Messaging) Notification Channel

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.nbulaienko:laravel-fcm-notification:'
	}
	dependencies {
		implementation("com.github.nbulaienko:laravel-fcm-notification:")
	}
	<dependency>
	    <groupId>com.github.nbulaienko</groupId>
	    <artifactId>laravel-fcm-notification</artifactId>
	    <version></version>
	</dependency>

                            
    libraryDependencies += "com.github.nbulaienko" % "laravel-fcm-notification" % ""
        
        

                            
    :dependencies [[com.github.nbulaienko/laravel-fcm-notification ""]]
        
        

Readme


laravel-fcm-notification

Laravel FCM (Firebase Cloud Messaging) Notification Channel

Latest Version Software License

Use this package to send push notifications via Laravel to Firebase Cloud Messaging. Laravel 5.3+ required.

Install

This package can be installed through Composer.

composer require benwilkins/laravel-fcm-notification:@dev-master

Once installed, add the service provider:

// config/app.php
'providers' => [
    ...
    Benwilkins\FCM\FcmNotificationServiceProvider::class,
    ...
];

Publish the config file:

php artisan vendor:publish --provider="Benwilkins\FCM\FcmNotificationServiceProvider"

The following config file will be published in config/laravel-fcm-notification.php. Add your Firebase API Key here.

return [
    /*
     * Add the Firebase API key
     */
    'api_key' => ''
];

Example Usage

Use Artisan to create a notification:

php artisan make:notification SomeNotification

Return [fcm] in the public function via($notifiable) method of your notification:

public function via($notifiable)
{
    return ['fcm'];
}

Add the method public function toFcm($notifiable) to your notification, and return an instance of FcmMessage:

public function toFcm($notifiable) 
{
    $message = new Benwilkins\FcmMessage();
    $message->content([
        'title'        => 'Foo', 
        'body'         => 'Bar', 
        'sound'        => '', // Optional 
        'icon'         => '', // Optional
        'click_action' => '' // Optional
    ])->data([
        'param1' => 'baz' // Optional
    ])->priority(Benwilkins\FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.
    
    return $message;
}

When sending to specific device, make sure your notifiable entity has routeNotificationForFcm method defined:

/**
 * Route notifications for the FCM channel.
 *
 * @return string
 */
public function routeNotificationForFcm()
{
    return $this->device_token;
}

When sending to a topic, you may define so within the toFcm method in the notification:

public function toFcm($notifiable) 
{
    $message = new Benwilkins\FcmMessage();
    $message->to('the-topic', $recipientIsTopic = true)
    ->content([...])
    ->data([...]);
    
    return $message;
}

License

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