bentools / pusher
PHP 异步推送通知管理
v0.4
2017-02-08 15:55 UTC
Requires
- php: >=7.1
- guzzlehttp/guzzle: ~6.0
- minishlink/web-push: v1.1
This package is auto-updated.
Last update: 2024-09-15 00:44:00 UTC
README
允许通过WebPushAPI发送消息,支持不同提供商(Google Chrome,Mozilla),异步地向多个收件人发送,使用多个API密钥。
看起来很稳定,但仍然是实验性的。
Pusher受minishlink/web-push库的启发,并基于不同的方法实现。
- 为了发送消息,首选Guzzle 6,因为它支持异步和并行请求管理。
- Push对象是一个包含Message和与相应handler关联的Recipients的包。
- Handler负责将Message正确地发送给Recipient - 目前实现:GCM处理器,Mozilla处理器。
- 可以为单个Push使用多个Handlers,并且一个Handler可以有多个实例(例如,当您使用多个GCM API密钥时)。
- 每个Handler都必须能够返回一个Promise以处理Push,以实现异步处理并允许批量处理。
- Pusher服务负责发送Push并更改其状态(pending => done)。
- 当Push完成时,它可以告诉哪些Recipients没有收到消息以及原因(然后您可以取消订阅它们)。
实现了多种类型的Message。
- Ping消息是不带有效载荷的消息。通常您的service worker应该在那时获取有效载荷(就像在早期的Webpush API实现中那样)。
- Notification消息包含一个json,其中包含显示Webpush通知的所有信息(标题、正文、图标等)。
- ServerMessage包含一个JSON,应由您的service worker处理,以便将其发送到活动窗口而不是显示通知。这可能有助于远程更改打开页面的DOM。
提供了一些示例JS文件。
安装
composer require bentools/pusher
示例用法
考虑以下subscription对象
{ "endpoint": "https://updates.push.services.mozilla.com/wpush/v1/gAAAAABYmwfiuCps0P3TPZXSNc8aWol6_2Nqu0VVY6lpJ_xsIrtC8YyfPz_XnobR_Wh2PezdDZFonsfoezNsXykv4", "keys": { "auth": "5coZoiZAodiBZHCkWX5LoAbA", "p256dh": "BHI7P_CAsz3knooINFZZPFONPYTRTzEacYpOx4-hVigOuWjzkRWdkTZmmrAI3U11_z-lU" } }
use BenTools\Pusher\Model\Message\Notification; use BenTools\Pusher\Model\Push\Push; use BenTools\Pusher\Model\Handler\MozillaHandler; use BenTools\Pusher\Model\Recipient\Recipient; use BenTools\Pusher\Pusher; use GuzzleHttp\Client as GuzzleClient; require_once __DIR__ . '/vendor/autoload.php'; $recipient = Recipient::unwrapJSON($json); $guzzle = new GuzzleClient(); $mozilla = new MozillaHandler($guzzle); $pusher = new Pusher(); $push = new Push(); $push->addRecipient($recipient, $mozilla); $message = new Notification( 'Sounds interesting', // title 'Seems to be working :)', // body 'https://pbs.twimg.com/profile_images/555076551818354689/F26py9T__reasonably_small.png', // icon 'https://github.com/bpolaszek/bentools-pusher', // link 'hello world' // tag ); $message->setTTL(60); $push->setMessage($message); $pusher->push($push); if ($push->hasErrors()) { foreach ($push->getFailedRecipients() AS $recipient) { echo $push->getFailureReason($recipient); // Remove recipient from database or set it inactive } }
待办事项
- 实现VAPID身份验证
- Google FCM处理器
- 测试
- 食谱
许可
MIT