PHP code example of gfs / notifications

1. Go to this page and download the library: Download gfs/notifications library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gfs / notifications example snippets


new GFS\NotificationBundle\NotificationBundle(),

use GFS\NotificationBundle\Entity\Notification as Base;
class Notifications extends Base
{
    /**
    * @var int
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;

  /**
   * @var User
   *
   * @ORM\ManyToOne(targetEntity="User",inversedBy="notifications")
   * @ORM\JoinColumn(name="user_id", onDelete="cascade")
   */
  private $user;

  /**
   * @param UserInterface $user
   *
   * @return $this
   */
  public function setUser(UserInterface $user)
  {
      $this->user = $user;

      return $this;
  }

  /**
   * @return User
   */
  public function getUser()
  {
      return $this->user;
  }

  /**
   * Get id
   *
   * @return int
   */
  public function getId()
  {
      return $this->id;
  }

  /**
   * This is important because the server will send your JSON notifications ( json_encode(your notification) ).
   * You can customize what field you want the server to send to your client.
   */
  public function jsonSerialize()
  {
      return [
          'type' => $this->getType(),
          'description' => $this->getDescription(),
          'checked' => $this->getChecked(),
          'checkedAt' => $this->getCheckedAt(),
          'createdAt' => $this->getCreateAt(),
          'url' => $this->url,
          'id' => $this->id,
          'userId' => $this->user->getUsername() //This helps the server identify if a specific user is connected and only send notifications to that user. You can use another field for common notifications, for example group notifications.
      ];
  }
}

 $notification = new Notifications();
 $notification->setType('type')
     ->setDescription('Your Notification here')
     ->setUser($user)
 ;
 $this->get('doctrine.orm.default_entity_manager')->persist($notification);
 $this->get('doctrine.orm.default_entity_manager')->flush();