2017 © Pedro Peláez
 

library laravel-fcm-notification

Laravel FCM (Firebase Cloud Messaging) Notification Channel

image

benwilkins/laravel-fcm-notification

Laravel FCM (Firebase Cloud Messaging) Notification Channel

  • Thursday, May 3, 2018
  • by benwilkins
  • Repository
  • 8 Watchers
  • 41 Stars
  • 21,807 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 22 Forks
  • 4 Open issues
  • 6 Versions
  • 24 % Grown

The README.md

Laravel FCM Notification

Laravel FCM (Firebase Cloud Messaging) Notification Channel, (*1)

GitHub tag Packagist Downloads Build Status License, (*2)

Use this package to send push notifications via Laravel to Firebase Cloud Messaging. Laravel 5.5+ required., (*3)

This package works only with Legacy HTTP Server Protocol, (*4)

Install

This package can be installed through Composer., (*5)

``` bash composer require benwilkins/laravel-fcm-notification, (*6)


If installing on < Laravel 5.5 then add the service provider: ```php // config/app.php 'providers' => [ ... Benwilkins\FCM\FcmNotificationServiceProvider::class, ... ];

Add your Firebase API Key in config/services.php., (*7)

return [

    ...
    ...
    /*
     * Add the Firebase API key
     */
    'fcm' => [
        'key' => env('FCM_SECRET_KEY')
     ]
];

Example Usage

Use Artisan to create a notification:, (*8)

php artisan make:notification SomeNotification

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

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

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

use Benwilkins\FCM\FcmMessage;

...

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

    return $message;
}

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

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

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

use Benwilkins\FCM\FcmMessage;

...

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

    return $message;
}

Or when sending with a condition:, (*13)

use Benwilkins\FCM\FcmMessage;

...

public function toFcm($notifiable) 
{
    $message = new FcmMessage();
    $message->contentAvailable(true)
        ->priority('high')
        ->condition("'user_".$notifiable->id."' in topics")
        ->data([...]);

    return $message;
}

You may provide optional headers or override the request headers using setHeaders():, (*14)

use Benwilkins\FCM\FcmMessage;

...

public function toFcm($notifiable) 
{
    $message = new FcmMessage();
    $message->setHeaders([
        'project_id'    =>  "48542497347"   // FCM sender_id
    ])->content([
        'title'        => 'Foo', 
        'body'         => 'Bar', 
        'sound'        => '', // Optional 
        'icon'         => '', // Optional
        'click_action' => '' // Optional
    ])->data([
        'param1' => 'baz' // Optional
    ])->priority(FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.

    return $message;
}

Interpreting a Response

To process any laravel notification channel response check Laravel Notification Events, (*15)

This channel return a json array response:, (*16)

 {
    "multicast_id": "number",
    "success": "number",
    "failure": "number",
    "canonical_ids": "number",
    "results": "array"
 }

Check FCM Legacy HTTP Server Protocol for response interpreting documentation., (*17)

License

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

The Versions

13/06 2017

v1.1

1.1.0.0

Laravel FCM (Firebase Cloud Messaging) Notification Channel

  Sources   Download

The Requires

 

by Ben Wilkins

25/05 2017

v1.0

1.0.0.0

Laravel FCM (Firebase Cloud Messaging) Notification Channel

  Sources   Download

The Requires

 

by Ben Wilkins

06/12 2016

v1.0-beta.1

1.0.0.0-beta1

Laravel FCM (Firebase Cloud Messaging) Notification Channel

  Sources   Download

The Requires

 

by Ben Wilkins