energyweapons / laravel-line-notify
Laravel 7 通知的 LINE Notify 提供商
1.0
2023-03-03 12:21 UTC
Requires
- php: ^8.1
This package is auto-updated.
Last update: 2024-09-30 01:23:53 UTC
README
Laravel 7.x 通知的 LINE Notify 提供商。
要求
- PHP >= 7.2.5
- Laravel 7.x (推荐 7.x)
用法
-
安装包
$ composer require energyweapons/laravel-line-notify
-
将
Energyweapons\LineNotify\LineNotifyServiceProvider
添加到config/app.php
或类似位置。 -
创建可通知类(例如用户模型实体)
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; // ... /** * @return string LINE Notify OAuth2 token */ protected function routeNotificationForLine() { return 'ADD_YOUR_ACCESS_TOKEN_HERE'; } }
-
创建通知
<?php namespace App\Notifications; use App\User; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Notification; use Hinaloe\LineNotify\Message\LineMessage; class NewUserNotification extends Notification// implements ShouldQueue { use Queueable; /** @var User */ protected $user; /** * Create a new notification instance. * * @param User $user */ public function __construct(User $user) { $this->user = $user; } /** * Get the notification's delivery channels. * * @param mixed $notifiable * * @return array */ public function via($notifiable) { return ['line']; } /** * @param mixed $notifable callee instance * @return LineMessage */ public function toLine($notifable) { return (new LineMessage())->message('New user: ' . $this->user->name) ->imageUrl('https://example.com/sample.jpg') // With image url (jpeg only) ->imageFile('/path/to/image.png') // With image file (png/jpg/gif will convert to jpg) ->sticker(40, 2); // With Sticker } }
-
调用
$notifable->notify()
$user = User::find(114514); $user->notify(new NewUserNotification($user));