ankurk91/bandwidth-notification-channel

此包已被废弃,不再维护。没有建议的替代包。

用于 Laravel php 框架的带宽 SMS 通知通道。

6.4.0 2024-03-05 07:38 UTC

README

Laravel 带宽通知通道

Packagist GitHub tag License Downloads tests codecov

使用 Laravel php 框架发送 Bandwidth SMS 通知。

安装

您可以通过 composer 安装此包。

composer require "ankurk91/bandwidth-notification-channel"

设置您的 Bandwidth 账户

  • Bandwidth 获取您的账户凭证。
  • 在您的 .env.env.example 文件中添加账户凭证
BANDWIDTH_ACCOUNT_ID=
BANDWIDTH_APPLICATION_ID=
BANDWIDTH_API_USERNAME=
BANDWIDTH_API_PASSWORD=
BANDWIDTH_FROM=
BANDWIDTH_DRY_RUN=false

发布配置文件(可选)

您可以将 配置 文件发布到您的项目中。

php artisan vendor:publish --provider="NotificationChannels\Bandwidth\BandwidthServiceProvider" --tag="config"

用法

您可以在 Notification 类的 via() 方法中使用 Bandwidth 通道。

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use NotificationChannels\Bandwidth\BandwidthChannel;
use NotificationChannels\Bandwidth\BandwidthMessage;

class AccountApproved extends Notification implements ShouldQueue
{
    use Queueable;
      
    public function via($notifiable): array
    {
        return [BandwidthChannel::class];
    }
  
    public function toBandwidth($notifiable): BandwidthMessage
    {
        return BandwidthMessage::create()
            ->content("Hi {$notifiable->name}, Your account is approved!");
    }
}

routeNotificationForBandwidth 方法添加到您的 Notifiable 模型中。

<?php

namespace App\Models;

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

class User extends Authenticatable
{
    use Notifiable;
      
    public function routeNotificationForBandwidth($notification)
    {
        return $this->phone_number;
    }
}

BandwidthMessage 类上可用的方法

  • content() - 接受一个字符串值作为通知正文。(必需)
  • from() - 接受一个用于通知发送者的电话号码。
  • media() - 接受一个 URL 或 URL 数组,用于 MMS。
  • httpBody() - 接受一个 array,与通知 http 有效负载一起发送。
<?php
use NotificationChannels\Bandwidth\BandwidthMessage;

BandwidthMessage::create()
            ->content("This is sample text message.")
            ->from('+19195551212')
            ->media([
                'https://example.com/a-public-image.jpg',
                'https://example.com/a-public-audio.mp3',
            ])
            ->httpBody([
                'tag' => 'info'         
            ]);

事件

  • 该包利用 Laravel 内置的通知 事件
  • 您可以在项目的 EventServiceProvider 中监听这些事件,例如
<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        \Illuminate\Notifications\Events\NotificationSent::class => [
           // \App\Listeners\BandwidthNotificationSent::class,
        ],
        \Illuminate\Notifications\Events\NotificationFailed::class => [
            \App\Listeners\BandwidthNotificationFailed::class,
        ],
    ];   
}

以下是失败事件监听器类的示例

<?php

namespace App\Listeners;

use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use NotificationChannels\Bandwidth\BandwidthChannel;
use Illuminate\Notifications\Events\NotificationFailed;

class BandwidthNotificationFailed implements ShouldQueue
{
    public function handle(NotificationFailed $event)
    {
        if ($event->channel !== BandwidthChannel::class) {
            return;
        }

        /** @var User $user */
        $user = $event->notifiable;
        
        // todo Do something with $user
    }
}

按需通知

您还可以使用 Laravel 的 on-demand 通知向号码发送推送通知。

use Illuminate\Support\Facades\Notification;
use App\Notification\ExampleSMSNotification;

Notification::route('Bandwidth', '+1234567890')   
    ->notify(new ExampleSMSNotification());

注意(摘自 API 文档)

  • fromto 号码必须以 E.164 格式表示,例如 +19195551212
  • 消息内容长度必须为 2048 个字符或更少。
  • 超过 160 个字符的消息将被自动分割并重新组装,以适应 160 个字符的传输约束。

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

测试

composer test

安全

如果您发现任何安全问题,请通过电子邮件 pro.ankurk1[at]gmail[dot]com 联系我们,而不是使用问题跟踪器。

资源

许可

此包根据 MIT 许可证 许可。