• v5.0
    • Versions
    • master

 

  • Install Akeneo PIM
    • Install Akeneo PIM for development with Docker
    • Install Akeneo PIM manually
      • System Requirements
      • System installation on Debian 10 (Buster)
      • System installation on Ubuntu 18.04 (Bionic Beaver)
      • System installation on Ubuntu 20.04 (Focal Fossa)
      • Installing Akeneo PIM Community Edition (CE)
      • Installing Akeneo PIM Enterprise Edition (EE) with the Archive
      • Setting up the job queue daemon
      • Setting up the Events API
    • How to customize the Dataset
    • How to Add Translation Packs
  • Upgrade Akeneo PIM projects
    • How to apply a patch?
      • How to apply a patch - Community Edition
      • How to apply a patch - Enterprise Edition - Flexibility Cloud offer
      • How to apply a patch - Enterprise Edition - On Premise offer
    • Where is the Changelog?
    • How to upgrade to a minor version?
    • How to upgrade to a major version?
      • Upgrade from 3.2 to 4.0
      • Upgrade from 4.0 to 5.0
  • Import and Export data
    • How import works
    • Understanding the Product Import
    • Understanding the Product Export
    • Formats
      • Localized labels
      • Scopable labels
      • Association types data structure
      • Attribute data structure
      • Category data structure
      • Family data structure
      • Family variant data structure
      • Group data structure
      • Options data structure
      • Product data structure
      • Product model data structure
    • Akeneo Connectors
    • How to Customize Import / Export
      • How to create a new Connector
      • How to import Products from a XML file
      • How to clean a CSV file during a Product import
      • How to automate imports/exports
  • Manipulate the Akeneo PIM data
    • How to Customize Mass Edit Operations
      • How to register a new bulk action
      • How to Register a New Mass Edit Action on Products
    • How to Manipulate Products
      • How to Query Products
      • How to Create Products
      • How to Update Products
      • How to Validate Products
      • How to Save Products
      • How to Remove Products
    • How to Manipulate Non-Product Objects
      • How to Query Non-Product Objects
      • How to Create Non-Product Objects
      • How to Update Non-Product Objects
      • How to Validate Non-Product Objects
      • How to Save Non-Product Objects
      • How to Remove Non-Product Objects
    • How to add a custom action rule
      • General information about rule format
      • How to add a custom action in the rule engine
    • How to Define Access Control List
    • How to Customize the Catalog Structure
      • How to Create a Reference Data
    • How To Customize Teamwork Assistant (Enterprise Edition)
      • Customize notifications
      • Add a calculation step
      • How to log calculation step
      • Remove projects impacted by a custom catalog update
    • How to store assets externally
    • How to Configure Measurement Limits
  • Maintain Akeneo PIM projects
    • First aid kit
    • Bug qualification
    • Common issues
    • Scalability Guide
      • Audit with 3 Representative Catalogs
      • More than 10k attributes?
      • More than 10k families?
      • More than 10k categories?
      • More than 500 attributes usable in the product grids?
      • More than 100k products to export?
      • More than 1GB of product media to export?
    • How to purge history
      • How to Purge jobs executions
      • How to adapt the version purger to your needs
  • Contribute to Akeneo PIM
    • How to report an issue?
    • How to translate the user interface?
    • How to enhance the documentation?
    • How to contribute to a Connector?
    • How to submit a patch to the PIM?
    • How to contribute to the frontend part of the application
    • How behavior tests are architectured in the PIM?
      • Establishing Decorator Pattern
      • Using Spin In Behat
  • Use SSO authentication locally
  • Reference Entities
    • Configure Entity Limits
    • Create a new Reference Entity Attribute type
    • Enrich Records with a new Reference Entity Attribute type
    • Add a Custom Property to Your Custom Attribute Type
    • Refresh records completeness
  • Troubleshooting guide
  • Technical overview
    • Product Information
    • Teamwork Assistant (Enterprise Edition)
      • Project creation
      • Project completeness
      • Project Completeness widget
      • Catalog update impact
      • Scalability guide
      • Users permission summary for Behat tests
    • Collaborative workflow
      • Simple workflow
      • Partial workflow
  • Technical architecture
    • Best Practices
      • Create a project
      • Create a reusable bundle
      • Code Conventions
      • Coding Standards
    • How to implement your business logic using the event system
    • Events
      • Storage events
      • Workflow events (Enterprise Edition only)
    • How to Localize your data
      • How to change the PIM locale
      • How to Use Localizers
      • How to use Presenters
    • How to Add a Notification
    • Performances Guide
      • Memory usage of rules execution (Enterprise Edition)
      • Memory leak fix in Rules Engine (ORM)
      • More than 100 WYSIWYG editors in a page
      • PHP7 and HHVM Compatibility?
      • Job product batch size
    • How to Use the Web REST API
    • Standard format
      • Products
      • Other entities
      • Usage
    • Application Technical Information
      • Application Technical Dependencies
      • Server side set up for hosting
      • System Requirements
      • Recommended configuration
      • Client side configuration and compatibilities
      • Operation processes
      • Flow Matrix
  • Akeneo Cloud Edition
    • Flexibility
      • Environment accesses
      • System Administration & Services Management
      • Periodic tasks & Crontab configuration
      • Composer settings
      • Queue Management & Workers
      • Disk Usage Management
    • Serenity
  • Akeneo Onboarder
    • Prerequisites
    • How to install the Onboarder bundle
    • Synchronization
    • How to update a minor version or to apply a patch
      • How to update the Onboarder bundle - Enterprise Edition - Flexibility Cloud offer
      • How to update the Onboarder bundle - Enterprise Edition - On Premise offer
    • How to upgrade to a major version
    • Troubleshooting
    • How to uninstall the Onboarder bundle
    • Environment variables
      • Using the DotEnv file
      • Using environment variables

