Lead developer frontend
maximiliengilet / notification-bundle Goto Github PK
View Code? Open in Web Editor NEWA simple Symfony bundle to notify user
Home Page: https://packagist.org/packages/mgilet/notification-bundle
License: MIT License
A simple Symfony bundle to notify user
Home Page: https://packagist.org/packages/mgilet/notification-bundle
License: MIT License
Hi Maximilien,
I'm really struggle to render "notifications.html.twig", can you show me how to do it?
Thanks
Hello,
I've started to use your bundle and I would to know if it's possible to add a field to know which entities create the notification.
For example :
On facebook each notification have a photo on left that corresponds to the entity who create the notification.
Thanks
First of all thanks for this bundle.
Since symfony 5 is out, it would be great to fix the deprecation messages coming from this bundle :
The "Mgilet\NotificationBundle\Controller\NotificationController" class extends "Symfony\Bundle\FrameworkBundle\Controller\Controller" that is deprecated since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.
Which shouldnt be too hard to fix ^^
Hello,
I'm trying to use the template _notification.html.twig but i got this error :
Unknown "mgilet_notification_generate_path" function. Did you mean "mgilet_notification_render", "mgilet_notification_unseen_count"?
I can't find this function in your code.
Symfony version : 4.1
NotificationBundle : 3.0
Thanks a lot
I couldn't find in docs or code whether it is possible (ready to use) or doesn't require a lot of extending to:
I am using this notification bundle with Symfony 5, it works perfectly, but I got stuck to use the following mentioned events with Symfony 5.
These are the following events, and few of them are required for me to fetch and show details runtime at the time of event dispatch. I want to up bell count no when a notification is created or assigned. To do this there are events. I was tried to use it but it always shows in the not listened event list in the profiler.
'mgilet.notification.created'
'mgilet.notification.assigned' -> when a notification is added to a notifiable entity
'mgilet.notification.seen'
'mgilet.notification.unseen'
'mgilet.notification.modified'
'mgilet.notification.removed'
'mgilet.notification.delete'
@marcelglaeser @avramcosmin @matthieumota @Viperoo @emulienfou @4ssil
Hey guys can anyone of you please help me in this.
I have seen @aquibbaig you also done this, please help me there.
Hey, just tried out your bundle with symfony flex and facing some issues. How to define notifiableEntity in twig template using mgilet_notification_render()? Just want to see user notifications, but can't pass the entity.
Ensure the bundle is compatible with Symfony 4
So, maybe it's me and I don't get it, but how do you deal with security?
I mean, the route @route("/{notifiable}") is kinda open to everyone. Yes, you can put /notifications behind a firewall, but once you login, you can start opening /notifications/{notifiable} however you like.
In our case, we have a User notifiable entity and the ids are easily guessable (1,2,3, etc...), so anyone who's logged in can start looking at everyone else's notifications.
I hope I am stupid and the solution is very obvious. Enlighten me, please :)
The only thing I can think of at the top of my head is just disable the /notifications route and call it a day. I doubt this will work, however (marking as seen and unseen probably will stop working).
Hi i'm getting the notification by this code
{% for item in notificationList %}
<li class="media notification-message">
<a href="{# path('mark_notifcation_as_seen',{'notification':item.notification.id}) #}">
<div class="media-body">
<p class="m-0 noti-details">{{item.notification.message}}</p>
<p class="m-0"><span class="notification-time">{{item.notification.date|date }}</span></p>
</div>
</a>
</li>
{% endfor %}
so what i need is when the current user click on one of this notification that notification has to mark as read and redirect user to another view
what should i do
Hi Maximilien,
In the default template (vendor/mgilet/notification-bundle/Resources/views/notifications.html.twig) this error is trigerred when trying to call notificationList|first.notifiableEntity for generating 'notification_mark_all_as_seen' route : Impossible to access an attribute ("notifiableEntity") on a boolean variable ("").
I'm facing this issue while installing the bundle please help
I have Difficulty to add notifications in symfony
I use this bundle https://github.com/maximilienGilet/notification-bundle
I have followed the doc, but i have encountered difficulty
THis what i have added in controller ( add annonce)
class TestController extends AbstractController
{
/**
* @Route("/", name="test")
*/
public function index(Request $request)
{..
if ($form->isSubmitted() && $form->isValid()) {
$manager = $this->get('mgilet.notification');
$notif = $manager->createNotification('Nouveau candidat !');
$notif->setMessage('X a entré un candidat');
$notif->setLink('http://symfony.com/');
$manager->addNotification(array($this->getUser()), $notif, true);
return $this->redirectToRoute('index');
}
And this what i have added in twig {{ mgilet_notification_render(app.user) }}
But after adding annonce i see this error
Service "mgilet.notification" not found: even though it exists in the app's container, the container inside "App\Controller\TestController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session", "templating" and "twig" services. Try using dependency injection instead.
Maybee i haven't know how to show notifications in twig,
the documentation is not very clear for beginners
I don't think this helper has any added value compared to the helper already included in Symfony:
https://symfony.com/doc/current/templating.html#linking-to-pages
This is just one more thing to document and maintain and also one more thing for the user to learn which could be avoided.
I want to see notification list one by one not all in same row by using {{ mgilet_notification_render(notifiableEntity) }}
Please i want the code twig of this view
https://camo.githubusercontent.com/aa3ef4e798bf4d2ac1aca1e6fa048f2ea40f6360/687474703a2f2f692e696d6775722e636f6d2f30374f634636632e676966
Can you share it ??
When marking notifications as read I'm getting a method not allowed exception.
Here is the exact error message:
No route found for "GET /notifications/11/mark_as_seen": Method Not Allowed (Allow: POST)
Also, everything works fine on Chrome and Firefox.
I'm trying install notification bundle on Symfony 4.4 with Twig 3.0.4 but problem is following: "Argument 3 passed to Mgilet\NotificationBundle\Twig\NotificationExtension::__construct() must be an instance of Twig_Environment".
I found the solution in construct we need change "\Twig_Environment $twig" on "\Twig\Environment $twig", because \Twig_Environment is deprecated.
"Using the "Twig_Environment" class is deprecated since Twig version 2.7, use "Twig\Environment" instead."
Hi everyone !
Just to let you know that with the last version of symfony (4.3.3), i had to add thoses lines of config in my doctrine.yaml for make the bundle works !
orm:
entity_managers:
mappings:
MgiletNotificationBundle:
is_bundle: true
type: annotation
prefix: 'Mgilet\NotificationBundle\Entity'
alias: MgiletNotificationBundle
In case of someone want to use the bundle and have the "Unknown Entity namespace alias 'MgiletNotificationBundle'." error !
Thanks for your work, still really great 👍
There seems to be no convenient way to get a NotificationEntity
id using an instance of a class implementing the NotificationInterface
.
While it's easy to get the notifications using mgilet_notification_render(my_entity, { 'display': 'list', 'seen': true })
,
it's really hard to use the routes notification_mark_as_seen
and notification_mark_all_as_seen
once you iterate over your notifications.
What is the best practice here ?
Dear all,
Actually i am using your bundle but i have some problem
so first problem i can't reproduce your example or include your twig in mine!!
can you helps me plaise
Hi,
I added an entityemail type array
in the table Notification
. But when i'm trying to persist i get this error:
Catchable Fatal Error: Argument 2 passed to Mgilet\NotificationBundle\Manager\NotificationManager::addNotification() must be an instance of Mgilet\NotificationBundle\Model\AbstractNotification, array given, called in C:\xampp\htdocs\Symfony\src\FLY\BookingsBundle\Controller\PostController.php on line 793 and defined
$manager = $this->get('mgilet.notification');
$notif = $manager->generateNotification('Test');
//$notif->setMessage();
//$notif->setLink();
$commande = array();
foreach ($entities as $entity) {
$notif->setEmail($notif);
$notif = $commande['entity'][$entity->getId()] = array(
$entity->getEmail());
}
$manager->addNotification($this->getUser(), $notif);
$em->persist($notif);
$em->flush();
if I delete this line : $manager->addNotification($this->getUser(), $notif);
I will get the following error:
EntityManager#persist() expects parameter 1 to be an entity object, array given.
How can i fix this error ?
Thank you
Hello,
There is no way to make the bundle finds out the path to notification_list.html.twig
Have you met this issue before ?
Thanks for the effort :)
The view MgiletNotificationBundle::notifications.html.twig
make one request per row because the method NotifiableNotificationRepository->findAllForNotifiableIdQb
join on the notification table but do not select the fields.
Which later cause them to be lazy loaded one by one when you acces the proxy in twig {{ notifiableNotification.notification }}
Add use case example and integration examples (eg JS dropdowns)
Hi,
In a Symfony 3.4 project, I use this bundle and Doctrine.
When I install Doctrine Migrations bundle, the warmup command return this error:
In ExceptionCaster.php line 289:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Maximum function nesting level of '256' reached, aborting!
[...]
Closure->__invoke() at /home/me/dev/var/cache/dev/ContainerFvefxol/EntityManager_9a5be93.php:298
EntityManager_9a5be93->getRepository() at /home/me/dev/vendor/mgilet/notification-bundle/Manager/NotificationManager.php:44
Mgilet\NotificationBundle\Manager\NotificationManager->__construct() at /home/me/dev/var/cache/dev/ContainerFvefxol/appDevDebugProjectContainer.php:2100
ContainerFvefxol\appDevDebugProjectContainer->getMgilet_NotificationService() at /home/me/dev/var/cache/dev/ContainerFvefxol/appDevDebugProjectContainer.php:1823
ContainerFvefxol\appDevDebugProjectContainer->getDoctrine_Dbal_DefaultConnectionService() at /home/me/dev/var/cache/dev/ContainerFvefxol/appDevDebugProjectContainer.php:1908
ContainerFvefxol\appDevDebugProjectContainer->getDoctrine_Orm_DefaultEntityManagerService() at /home/me/dev/var/cache/dev/ContainerFvefxol/appDevDebugProjectContainer.php:1867
ContainerFvefxol\appDevDebugProjectContainer->ContainerFvefxol\{closure}() at /home/me/dev/var/cache/dev/ContainerFvefxol/EntityManager_9a5be93.php:298
Closure->__invoke() at /home/me/dev/var/cache/dev/ContainerFvefxol/EntityManager_9a5be93.php:298
EntityManager_9a5be93->getRepository() at /home/me/dev/vendor/mgilet/notification-bundle/Manager/NotificationManager.php:44
Mgilet\NotificationBundle\Manager\NotificationManager->__construct() at /home/me/dev/var/cache/dev/ContainerFvefxol/appDevDebugProjectContainer.php:2100
ContainerFvefxol\appDevDebugProjectContainer->getMgilet_NotificationService() at /home/me/dev/var/cache/dev/ContainerFvefxol/appDevDebugProjectContainer.php:1823
ContainerFvefxol\appDevDebugProjectContainer->getDoctrine_Dbal_DefaultConnectionService() at /home/me/dev/var/cache/dev/ContainerFvefxol/appDevDebugProjectContainer.php:1908
ContainerFvefxol\appDevDebugProjectContainer->getDoctrine_Orm_DefaultEntityManagerService() at /home/me/dev/var/cache/dev/ContainerFvefxol/appDevDebugProjectContainer.php:1867
ContainerFvefxol\appDevDebugProjectContainer->ContainerFvefxol\{closure}() at /home/me/dev/var/cache/dev/ContainerFvefxol/EntityManager_9a5be93.php:298
Closure->__invoke() at /home/me/dev/var/cache/dev/ContainerFvefxol/EntityManager_9a5be93.php:298
EntityManager_9a5be93->getRepository() at /home/me/dev/vendor/mgilet/notification-bundle/Manager/NotificationManager.php:44
Mgilet\NotificationBundle\Manager\NotificationManager->__construct() at /home/me/dev/var/cache/dev/ContainerFvefxol/appDevDebugProjectContainer.php:2100
ContainerFvefxol\appDevDebugProjectContainer->getMgilet_NotificationService() at /home/me/dev/var/cache/dev/ContainerFvefxol/appDevDebugProjectContainer.php:2359
ContainerFvefxol\appDevDebugProjectContainer->getTwigService() at /home/me/dev/var/cache/dev/ContainerFvefxol/getTwig_CacheWarmerService.php:12
ContainerFvefxol\appDevDebugProjectContainer->{closure}() at /home/me/dev/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/ServiceLocator.php:64
Symfony\Component\DependencyInjection\ServiceLocator->get() at /home/me/dev/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheCacheWarmer.php:63
Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheCacheWarmer->warmUp() at /home/me/dev/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php:52
Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate->warmUp() at /home/me/dev/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheWarmupCommand.php:96
Symfony\Bundle\FrameworkBundle\Command\CacheWarmupCommand->execute() at /home/me/dev/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:255
Symfony\Component\Console\Command\Command->run() at /home/me/dev/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:1005
Symfony\Component\Console\Application->doRunCommand() at /home/me/dev/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:86
Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /home/me/dev/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:255
Symfony\Component\Console\Application->doRun() at /home/me/dev/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:74
Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /home/me/dev/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:148
Symfony\Component\Console\Application->run() at /home/me/dev/bin/console:27
Hi, I've added this to my nav header: {{ mgilet_notification_render(app.user) }}
, but there is no html in the rendered string. Not even if I use the twig raw
filter.
So after adding the demo notification a couple of time, the rendered list looks like this:
<nav class="collapse navbar-collapse" role="navigation">
<p>Notifications :
Hello world! - This a notification.
Hello world! - This a notification.
</p>
...
What could cause this, and how could it be fixed?
The entities NotifiableEntity
NotifiableNotification
and Notification
have hardcoded table names:
/**
* Class NotifiableEntity
* @package Mgilet\NotificationBundle\Entity
*
* @ORM\Table(name="notifiable")
* @ORM\Entity(repositoryClass="Mgilet\NotificationBundle\Entity\Repository\NotifiableRepository")
* @UniqueEntity(fields={"identifier", "class"})
*/
class NotifiableEntity
This is bad practice because as it force Doctrine to bypass any custom naming strategy the user may have implemented and may lead to unsolvable name collision.
You should just use @ORM\Table
without name property:
/**
* Class NotifiableEntity
* @package Mgilet\NotificationBundle\Entity
*
* @ORM\Table
* @ORM\Entity(repositoryClass="Mgilet\NotificationBundle\Entity\Repository\NotifiableRepository")
* @UniqueEntity(fields={"identifier", "class"})
*/
class NotifiableEntity
You can also just remove the annotation as you do not have any property left:
/**
* Class NotifiableEntity
* @package Mgilet\NotificationBundle\Entity
*
* @ORM\Entity(repositoryClass="Mgilet\NotificationBundle\Entity\Repository\NotifiableRepository")
* @UniqueEntity(fields={"identifier", "class"})
*/
class NotifiableEntity
Hi maximilienGilet ,
im also using your bundle, everytime i tape {{ mgilet_notification_render(app.user) }} , the page wont refresh and gives me a server problem , can you please help ?
Thank you so much
Is it possible to override notification entity ? i start using your bundle and i want to map a relation between notification entity and my own entity, how can override for exemple the attribute subject in notification entity ?
There's o need for this field to be so short, it becomes varchar in the db anyways and thus the size is flexible. Hard limit here doesn't allow for html messages that would normally fit in 4000
Hi Maximilien,
I saw that everyone can access the routes, and see the notifications, mark them as seen, etc.
Shouldn't there be some control to be sure that the current user is matching the notifiable entity?
It might be me, and my lack of PHP experience, but when I'm upgrading from v1.0 (or dev-master as it is in the composer json file), I end up getting this from composer:
[ReflectionException]
Class Mgilet\NotificationBundle\Model\AbstractNotification not found
[RuntimeException]
An error occurred when executing the ""cache:clear --no-warmup"" command:
[Symfony\Component\Config\Exception\FileLoaderLoadException]
Class Mgilet\NotificationBundle\Model\AbstractNotification not found in xxxxxx\app/config\services.yml (which is being imported from xxxxxxxx\app/config\config.yml").
This seems quite specific to this package, which is why I ask - I hope you can help!
In the template MgiletNotificationBundle::notification_list.html.twig
the notificationList
is an array of objects with the seen
property and a Notification instance.
Fetching whole NotifiableNotification
entities would not cost any performance overhead (you are already joining on the table) and provides several advantages:
notification_list.html.twig
and notification.html.twig
NotifiableEntity->id
at your disposalJsonSerializable
interface; that would make notifications refreshing (or lazy load) much more practicalnotificationItem[0]
in your twig template and have something like item.notification
and item.notifiable.id
I have this error, if you could help me
Too few arguments to function Mgilet\NotificationBundle\Twig\NotificationExtension::countNotifications(), 0 passed in D:\Datos\xampp7\htdocs\SIBHP\var\cache\dev\twig\9a\9adaa1539f219a41e1ccbdefba3dfe8d988e67b798e7dbc8adda16cf97849646.php on line 96 and exactly 1 expected
I currently tried to use this bundle in a Symfony 5.1.3 but I ran into a lot of errors.
Any plans to update this to 5.x?
Hi @marcelglaeser @avramcosmin @emulienfou @matthieumota @Viperoo @4ssil @maximilienGilet I am new in Symfony and trying to use this notification bundle in my project but didn't get proper configurations steps for Symfony 4.4 can anyone in the group please help me. #HELP REQUIRED
Add an option in the manager and twig functions to fetch a limited number of notifications
The route mark_notifications_as_seen
returns a JsonResponse true
rather than redirecting users to a new view. The functionality seems to work well, Just that a view should be rendered at the end
Hi Maximilien!
I added your bundle to my project and it's working fine! But I'm wondering how can I improve it by updating the notification count without refreshing the whole page.
I have one admin user which sees new notifications when many client users do a specific action. I want to update the notification count automaticaly for the admin when a new notification is created, as a Facebook notification, in realtime.
I hope you can help me or give me a clue.
Thanks in advance!
Support databases such as MongoDB...
When calling NotificationManager->removeNotification() only NotifiableNotifications are removed (the linking entity) and not the notification from the database. Currently you must calle NotificationManager->deleteNotification() afterwards to delete the notification from the database. This seems very redundant because removeNotification() should remove the notification.
Hello,
what about to create some commands to accomplish common useful actions?
For example:
php bin/console mgilet:notification:clear_notification
For development purpose i had to clear notification tables several times.
If interested I already have the code
Is there a methos for send a notification to all users?
Actually, I do:
// Get all users
$users = $this->getDoctrine()->getRepository('AppBundle:User')->findAll();
foreach ($users as $user) {
$manager->addNotification($user, $notification);
}
But it is slow when I have many users
Notifications should be sorted by descending date by default.
Maybe you should provide a parameter in the unlikely case some users may need the oldest first.
Hello,
I tried overriding the Notification entity following your guide:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Mgilet\NotificationBundle\Entity\NotificationInterface;
use Mgilet\NotificationBundle\Model\Notification as NotificationModel;
/**
* Notification
*
* @ORM\Entity(repositoryClass="AppBundle\Repository\NotificationRepository")
*/
class Notification extends NotificationModel implements NotificationInterface
{
/**
* @var string
*
* @ORM\Column(name="`type`", type="string", length=255)
*/
private $type;
/**
* @var string|null
*
* @ORM\Column(name="sender", type="string", length=255, nullable=true)
*/
private $sender;
/**
* Set type.
*
* @param string $type
*
* @return Notification
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type.
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Set sender.
*
* @param string|null $sender
*
* @return Notification
*/
public function setSender($sender = null)
{
$this->sender = $sender;
return $this;
}
/**
* Get sender.
*
* @return string|null
*/
public function getSender()
{
return $this->sender;
}
}
Unfortunately it's not working as it tries to create the table twice:
mysite git:(master) ✗ sf doctrine:schema:update --dump-sql | highlight -l sql
In SchemaException.php line 108:
The table with name 'mysite.notification' already exists.
doctrine:schema:update [--complete] [--dump-sql] [-f|--force] [--em [EM]] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>
Looking at the output it's trying to execute the query twice. This is my config:
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
dql:
numeric_functions:
rand: DoctrineExtensions\Query\Mysql\Rand
orm:
resolve_target_entities:
Mgilet\NotificationBundle\Entity\Notification: AppBundle\Entity\Notification
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.