• v2.2
    • Versions
    • master
    • v3.2
    • v3.1
    • v3.0
    • v2.3
    • v2.2
    • v2.1
    • v2.0
    • v1.7
    • v1.6
    • v1.5
    • v1.4
    • v1.3
    • v1.2
    • v1.1
    • v1.0
Caution! You are browsing the documentation for Akeneo in version 2.2, which is not maintained anymore.
Consider upgrading to the latest version.

 

  • Install Akeneo PIM
    • Install Akeneo PIM with Docker
    • Install Akeneo PIM manually
      • System Requirements
      • System installation on Debian 9 (Stretch)
      • System installation on Ubuntu 16.04 (Xenial Xerus)
      • Installing Akeneo PIM Community Edition (CE) with the Archive
      • Installing Akeneo PIM Enterprise Edition (EE) with the Archive
      • Setting up the job queue daemon
    • How to customize the Dataset
    • How to Add Translation Packs
  • Migrate Akeneo PIM projects
  • 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 Avoid Rules Execution on Mass Edit Actions
    • 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 add a custom unit of measure
      • How to Add New Properties to a Category
    • How to Customize Product Assets
      • How to Add a New Transformation
      • How to Add a Default Thumbnail For Unknown File Types
      • How to connect to an external server for storage
      • How to change the validation rule to match a reference file to an asset
      • How to Mass Import Assets
    • 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
  • Design the user interfaces
    • How to customize any frontend part of the application
    • How to add an action button or meta data to the product edit form
    • How to add a tab to a form
    • How to add a new tab in System / Configuration
    • How to add custom information to a field
    • How to add a new field type
    • Create a custom product export builder filter
    • How to create the UI to manage a Reference Data
    • How to add a new page
    • How to customize the main menu
    • Styleguide
  • 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 behavior tests are architectured in the PIM?
      • Establishing Decorator Pattern
      • Using Spin In Behat
  • 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
      • Setup Behat
      • Code Conventions
      • Coding Standards
    • Bundles
    • Components
    • 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?
    • 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 Mode
      • Overview
      • Partners Starterkit
      • Environments Access
      • Composer settings
      • Periodic tasks / Crontab settings
      • PIM Application
      • PIM Updates and Migrations
      • Data File Transfer
      • Backups management
      • Partners
      • Queue management
      • System Components
      • Disk Usage
    • Serenity Mode
      • Overview
      • PIM Updates and Migrations

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 Pim\Bundle\NotificationBundle\Factory\NotificationFactoryInterface.

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

namespace Acme\CustomBundle\Notification;

use Pim\Bundle\NotificationBundle\Entity\Notification
use Pim\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

Note

You can see available notification icons on the styleguide website.

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!