How to Add a Notification¶

The Akeneo PIM comes with a notification system.

Prerequisites¶

If you need to send a custom message for an existing notification type (like import/export, mass edit, etc.) you can go to the Inject Services step.

If you want to create your own notification type for a custom bundle, you will need to create your notification factory.

Note

This cookbook assumes that you already created a new bundle to add your new Notification. Let’s assume its namespace is Acme\CustomBundle.

Create a Notification Factory¶

First we’ll need to create the Notification Factory that will build our own notifications. This class should implement Akeneo\Platform\Bundle\NotificationBundle\Factory\NotificationFactoryInterface.

# /src/Acme/Bundle/CustomBundle/Notification/CustomNotificationFactory.php
<?php

namespace Acme\CustomBundle\Notification;

use Akeneo\Platform\Bundle\NotificationBundle\Entity\Notification
use Akeneo\Platform\Bundle\NotificationBundle\Factory\NotificationFactoryInterface;

class CustomNotificationFactory implements NotificationFactoryInterface
{
    /** @var array */
    protected $notificationTypes;

    /**
     * @param array $notificationTypes
     */
    public function __construct(array $notificationTypes)
    {
        $this->notificationTypes = $notificationTypes;
    }

    /**
     * {@inheritdoc}
     */
    public function create($object)
    {
        $notification = new Notification(); // we'll setup it just after

        return $notification;
    }

    /**
     * {@inheritdoc}
     */
    public function supports($type)
    {
        return in_array($type, $this->notificationTypes);
    }
}

Let’s take a look at what’s important here, the create method of this factory:

public function create($object)
{
    $notification = new Notification();

    $notification->setType($type);
    $notification->setMessage($message);
    $notification->setMessageParams($messageParams);
    $notification->setRoute($route);
    $notification->setRouteParams($routeParams);
    $notification->setContext($context);

    return $notification;
}

