Git Product home page Git Product logo

Comments (9)

chimit avatar chimit commented on July 24, 2024 2

Still waiting for Laravel Notifications support.

from laravel-fcm.

MithrandirDK avatar MithrandirDK commented on July 24, 2024 1

I have created a NotificationServiceProvider and related classes in my own namespace, but it should be possible to move directly.

I have made a wrapper around the options and payload classes to make it possible for a toFCM() method on the notification to return a complete message, ready to be sent. I added helper methods to dispatch options, notification and data to the various builders, but maybe it is just as good to have $message->options->setTimeToLive($value) rather than $message->setOption('timeToLive', $value)?

In the FirebaseChannel::send() method, I chose to add the notification's toArray() result as payload data, because in my use cases, it seems to be a general desire - but maybe we should leave that up to the toFCM() method to decide?

NotificationServiceProvider.php

<?php

namespace App\Providers;

use App\Notifications\Channels\FirebaseChannel;
use Illuminate\Notifications\ChannelManager;
use Illuminate\Support\ServiceProvider;

/**
 * Class NotificationServiceProvider
 * @package DouglasResende\FCM
 */
class NotificationServiceProvider extends ServiceProvider
{
    /**
     * Register
     */
    public function register()
    {
        $app = $this->app;
        $this->app->make(ChannelManager::class)->extend('fcm', function() use ($app) {
            return $app->make(FirebaseChannel::class);
        });
    }
}

FirebaseChannel (for channeling notification messages)

<?php

namespace App\Notifications\Channels;

use App\Notifications\Contracts\FirebaseNotification;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Notifications\Notifiable;

/**
 * Class FirebaseChannel
 */
class FirebaseChannel
{
    /**
     * @var Config
     */
    private $config;

    /**
     * FirebaseChannel constructor.
     * @param Config $config
     */
    public function __construct(Config $config)
    {
        $this->config = $config;
    }

    /**
     * @param Notifiable $notifiable
     * @param \App\Notifications\Contracts\FirebaseNotification $notification
     */
    public function send(Notifiable $notifiable, FirebaseNotification $notification)
    {
        $message = $notification->toFCM($notifiable);
        $message->setTo($notifiable->routeNotificationFor('fcm'));

        $message->addData($notification->toArray());

        $message->send();

    }

}

FirebaseMessage

<?php


namespace App\Notifications\Messages;


use LaravelFCM\Facades\FCM;
use LaravelFCM\Message\OptionsBuilder;
use LaravelFCM\Message\PayloadDataBuilder;
use LaravelFCM\Message\PayloadNotificationBuilder;

class FirebaseMessage
{
    /**
     * @var OptionsBuilder
     */
    protected $optionsBuilder;
    /**
     * @var PayloadNotificationBuilder
     */
    protected $notificationBuilder;
    /**
     * @var PayloadDataBuilder
     */
    protected $dataBuilder;
    /**
     * @var array
     */
    protected $to = [];

    public function __construct()
    {
        $this->optionsBuilder = new OptionsBuilder();
        $this->notificationBuilder = new PayloadNotificationBuilder();
        $this->dataBuilder = new PayloadDataBuilder();
    }

    /**
     * Set value on OptionsBuilder
     * @param $key
     * @param $value
     *
     * @return $this
     */
    public function setOption($key, $value) {
        $method = 'set' . ucfirst($key);
        $this->optionsBuilder->$method($value);

        return $this;
    }

    public function setNotification($key, $value) {
        $method = 'set' . ucfirst($key);
        $this->notificationBuilder->$method($value);

        return $this;
    }

    public function addData(array $data) {
        $this->dataBuilder->addData($data);

        return $this;
    }

    /**
     * Add a FCM token to the recipients list
     *
     * @param $token
     */
    public function setTo($token) {
        $this->to[] = $token;
    }

    /**
     * Send notification to FCM
     */
    public function send() {
        $downstreamResponse = FCM::sendTo($this->to, $this->optionsBuilder->build(), $this->notificationBuilder->build(), $this->dataBuilder->build());
    }
}

And finally, the interface declaring the toFCM method

<?php
namespace App\Notifications\Contracts;

use App\Notifications\Messages\FirebaseMessage;
use Illuminate\Contracts\Support\Arrayable;


/**
 * Interface FirebaseNotification
 */
interface FirebaseNotification extends Arrayable
{
    /**
     * @param $notifiable
     * @return FirebaseMessage
     */
    public function toFCM($notifiable);

}

Let me know what you think.

from laravel-fcm.

francislavoie avatar francislavoie commented on July 24, 2024 1

+1 from me as well.

I was looking at the docs, and the way the lib is currently set up doesn't make a whole lot of sense for me, from a user perspective. You have a FCM facade, but to be able to use it you still need to use the builders, which pretty much counteracts the entire point of facades. You get trapped into a single implementation. The FCM facade (or FirebaseMessage as defined in a comment above) should wrap the builders so that they "could" be swapped out. It also makes writing code much nicer, no longer need to create a bunch of builders before actually sending messages, it just gets simplified into ->setOption() calls etc. I think this really a must-have for this lib to make is actually nice to use. The functionality is great, but usage can be significantly cleaned up.

from laravel-fcm.

brozot avatar brozot commented on July 24, 2024

Hello,

Thank you for your interest about this package. I have planned to explore a way to make this package compatible with Laravel Notifications. If you want to work on that we can collaborate.

Best regards

from laravel-fcm.

brozot avatar brozot commented on July 24, 2024

Hi sorry to answer only now

It looks good,

I will take a look more in detail tomorrow.

Thank you very much

from laravel-fcm.

tusharvikky avatar tusharvikky commented on July 24, 2024

Any update on this?

from laravel-fcm.

prasanthsd avatar prasanthsd commented on July 24, 2024

Are there any updates on this? This is one of the must have features

from laravel-fcm.

mpyw avatar mpyw commented on July 24, 2024

+1

Need updates

from laravel-fcm.

walidbagh avatar walidbagh commented on July 24, 2024

@brozot Hey, any news on this ?

from laravel-fcm.

Related Issues (20)

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.