Laravel FCM (Firebase Cloud Messaging) Notification Channel

Overview

Laravel FCM Notification

Laravel FCM (Firebase Cloud Messaging) Notification Channel

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

This package works only with Legacy HTTP Server Protocol

Install

This package can be installed through Composer.

composer require vishaldudhatra/laravel-fcm-notification

If installing on < Laravel 5.5 then add the service provider:

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

Add your Firebase API Key in config/services.php.

return [
   
    ...
    ...
    /*
     * Add the Firebase API key
     */
    'fcm' => [
        'key' => env('FCM_SECRET_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:

use vishaldudhatra\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:

/**
 * 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:

use vishaldudhatra\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:

use vishaldudhatra\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():

use vishaldudhatra\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

This channel return a json array response:

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

Check FCM Legacy HTTP Server Protocol for response interpreting documentation.

License

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

You might also like...
Multiple channels of laravel exception notification(DingTalk、FeiShu、ServerChan、WeWork、XiZhi). - 多种通道的 laravel 异常通知(钉钉群机器人、飞书群机器人、Server 酱、企业微信群机器人、息知)。
Multiple channels of laravel exception notification(DingTalk、FeiShu、ServerChan、WeWork、XiZhi). - 多种通道的 laravel 异常通知(钉钉群机器人、飞书群机器人、Server 酱、企业微信群机器人、息知)。

laravel-exception-notify 简体中文 | ENGLISH Multiple channels of laravel exception notification(DingTalk、FeiShu、ServerChan、WeWork、XiZhi). - 多种通道的 laravel

A Toast notification library for the Laravel TALL stack.
A Toast notification library for the Laravel TALL stack.

A Toast notification library for the Laravel TALL stack. You can push notifications from the backend or frontend to render customizable toasts with almost zero footprint on the published CSS/JS 🔥🚀

Slack notification for Laravel as it should be. Easy, fast, simple and highly testable.
Slack notification for Laravel as it should be. Easy, fast, simple and highly testable.

Based on illuminate/mail About Laravel Slack Slack notification for Laravel as it should be. Easy, fast, simple and highly testable. Since it uses On-

AmaranJS stylish notification for your laravel application.

AmaranJS Laravel 5 Package AmaranJS L5 package is a Laravel wrapper for my jquery plugin AmaranJS.You can create easy and stylish notifications with A

Our Laravel Sendgrid Notification package

laravel-sendgrid-notification-channel Laravel Sendgrid Notification channel Installation To get started, you need to require this package: composer re

 Lara-Izitoast : Laravel Notification Package
Lara-Izitoast : Laravel Notification Package

Lara-Izitoast : Laravel Notification Package This is a laravel notification wrapper build with http://izitoast.marcelodolce.com javascript library. Ho

Notification package for Laravel

Package is looking for maintainers Please contact me if interested. Notification package for Laravel4 / Laravel5 A simple notification management pack

Laravel notification manager

Easily manage notifications and notification subscriptions in your Laravel application.

Livewire Package to display Toast Notification based on TALL Stack.
Livewire Package to display Toast Notification based on TALL Stack.

livewire-toast Livewire Package to display Toast Notification based on TALL Stack. Requirements Make sure that Livewire is installed properly on your

Releases(laravel-fcm-notification)
Owner
Vishal Dudhatra
Vishal Dudhatra
@Authy notification channel for @Laravel, with the ability to send in-app, sms, and call verification tokens.

Authy Notification Channel for Laravel Authy notification channel for Laravel, with the ability to send in-app, sms, and call verification tokens. Tab

Laravel Notification Channels 57 Dec 19, 2022
Google Chat - Laravel Notification Channel

Google Chat - Laravel Notification Channel This package makes it easy to send notifications using Google Chat , (formerly known as Hangouts Chat) with

Laravel Notification Channels 36 Dec 6, 2022
Laravel SMS Notification Channel

Laravel SMS Notification Channel Installation composer require guangda/laravel-sms-notification-channel env 配置 SMS_PROVIDER=yunpian SMS_SIGNATURE=【签名

Guangda 6 Dec 29, 2021
Lark Notification Channel for laravel.

Lark notifications channel for Laravel This package makes it easy to send Lark using the Laravel notification system. Supports 5.5+, 6.x, 7.x and 8.x.

null 1 Nov 3, 2021
An SMS notification channel for the PHP framework Laravel.

Laravel SMS notification channel An SMS notification channel for the PHP framework Laravel. Supported SMS gateways: 46elks Cellsynt Telenor SMS Pro Tw

Andreas 2 Jan 22, 2022
Larafirebase is a package thats offers you to send push notifications or custom messages via Firebase in Laravel.

Introduction Larafirebase is a package thats offers you to send push notifications or custom messages via Firebase in Laravel. Firebase Cloud Messagin

Kutia Software Company 264 Jan 7, 2023
Send Firebase push notifications with Laravel php framework.

FCM Notification Channel for Laravel Send Firebase push notifications with Laravel php framework. Installation You can install this package via compos

Ankur Kumar 23 Oct 31, 2022
📨 Facebook Notifications Channel for Laravel

Facebook Notifications Channel for Laravel This package makes it easy to send notifications using the Facebook Messenger with Laravel. Contents Instal

Laravel Notification Channels 142 Dec 27, 2022
✈️ Whatsapp Notifications Channel for Laravel

Whatsapp Notifications Channel for Laravel This package makes it easy to send Whatsapp notification using Venom API with Laravel. This package was cre

Felipe Damaceno Teodoro 63 Dec 7, 2022
Notion.so notifications channel for Laravel

Installation composer require astroshippers/notion-notification-channel Usage Inside eloquent model: public function routeNotificationForNotion(): arr

AstroShippers 3 Jul 4, 2022