gfs/notifications

Symfony GFSNotificationsBundle

v1.0.2 2016-05-05 14:09 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:53:07 UTC


README

通过 composer 安装

composer require gfs/notifications ~v1.0

添加到 AppKernel.php

new GFS\NotificationBundle\NotificationBundle(),

2. 创建通知

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.
      ];
  }
}

函数 jsonSerialize 应返回一个必须包含 'userId' 字段的数组,服务器会检查所有连接是否包含此值。

3. 启动服务器

Symfony 2 php app/console server:notification

Symfony 3 php bin/console server:notification

4. 客户端任务

使用任何你想要的 WebSocket 或其他技术。最重要的是 URL,它必须包含 GET 参数 userId。该参数将绑定到当前连接。此 userId 在创建通知时使用,来自函数 jsonSerialize 的参数 userId。GET 参数 userId 应与函数 jsonSerialize 返回的参数相同,以便用户收到通知。

var conn = new WebSocket('ws://yourip:8080?userId='+$scope.username);
conn.onopen = function(e) {
    console.log("Connection established!");
};

conn.onmessage = function(e) {
    var noitification = JSON.parse(e.data);
    // handle your notification object here
};

5. 创建通知

创建一个实体:Notification。成功将其插入数据库后,通知将被发送到服务器,服务器将找到匹配 jsonSerializeuserId 字段的连接。

 $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();

配置

#config.yml
gfs_notifications:
    host: localhost # ip or DNS where server run default is localhost
    port: 8080 # port when server want run default is 8080
    notification: GFS\NotificationBundle\Notification\Notification # you can implement your own Ratchet\MessageComponentInterface

默认实体字段

  • id
  • 类型 ( 字符串,255 )
  • 描述 ( 文本 )
  • created_at ( DateTime )
  • checked_at ( DateTime, 默认 null )
  • checked ( 布尔 )
  • user ( Symfony\Component\Security\Core\User\UserInterface 实例 )

如果你想要重写 __construct,不要忘记写

$this->created_at = new \DateTime('now').