alhoqbani/laravel-mobily-ws-notification

使用 Laravel 5.4 通过 mobily.ws 发送短信通知

0.1.0 2017-08-30 07:19 UTC

This package is auto-updated.

Last update: 2024-09-26 16:49:04 UTC


README

Latest Version on Packagist Software License Build Status Code Coverage Total Downloads

此包使您能够使用 Laravel 5.4 通过 MobilyWs 发送通知变得简单。

内容

安装

包安装

使用 composer 安装包

composer require alhoqbani/laravel-mobily-ws-notification

将服务提供者添加到 config/app.php 中的提供者数组

对于 Laravel 5.5+,您无需执行此步骤

        NotificationChannels\MobilyWs\MobilyWsServiceProvider::class,

发布配置文件

php artisan vendor:publish --provider="NotificationChannels\MobilyWs\MobilyWsServiceProvider"

设置 mobily.ws 账户

您必须拥有 MobilyWs 账户才能使用此包。

此包与 mobily.ws 完全无关。

凭证。

使用 mobily.ws API 时有两种认证方式。

您可以使用您的登录凭据(手机/密码)发送请求,或者使用从您的 mobily.ws 账户生成的 apiKey。

您必须将 mobily.ws 凭据添加到您的 .env 文件中。

# Mobile number and password used for log in.
MOBILY_WS_MOBILE= 
MOBILY_WS_PASSWORD=
# or your apiKey:
MOBILY_WS_API_KEY=
# name/number of the sender which must be approved by mobily.ws for GCC
MOBILY_WS_SENDER=
使用哪种方法

您可以通过编辑您的 config/mobilyws 文件来定义您想要使用的认证方法。

您可以选择:authpasswordauto

如果您选择 auto,我们将首先查找 apiKey 键,如果找不到,我们将查找手机和密码

// config/mobilyws

    // Authentication mode
    'authentication' => 'auto',
    
    // Set yor login credentials to communicate with mobily.ws Api
    'mobile' => env('MOBILY_WS_MOBILE'),
    'password' =>  env('MOBILY_WS_PASSWORD'),
    
    // Or use the generated apiKey from your mobily.ws account
    'apiKey' => env('MOBILY_WS_API_KEY'),
    
    // Name of Sender must be approved by mobily.ws
    'sender' => env('MOBILY_WS_SENDER'),

用法

创建新通知

使用 Laravel artisan 创建一个新的通知类

php artisan make:notification UserRegistered

并配置通知类以使用 MobilyWsChannel。

或者,您可以使用我们的自定义 artisan 命令

php artisan mobilyws:notification UserRegistered

toMobilyWs 方法应返回要发送的文本消息的字符串或 MobilyWsMessage 实例。

有关更多信息,请参阅 可用的消息方法

<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use NotificationChannels\MobilyWs\MobilyWsChannel;
use NotificationChannels\MobilyWs\MobilyWsMessage;

class UserRegistered extends Notification
{
    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [MobilyWsChannel::class];
    }
    
    /**
     * Get the text message representation of the notification
     *
     * @param  mixed      $notifiable
     * @param \NotificationChannels\MobilyWs\MobilyWsMessage $msg
     *
     * @return \NotificationChannels\MobilyWs\MobilyWsMessage|string
     */
    public function toMobilyWs($notifiable, MobilyWsMessage $msg)
    {
        return "Dear $notifiable->name, welcome to our website";
    }
}

路由短信通知

当通过 MobilyWs 通道发送通知时,通知系统会自动在可通知实体上查找 phone_number 属性。如果您想要自定义通知送达的电话号码,请在实体上定义 routeNotificationForMobilyWs 方法

<?php

    namespace App;

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

    class User extends Authenticatable
    {
        use Notifiable;

        /**
         * Route notifications for the MobilyWs channel.
         *
         * @return string
         */
        public function routeNotificationForMobilyWs()
        {
            return $this->mobile;
        }
    }

routeNotificationForMobilyWs 应返回要将短信发送到的手机号码。

请注意,手机号码必须以国家代码开头,不能有前导零。

例如,9665xxxxxxxx

发送短信

use App\Notifications\UserRegistered;

$user = App\User::first();

$user->notify(new UserRegistered());

计划短信

MobilyWs Api 允许发送计划消息,该消息将在定义的日期/时间发送。

请注意,如果您定义的时间是过去的,消息将由 mobily.ws 立即发送。此库不会检查定义的时间是否在将来。

您可以通过在 MobilyWsMessage 实例上调用 time 方法来定义 mobily.ws 应发送消息的日期和时间。

    public function toMobilyWs($notifiable)
    {
        return (new MobilyWsMessage)
            ->text("Message text")
            ->time(Carbon::parse("+1 week);
    }

time 方法接受 DateTime 对象或时间戳。

可用的消息方法

在您的通知中,您必须定义一个 toMobilyWs 方法,该方法将接收可通知实体(例如 User 模型)和 MobilyWsMessage 实例。

此方法应返回要发送到 mobily.ws 的短信文本或 MobilyWsMessage 实例。

<?php

use NotificationChannels\MobilyWs\MobilyWsMessage;
            //
    /**
     * Get the text message of the SMS.
     *
     * @param  mixed  $notifiable
     * @return \NotificationChannels\MobilyWs\MobilyWsMessage|string
     */
    public function toMobilyWs($notifiable)
    {
        return MobilyWsMessage::create("Text message");
    }

您还可以将消息传递给 MobilyWsMessage 构造函数

返回 new MobilyWsMessage("文本消息");

或使用 msg() 方法设置文本消息

    public function toMobilyWs($notifiable, MobilyWsMessage $msg)
    {
        return $msg->text($this->message);
    }

方法 toMobilyWs 将接收一个 MobilyWsMessage 实例作为第二个参数。

可用方法列表

text() 添加文本消息的内容

time() 设置定时短信的时间。

待办事项

  • 验证手机号码
  • 验证文本消息类型和长度
  • 验证给定时间是否在将来。
  • 验证方法 toMobilyWs 的存在性和配置文件。
  • 添加发送定时短信的选项
  • 添加剩余的参数(MsgID、msgKey、deleteKey、timeSenddateSend
  • 翻译 mobily.ws 错误消息
  • 创建 artisan 命令以生成 mobily.ws 通知
  • 将已触发的事件列表添加到文档中。

变更日志

请参阅 变更日志 了解最近的变化。

测试

$ composer test

贡献

请参阅 贡献指南 了解详细信息。

许可证

MIT 许可协议(MIT)。请参阅 许可文件 了解更多信息。