katsana/fcm-notification

Firebase Cloud Messaging (FCM) Notification Channel for Laravel

v1.0.1 2020-04-03 06:00 UTC

This package is auto-updated.

Last update: 2024-10-29 05:41:08 UTC


README

This package makes it easy to send notifications using Firebase Cloud Messaging with Laravel 6.0+ using kreait/laravel-firebase

Build Status Latest Stable Version Total Downloads Latest Unstable Version License Coverage Status

Installation

FCM Notification can be installed via composer:

composer require "katsana/fcm-notification"

Configuration

This part is based on Firebase for Laravel Configuration

In order to access a Firebase project and its related services using a server SDK, requests must be authenticated. For server-to-server communication this is done with a Service Account.

The package uses auto discovery to find the credentials needed for authenticating requests to the Firebase APIs by inspecting certain environment variables and looking into Google's well known path(s).

If you don't already have generated a Service Account, you can do so by following the instructions from the official documentation pages at https://firebase.google.com/docs/admin/setup#initialize_the_sdk.

Once you have downloaded the Service Account JSON file, you can use it to configure the package by specifying the environment variable FIREBASE_CREDENTIALS in your .env file:

FIREBASE_CREDENTIALS=/full/path/to/firebase_credentials.json
# or
FIREBASE_CREDENTIALS=relative/path/to/firebase_credentials.json

For further configuration, please see config/firebase.php. You can modify the configuration by copying it to your local config directory with the publish command:

php artisan vendor:publish --provider="Kreait\Laravel\Firebase\ServiceProvider" --tag=config

Usages

If a notification supports being sent as an FCM, you should define a toFcm method on the notification class. This method will receive a $notifiable entity and should return a NotificationChannels\Fcm\Message instance:

use NotificationChannels\Fcm\Message;

// ...

/**
 * Get the FCM representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \NotificationChannels\Fcm\Message
 */
public function toFcm($notifiable)
{
    return (new Message)
        ->notification('Your title', 'Your body');
}

Routing FCM Notifications

When sending notifications via the fcm channel, the notification system will automatically look for routeNotificationForFcm method on the entity:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

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