Note that $object is mixed, so feel free to give anything useful to build your notification. For instance, in our internal job notifications, we directly send the JobExecution to the create method of our Notification Factories.

  1. Type (string)

    Type of the notification. Can be success, warning or error.

  2. Message (string)

    The message to display in the notification. It can be a simple string, or a translation key, eg. pim_import_export.notification.export.success

  3. MessageParams (array)

    The message parameters to give to the translation key, if any. Eg. ['%label%' => 'Product export'] If provided, it will be passed to the message when translating it.

  4. Route (string)

    The route the user will be redirected to if the notification is clicked.

  5. RouteParams (array)

    The parameters to that given route. For example, the route of the show export profile page is:

    pim_importexport_export_profile_show:
        path: /{id}
        defaults: { _controller: pim_import_export.controller.export_profile:showAction }
        requirements:
            id: \d+
    

    The optional route parameter will be:

    $route = 'pim_importexport_export_execution_show';
    $routeParams = ['id' => $jobExecutionId];
    
  6. Context (array)
    The context allows to store some extra data in the notification, it is not displayed in the UI by default. Some important extra data you may use are:
    • actionType (string): this will be used to guess the icon to display on the notification

    • showReportButton (bool): to hide/display the “report” label on the notification

For example, the create method of the NotificationFactory for mass edit notifications looks like that:

public function create($jobExecution)
{
    $notification = new Notification();
    $type = $jobExecution->getJobInstance()->getType();
    $status = $this->getJobStatus($jobExecution);

    $notification
        ->setType($status)
        ->setMessage(sprintf('pim_mass_edit.notification.%s.%s', $type, $status))
        ->setMessageParams(['%label%' => $jobExecution->getJobInstance()->getLabel()])
        ->setRoute('pim_enrich_job_tracker_show')
        ->setRouteParams(['id' => $jobExecution->getId()])
        ->setContext(['actionType' => $type]);

    return $notification;
}

Well, now we created our very own Notification Factory, we need to register it with the proper tag:

services:
    acme_custom.notification.factory.custom_notification_factory:
        class: 'Acme\CustomBundle\Notification\CustomNotificationFactory'
        arguments:
            - ['my_custom_notification_name']
        tags:
            - { name: pim_notification.factory.notification }

With this tag we will be able to get our new factory from the dedicated registry.

Inject Services¶

Now that our Notification Factory is created and registered, we can build our own notifications!

Warning

The Notification Factory we just created is not responsible for sending notifications, only to build them. To send notification, we need to call the Notifier.

To send our notifications, we’ll need to:
  1. Retrieve our factory with the notification factory registry (@pim_notification.registry.factory.notification)

  2. Build the notification

  3. Give it to the Notifier (@pim_notification.notifier) to actually notify users

So we’ll need 2 services:

services:
    acme_custom.event_subscriber.custom_subscriber:
        class: 'Acme\CustomBundle\EventSubscriber\CustomEventSubscriber'
        arguments:
            - '@pim_notification.registry.factory.notification'
            - '@pim_notification.notifier'
        tags:
            - { name: kernel.event_subscriber }

Here, we inject services in an event subscriber, but we can inject them wherever we have an action which notifies a user. Then, let’s add it to our constructor as follows:

# /src/Acme/Bundle/CustomBundle/EventSubscriber/CustomEventSubscriber.php

/** @var NotificationFactoryRegistry */
protected $factoryRegistry;

/** @var Notifier */
protected $notifier;

/**
 * @param NotificationFactoryRegistry $factoryRegistry
 * @param Notifier                    $notifier
 */
public function __construct(NotificationFactoryRegistry $factoryRegistry, Notifier $notifier)
{
    $this->factoryRegistry = $factoryRegistry;
    $this->notifier        = $notifier;
}

Notify Users¶

Now everything is plugged together, let’s send some notifications!

// 1) retrieve our factory with the notification factory registry
$factory = $this->factoryRegistry->get('my_custom_notification_name');

// 2) build the notification
$notification = $factory->create($status, $message, $messageParams, $route, $routeParams, $type);

// 3) give it to the Notifier to actually notify users
$this->notifier->notify(
    $notification,
    [$user1, $user2] // An array of users (UserInterface or just the username)
);

Found a typo or a hole in the documentation and feel like contributing?
Join us on Github!