Comments (9)
Still waiting for Laravel Notifications support.
from laravel-fcm.
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.
+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.
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.
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.
Any update on this?
from laravel-fcm.
Are there any updates on this? This is one of the must have features
from laravel-fcm.
+1
Need updates
from laravel-fcm.
@brozot Hey, any news on this ?
from laravel-fcm.
Related Issues (20)
- Laravel 7 Not Supported HOT 4
- Notification sound not working HOT 3
- Support guzzle 7.0
- send notification issue
- Change the connection on the fly
- SnederId Mismatch
- Lumen - Unable to resolve NULL driver for [LaravelFCM\FCMManager]. HOT 1
- onMessage payload not working HOT 1
- Laravel 8 HOT 6
- How to add badge on push notification using brozot/Laravel-FCM
- How to test FCM::sendTo with Feature test HOT 4
- Laravel 8 supported or not?(php 8) HOT 6
- How to send FCMGroup?
- Find exact error code or error message in responce
- How to add small icon on the right side of the notification.
- Not working on iOS devices HOT 2
- Your requirements could not be resolved to an installable set of packages. HOT 4
- Not compatible with guzzle ~7.0 HOT 1
- Not support in Laravel 9 HOT 1
- Not Support In Laravel 10 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-fcm.