rich2k / pusher-beams
Pusher Beams 是 Pusher 提供的推送通知服务。
v1.0.18
2024-07-23 09:50 UTC
Requires
- php: >=7.1
- illuminate/events: ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
- illuminate/notifications: ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
- illuminate/queue: ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
- illuminate/support: ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
- pusher/pusher-push-notifications: ^2.0
Requires (Dev)
- mockery/mockery: ^0.9.5
- phpunit/phpunit: 7.*
README
此包使您能够轻松使用 Laravel (应适用于其他非 Laravel PHP 项目) 发送 Pusher 推送通知。它基于 Mohamed Said 的此包。
此分支存在是为了让我们能够同时运行 Pusher Beams 和旧的 Pusher Channels 代码。
内容
安装
您可以通过 composer 安装此包。
composer require rich2k/pusher-beams
您必须安装服务提供者。
// config/app.php 'providers' => [ ... Rich2k\PusherBeams\PusherBeamsServiceProvider::class, ],
设置您的 Pusher 账户
在开始使用此包之前,您应该设置一个 Pusher 账户。以下是所需的步骤。
- 登录到https://dash.pusher.com/
- 从侧边栏中选择 Beams,然后从右侧点击 创建 以创建您的实例。
- 转到设置选项卡(您可以关闭向导)
- 上传您的 iOS .p8 认证密钥(他们会指导您完成此操作),您的 iOS Team Id 和/或您的 CM 服务器密钥。
- 现在选择 凭据 选项卡。
- 复制您的 instanceId 和 SecretKey。
- 更新您的
config/broadcasting.php
文件中 pusher 连接下的值,如下所示。 - 现在您已经准备好开始了。
配置
在 config/broadcasting.php
'connections' => [ ... 'pusher' => [ 'beams' => [ 'secret_key' => env('PUSHER_BEAMS_SECRET'), 'instance_id' => env('PUSHER_BEAMS_INSTANCE_ID'), ], ], ],
用法
现在您可以在 Notification
类的 via()
方法中使用该频道。
use Rich2k\PusherBeams\PusherBeams; use Rich2k\PusherBeams\PusherBeamsMessage; use Illuminate\Notifications\Notification; class AccountApproved extends Notification { public function via($notifiable) { return [PusherBeams::class]; } public function toPusherBeamsNotification($notifiable) { return PusherBeamsMessage::create() ->iOS() ->badge(1) ->sound('success') ->body("Your {$notifiable->service} account was approved!"); } }
可用的消息方法
platform('')
: 接受iOS
或Android
的字符串值。iOS()
: 将平台值设置为 iOS。android()
: 将平台值设置为 Android。title('')
: 接受用于标题的字符串值。body('')
: 接受用于正文的字符串值。sound('')
: 接受用于通知声音文件的字符串值。注意,如果您留空,默认声音值将是default
。icon('')
: 接受用于图标文件的字符串值。(仅限 Android)badge(1)
: 接受用于徽章的整数值。(仅限 iOS)setOption($key, $value)
: 允许您在消息有效载荷中设置任何值。有关更多信息,请在此处查看 iOS,或在此处查看 Android。withiOS(PusherBeamsMessage $message)
: 设置要发送到 iOS 的额外消息withAndroid(PusherBeamsMessage $message)
: 设置要发送到 Android 的额外消息
向多个平台发送消息
您可以使用 withiOS()
和 withAndroid()
方法同时向 iOS 设备和 Android 设备发送单个消息。
use Rich2k\PusherBeams\PusherBeams; use Rich2k\PusherBeams\PusherBeamsMessage; use Illuminate\Notifications\Notification; class AccountApproved extends Notification { public function via($notifiable) { return [PusherBeams::class]; } public function toPusherBeamsNotification($notifiable) { return PusherBeamsMessage::create() ->android() ->sound('success') ->body("Your {$notifiable->service} account was approved!") ->withiOS(PusherBeamsMessage::create() ->body("Your {$notifiable->service} account was approved!") ->badge(1) ->sound('success') ); } }
路由消息
默认情况下,Pusher Beams 的 "interest" 消息将使用 {notifiable}.{id} 约定定义,例如 App.User.1
,但是您可以通过在可通知类的方法中包含 routeNotificationForPusherPushNotifications()
来更改此行为,该方法返回兴趣名称。
在应用中设置的任何兴趣都应在您的移动端注册。
测试
$ composer test
安全性
如果您发现任何与安全相关的问题,请通过电子邮件 rhyland@gmail.com 报告,而不是使用问题跟踪器。
贡献
有关详细信息,请参阅 CONTRIBUTING。
致谢
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件。