sepiosky/kavenegar-notifications-channel

此包的最新版本(dev-master)没有提供许可证信息。

Kavenegar 通知通道适用于 Laravel

dev-master 2020-11-28 17:43 UTC

This package is auto-updated.

Last update: 2024-09-25 00:46:53 UTC


README

此包通过 Laravel 使使用 Kavenegar API(包括 SMS webserviceOTP webservice)发送 SMS 通知变得简单。

内容

安装

您可以通过 composer 安装此包

composer require sepiosky/kavenegar-notifications-channel

然后,将您的 Kavenegar API 凭证添加到 config/services.php: (注意,sender 仅用于 SMS webservice(不是 OTP)。它也是可选的)

// config/services.php
'kavenegar' => [
    'key' => env('KAVENEGAR_API_KEY'),
    'sender' => env('KAVENEGAR_SENDER')
],

并在 config/app.php 中注册 Kavenegar 服务提供者(在 Laravel 5.5 及以后版本中,这将是自动完成的)

// config/app.php
'providers' => [
    ...
    NotificationChannels\Kavenegar\KavenegarServiceProvider::class,
    ...
],

使用方法

安装后,您可以通过在 via() 方法中注册 KavenegarChannel 来在您的通知中使用 Kavenegar

纯文本 SMS 通知

KavenegarMessagemethod 属性默认为 sms

use NotificationChannels\Kavenegar\KavenegarChannel;
use NotificationChannels\Kavenegar\KavenegarMessage;
use Illuminate\Notifications\Notification;

class WelcomeMessage extends Notification
{
    public function via($notifiable)
    {
        return [KavenegarChannel::class];
    }

    public function toSMS($notifiable)
    {
        return KavenegarMessage::create()
            ->method('sms')
            //its optional since KavenegarMessage has it deafult method set sms
            ->to($notifiable->phone)
            ->Message('Dear '.$notifiable->username.'! Thanks for joining us');
    }
}

OTP SMS 通知

对于此类通知,您应该在返回 KavenegarMessage 时设置 method('otp')

use NotificationChannels\Kavenegar\KavenegarChannel;
use NotificationChannels\Kavenegar\KavenegarMessage;
use Illuminate\Notifications\Notification;

class VerifyAccount extends Notification
{
    public function via($notifiable)
    {
        return [KavenegarChannel::class];
    }

    public function toSMS($notifiable)
    {
        $url = env('APP_URL');
        return KavenegarMessage::create()
            ->method('otp')->to($notifiable->phone)
            ->token('1425')->template('registerVerifyTemplate')
            ->token2('please')->token10($url);
    }
}

注意,仅需要 tokentemplate(您应该首先在您的 Kavenegar 控制面板上创建和验证您的模板)和其他令牌是可选的。

消息路由

您可以通过在示例中像 to($phone) 方法一样提供接收者的电话号码,或者添加一个 routeNotificationForSms() 方法到您的可通知模型中。

/**
 * Route notifications for the Kavenegar channel.
 *
 * @return int
 */
public function routeNotificationForSms()
{
    return $this->phone_number;
}

可用的消息方法

  • method($method): (string) 消息方法。 (sms/otp)
  • to($phone): (integer) 接收者的电话号码。
  • template($template): (string) 消息的模板名称(OTP 消息必须要求)
  • token($token): (string) 模板的第一令牌(OTP 消息必需)
  • token2($token)token3($token)token10($token)token20($token): (string) OTP 消息的可选令牌
  • message($message): (string) 消息正文(SMS 消息必需)

有关 API 输入格式的详细信息,请参阅 Kavenegar REST API 文档

处理响应

您可以使用 通知事件 来处理来自 Kavenegar 的响应。在成功的情况下,您的事件监听器将接收一个包含各种字段的数组对象,这些字段适用于通知类型。

有关完整响应字段列表,请参阅 Kavenegar REST API 文档