Git Product home page Git Product logo

fcm-notification's Introduction

Firebase Cloud Messaging (FCM) Notification Channel for Laravel

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;
    }
}

fcm-notification's People

Contributors

crynobone avatar feryardiant avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.