mrad / notifications-bundle
使用 Pusher API 在您的 symfony 项目中实现通知系统
v1.3.0
2019-01-23 09:13 UTC
Requires
- php: >=5.5.9
- pusher/pusher-php-server: ^3.0
- symfony/framework-bundle: ^2.8 || ^3.0
This package is not auto-updated.
Last update: 2024-09-29 05:41:51 UTC
README
这是一个用于 Pusher API 的简单实现,帮助您通过几个简单步骤集成通知系统。
安装
composer require mrad/notifications-bundle
- 在 AppKernel.php 中启用该包
new SBC\NotificationsBundle\NotificationsBundle(),
用法
步骤 1
首先,您需要在 Pusher 上创建一个账户,然后在 您的应用 菜单中创建一个新的应用。
步骤 2
将您的应用配置添加到 app/config.yml
# NotificationsBundle configuration
notifications:
app_id: your_id
app_key: your_key
app_secret: secret
cluster: cluster
# Optional
# Default false: work without ssl encryption
# Set it to true to work with ssl encryption
encrypted: false
步骤 3
现在在您的 view.html.twig
(客户端)中添加以下内容
{# Call NotificationsBundles's assets #}
{{ notifications_assets() }}
<script>
/**
* After calling notifications_assets() "pusher" is now available
* and you can use it this way
*/
// select the channel you want to listen to
var channel = pusher.subscribe("notifications");// notifications channel
channel.bind("my-event", function(data) {
console.log('from notifications channel', data);
});
var channel = pusher.subscribe("messages");// messages channel
channel.bind("my-event", function(data) {
console.log('from messages channel', data);
});
</script>
然后就可以这样了 😃,现在为了确保您的客户端正确接收数据,您可以调用以下控制台命令进行测试
php bin/console notifications:trigger "您的消息" "频道"
如果您打开浏览器控制台,应该会看到类似以下内容
从后端广播自定义消息
要从后端广播消息,您可以简单地这样做
// From your controller or service $data = array( 'my-message' => "My custom message", ); $pusher = $this->get('mrad.pusher.notificaitons'); $channel = 'messages'; $pusher->trigger($data, $channel); // or you can keep the channel pram empty and will be broadcasted on "notifications" channel by default $pusher->trigger($data);
下一步
现在我将展示如何在数据库中创建和保存通知。
许可协议
该项目遵循 MIT 许可协议
视频
您可以查看这个 播放列表 了解更多关于此包如何工作的细节。
感谢
感谢 SlimenTN 在本项目中的帮助