jlorente / laravel-pushy
Laravel 集成 Pushy SDK 包括通知通道
Requires
- php: >=7.1.3
- illuminate/support: >=5.5
- jlorente/pushy-php-sdk: ^1.0.2
This package is auto-updated.
Last update: 2024-08-29 05:28:14 UTC
README
Laravel 集成 Pushy SDK 包括通知通道。
安装
推荐通过 composer 安装此扩展。
安装 Composer 后,可以使用以下命令安装扩展
$ php composer.phar require jlorente/laravel-pushy
或添加
... "require": { "jlorente/laravel-pushy": "*" }
到您的 composer.json
文件的 require
部分。
配置
- 在您的 config/app.php 服务提供者列表中注册 ServiceProvider。
config/app.php
return [ //other stuff 'providers' => [ //other stuff \Jlorente\Laravel\Pushy\PushyServiceProvider::class, ]; ];
- 在 $aliases 部分添加以下外观。
config/app.php
return [ //other stuff 'aliases' => [ //other stuff 'Pushy' => \Jlorente\Laravel\Pushy\Facades\Pushy::class, ]; ];
- 发布包以便将配置文件复制到配置文件夹。
$ php artisan vendor:publish --provider=Jlorente\\Laravel\\Pushy\\PushyServiceProvider
- 在 config/pushy.php 文件中设置 api_key 或使用预定义的 env 变量。
config/pushy.php
return [ 'api_key' => 'YOUR_SECRET_API_KEY', //other configuration ];
或 .env
//other configurations
PUSHY_API_KEY=<YOUR_SECRET_API_KEY>
PUSHY_NOTIFICATION_TTL=<YOUR_CUSTOM_VALUE>
使用
您可以使用外观别名 Pushy 来执行 API 调用。认证参数将被自动注入。
Pushy::api()->deviceInfo($token);
通知通道
此包包含一个通知通道,允许您将 Pushy 发送通知服务与 Laravel 通知集成。
格式化通知
如果您想向 Pushy 发送通知,应在通知类中定义一个 toPushy 方法。此方法将接收一个 $notifiable 实体,并应返回一个 Jlorente\Laravel\Pushy\Notifications\Messages\PushyMessage 实例或一个包含要发送在通知上的负载的数组
/** * Get the PushyMessage that represents the notification. * * @param mixed $notifiable * @return \Jlorente\Laravel\Pushy\Notifications\Messages\PushyMessage|array */ public function toPushy($notifiable) { return (new PushyMessage) ->setData([ 'eventName' => 'my_event_name' ]) ->setTimeToLive(3600); }
完成之后,您必须在通知的 via() 方法的数组中添加通知通道
/** * Get the notification channels. * * @param mixed $notifiable * @return array|string */ public function via($notifiable) { return [PushyChannel::class]; }
有关 Laravel 通知的更多信息,请参阅 此页面。
路由通知
通过 Pushy 通道发送通知时,通知系统将自动在可通知实体上查找 pushy_token 属性。如果您想自定义通知送达的设备的 token,请在实体上定义 routeNotificationForPushy 方法
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /** * Route notifications for the Pushy channel. * * @param \Illuminate\Notifications\Notification $notification * @return string */ public function routeNotificationForPushy($notification) { return $this->device_token; } }
有关 Laravel 通知的更多信息,请参阅 此页面。
许可证
版权所有 © 2019 José Lorente Martín jose.lorente.martin@gmail.com.
根据 BSD 3-Clause 许可证授权。有关详细信息,请参阅 LICENSE.txt。