laravel-notification-channels / pusher-push-notifications
Pusher 原生推送通知驱动程序。
4.2.0
2024-03-14 01:48 UTC
Requires
- php: >=8.0
- illuminate/events: ~8.0 || ~9.0 || ^10.0 || ^11.0
- illuminate/notifications: ~8.0 || ~9.0 || ^10.0 || ^11.0
- illuminate/queue: ~8.0 || ~9.0 || ^10.0 || ^11.0
- illuminate/support: ~8.0 || ~9.0 || ^10.0 || ^11.0
- pusher/pusher-push-notifications: ^2.0
Requires (Dev)
- mockery/mockery: ^1.3
- phpunit/phpunit: ^9.5 || ^10.5
README
此包使您能够通过 Laravel 发送 Pusher Beams 推送通知。
请注意,此通知通道不应与 Pusher Channels 混淆。
此外,请注意,在版本 2.0 之前,此包与 Pusher Channels 的一部分 Pusher 测试推送通知服务集成。有关更多信息,请参阅 Pusher 的 迁移指南。
内容
安装
您可以通过 composer 安装此包。
composer require laravel-notification-channels/pusher-push-notifications
设置您的 Pusher 账户
在开始使用此包之前,您应该设置 Pusher Beams 账户。以下是所需步骤。
- 登录到 https://dash.pusher.com/
- 选择“Beams”产品。
- 从列表中选择您的实例或创建一个新实例。
- 单击“设置”选项卡。
- 上传您的 APNS 证书和/或添加您的 FCM 服务器密钥。
- 现在选择“密钥”选项卡。
- 复制您的
实例 ID
和密钥
。 - 现在在您的
config/services.php
文件中添加一个新条目'pusher' => [ 'beams_instance_id' => 'Your Instance Id', 'beams_secret_key' => 'Your Secret Key', ],
- 现在您可以开始了。
用法
现在您可以在通知类中的 via()
方法中使用该通道。
use NotificationChannels\PusherPushNotifications\PusherChannel; use NotificationChannels\PusherPushNotifications\PusherMessage; use Illuminate\Notifications\Notification; class AccountApproved extends Notification { public function via($notifiable) { return [PusherChannel::class]; } public function toPushNotification($notifiable) { return PusherMessage::create() ->iOS() ->badge(1) ->sound('success') ->body("Your {$notifiable->service} account was approved!"); } }
可用的消息方法
platform('')
:接受 iOS、Android 或 web 的字符串值。iOS()
:将平台值设置为 iOS。android()
:将平台值设置为 Android。web()
:将平台值设置为 web。link()
:接受一个字符串值,该值将导向通知点击时指定的 URI。title('')
:接受一个字符串值作为标题。body('')
:接受一个字符串值作为正文。sound('')
:接受一个字符串值作为通知声音文件。请注意,如果您留空,则默认声音值为default
。icon('')
:接受一个字符串值作为图标文件。(仅限 Android)badge(1)
:接受一个整数值作为徽章。(仅限 iOS)setOption($key, $value)
:允许您在消息有效载荷中设置任何值。有关更多信息,请参阅 Pusher Beam 文档中的请求体部分。
发送到多个平台
您可以使用 withiOS()
和 withAndroid()
方法同时向 iOS 设备和 Android 设备发送单个消息
public function toPushNotification($notifiable) { $message = "Your {$notifiable->service} account was approved!"; return PusherMessage::create() ->iOS() ->badge(1) ->body($message) ->withAndroid( PusherMessage::create() ->title($message) ->icon('icon') ); }
- 请注意,iOS 是默认平台,这意味着您不需要调用
->iOS()
。- 当使用
withAndroid()
、withiOS()
或withWeb()
时,您不需要定义平台,这将在幕后为您完成。
路由消息
默认情况下,将使用 {notifiable}.{id} 约定来定义将发送的 Pusher "兴趣" 消息,例如 App.User.1
,但是您可以通过在可通知类中包含 routeNotificationFor()
来更改此行为。
即,如果您正在对 用户
模型推送通知,您可以进入 App\Models\User
类并实现方法
public function routeNotificationForPusherPushNotifications($notification): string { return 'your.custom.interest.string'; }
PusherPushNotifications()
方法在可通知类中返回兴趣名称。
发布给用户
您可以使用与发布到兴趣相同的方式发布给用户,但必须在可通知模型中添加以下变量
class Client extends Model
{
use Notifiable;
public $pushNotificationType = 'users';
}
变更日志
有关最近更改的更多信息,请参阅 变更日志
测试
$ composer test
安全
如果您发现任何安全相关的问题,请发送电子邮件至 themsaid@gmail.com,而不是使用问题跟踪器。
贡献
有关详细信息,请参阅 贡献指南
鸣谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件