liliom / unifonic-notification-channel
v3.0.0
2021-06-01 18:38 UTC
Requires
- php: ^7.4|^8.0
- multicaret/laravel-unifonic: ^2.0
Requires (Dev)
- illuminate/notifications: ~v8.35.1
- mockery/mockery: ^1.0
- phpunit/phpunit: ^9.5.0
README
Unifonic 通知通道用于 Laravel 8.x+
Unifonic 通道使得发送 Laravel 通知作为短信成为可能。
安装
您可以通过 composer 安装此包。
composer require multicaret/unifonic-notification-channel
服务提供者将自动加载。
设置 Unifonic 服务
查看 Laravel Unifonic 库 的配置。
使用方法
要使用此包,您需要在 Laravel 应用程序中创建一个通知类,例如以下示例中的 InvoicePaid
。请确保查看 Laravel 文档 了解此过程。
通知示例
<?php namespace App\Notifications; use Illuminate\Notifications\Notification; use Multicaret\Notifications\Messages\UnifonicMessage; class InvoicePaid extends Notification { private $message; /** * Create a new notification instance. * * @param $message */ public function __construct($message) { $this->message = $message; } /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return [ 'unifonic', ]; } /** * Get the text message representation of the notification * * @param mixed $notifiable * * @return UnifonicMessage */ public function toUnifonic($notifiable) { return (new UnifonicMessage()) ->content($this->message); } }
或直接传递您想要的内容到构造函数中
public function toUnifonic($notifiable) { return new UnifonicMessage('Laravel notifications are awesome!'); }
发送通知
在 Laravel 中发送通知有两种方法
方法 1
使用任何使用 Notifiable
特性的模型的 notify()
函数。为了实现这一点,您需要提供用作接收者号码的适当列,如下例所示,此函数位于 User 模型中(您可能希望将其写入不同的模型中,只需确保使用 use Notifiable
即可)
/** * Route notifications for the Unifonic channel. * * @param \Illuminate\Notifications\Notification $notification * * @return string */ public function routeNotificationForUnifonic($notification) { return $this->phone; // where phone is the column within your, let's say, users table. }
方法 2
使用 Notification 类的静态函数 route()
。
Notification::route('unifonic', 'xxxxx') ->notify( new \App\Notifications\InvoicePaid('Laravel notifications are awesome!') ); // where xxxxx is the phone number you want to sent to, // i.e: 1xxxxxxx - NO NEED for _00_ or _+_
贡献
查看 CONTRIBUTING 指南。
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。