ab-alselwi/hisms-ws-notification-channel

Hisms.ws 通知通道用于 Laravel。

dev-main / 0.x-dev 2021-10-24 19:55 UTC

This package is auto-updated.

Last update: 2024-09-04 22:09:23 UTC


README

此包使您能够通过 HismsWs 在 Laravel 8.x 中轻松发送通知。

此包受到了以下灵感的启发:

内容

安装

包安装

使用 composer 安装包

composer require ab-alselwi/hisms-ws-notification-channel

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

对于 Laravel 8.0+,您不需要执行此步骤

        NotificationChannels\HismsWs\HismsWsServiceProvider::class,

发布配置文件

php artisan vendor:publish --provider="NotificationChannels\HismsWs\HismsWsServiceProvider"

设置 hisms.ws 账户

您必须拥有一个 HismsWs 账户才能使用此包。

凭证

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

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

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

// Mobile number and password used for log in.
HISMS_WS_MOBILE= 
HISMS_WS_PASSWORD=

// name/number of the sender which must be approved by hisms.ws for GCC
HISMS_WS_SENDER=
使用哪种方法

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

// config/hismsws


    
     // Set yor login credentials to communicate with hisms.ws Api
    'mobile' => env('HISMS_WS_MOBILE'),
    'password' =>  env('HISMS_WS_PASSWORD'),
    
    // Name of Sender must be approved by hisms.ws
    'sender' => env('HISMS_WS_SENDER'),

用法

创建新通知

使用 Laravel artisan 创建新的通知类

php artisan make:notification UserRegistered

并将通知类配置为使用 HismsWsChannel。

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

<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use NotificationChannels\HismsWs\HismsWsChannel;
use NotificationChannels\HismsWs\HismsWsSmsMessage;

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

路由 SMS 通知

当通过 HismsWs 通道发送通知时,通知系统会自动在可通知实体上查找 phone_number 属性。如果您想要自定义通知发送到的手机号码,请在实体上定义 routeNotificationForHismsWs 方法

<?php

    namespace App;

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

    class User extends Authenticatable
    {
        use Notifiable;

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

routeNotificationForHismsWs 应返回要发送 SMS 消息的手机号码。

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

例如,9665xxxxxxxx

发送 SMS

use App\Notifications\UserRegistered;

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

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

计划发送 SMS

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

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

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

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

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

测试

$ composer test

许可证

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