nextsms/laravel

此包简化了使用 Laravel 通过 NextSMS 发送通知的过程

v0.0.7 2022-05-15 23:32 UTC

This package is auto-updated.

Last update: 2024-09-16 04:47:09 UTC


README

Latest Version on Packagist Software License PHPUnit tests

此包简化了使用 Laravel 和 NextSMS 发送通知的过程。

内容

关于

NextSMS 频道使得能够使用 NextSMS API 将 Laravel 通知发送为 SMS

Laravel 兼容性

包与 Laravel v8 兼容

安装

您可以通过 composer 安装此包

这将安装最新版本

composer require nextsms/laravel

要安装先前版本的包,例如:与 Laravel v6 兼容的版本 6.0.x-dev,请参阅

composer require nextsms/laravel:6.0.x-dev

服务提供者会自动加载。

设置 NextSMS 服务

您需要 注册

请记住添加您将用于发送消息的发送者 ID。

NEXTSMS_USERNAME=""
NEXTSMS_PASSWORD=""
NEXTSMS_FROM=""
NEXTSMS_ENVIROMENT="production"

您可以将包配置文件发布

php artisan vendor:publish --provider="NotificationChannels\NextSms\NextSmsServiceProvider" --tag="config"

在您的通知模型上添加 routeNotificationForNextSms 方法。如果未添加,将自动使用 phone_number 字段。

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * Route notifications for the NextSMS channel.
     *
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return string
     */
    public function routeNotificationForNextSms($notification)
    {
        return $this->phone;
    }
}

使用

要使用此包,您需要在 Laravel 应用程序中创建一个通知类,例如以下示例中的 NewsWasPublished。请确保查看 Laravel 的文档 了解此过程。

<?php

use NotificationChannels\NextSms\NextSmsChannel;
use NotificationChannels\NextSms\NextSmsMessage;

class NewsWasPublished extends Notification
{

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [NextSmsChannel::class];
    }

    public function toNextSms($notifiable)
    {
        return (new NextSmsMessage())
                    ->content('Your SMS message content');

    }
}

替代使用方法

此包附带一个 Facade,用于携带 NextSmsService,如果您只想发送 SMS 而不处理 Laravel 通知系统,这非常有用。

查看完整的 API 列表

// import this 

use NotificationChannels\NextSms\NextSmsFacade as NextSms;

// in your controller
NextSms::singleDestination([
    'from' => 'NEXTSMS',
    'to' => '255716718040',
    'text' => 'Your message'
]);

测试

composer test

安全

如果您发现任何与安全相关的问题,请通过电子邮件 alphaolomi@gmail.com 联系,而不是使用问题跟踪器。

贡献

有关详细信息,请参阅 CONTRIBUTING

致谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件

如何表示感谢?

留下 ⭐ 星标,并在 Twitter 上关注我。