laravel 消息通知实现

PS:本次主要记录一下laravel 自带的消息通知Notification的实现

1.生成数据库,可以使用迁移

  • 在项目目录下的cmd中运行 artisan命令
    php artisan notifications:table
    php artisan migrate

  • 在user表里增加一个notification_count 字段,记录未读通知数量
  • php artisan make:notification UserSupport

    如果implements ShouldQueue这个接口的话就会异步队列执行,如果去掉的话就是同步执行。

  • <?php
    
    /**
     * 模拟用户点赞通知
     * laravel 通知表  notifications
     * 点赞表 user_support
     * 作者表 users
     * 用户表 user_info
     */
    
    namespace App\Notifications;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Notifications\Messages\MailMessage;
    use Illuminate\Notifications\Notification;
    
    use App\Models\UserSupport as UserSupportModel;
    
    class UserSupport extends Notification implements ShouldQueue
    {
        use Queueable;
    
        public $support;
       
         public function __construct(UserSupportModel $support)
        {
            // 注入回复实体,方便 toDatabase 方法中的使用
            $this->support = $support;
        }
    
        public function via($notifiable)
        {	
        	// 开启通知的频道
            return ['mail','database'];
        }
    
        public function toMail($notifiable)
        {
            return (new MailMessage)
                        ->line('The introduction to the notification.')
                        ->action('Notification Action', 'www.baidu.com')
                   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Laravel发送阿里短信通知,您需要执行以下步骤: 1. 安装阿里云SDK 您需要通过Composer安装阿里云SDK,可以在终端中执行以下命令: ``` composer require alibabacloud/sdk ``` 2. 设置阿里云AK信息 您需要设置阿里云AK信息,包括Access Key ID和Access Key Secret。您可以在阿里云控制台中创建一个RAM子账户,然后使用子账户的AK信息进行配置。 您可以在Laravel的.env文件中设置以下信息: ``` ALIYUN_ACCESS_KEY_ID=your_access_key_id ALIYUN_ACCESS_KEY_SECRET=your_access_key_secret ``` 3. 创建通知类 创建一个通知类来发送短信通知。您可以在终端中使用以下命令创建一个通知类: ``` php artisan make:notification AliyunSmsNotification ``` 这将创建一个新的通知类,您需要在该类中实现一个toAliyunSms方法来发送短信通知。以下是一个示例实现: ``` use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException; use Illuminate\Notifications\Notification; class AliyunSmsNotification extends Notification { private $mobile; private $templateCode; private $templateParam; public function __construct($mobile, $templateCode, $templateParam) { $this->mobile = $mobile; $this->templateCode = $templateCode; $this->templateParam = $templateParam; } public function via($notifiable) { return ['aliyun']; } public function toAliyunSms($notifiable) { try { AlibabaCloud::accessKeyClient(config('services.aliyun.access_key_id'), config('services.aliyun.access_key_secret')) ->regionId(config('services.aliyun.region_id')) ->asDefaultClient(); $result = AlibabaCloud::rpc() ->product('Dysmsapi') ->version('2017-05-25') ->action('SendSms') ->method('POST') ->host(config('services.aliyun.host')) ->options([ 'query' => [ 'PhoneNumbers' => $this->mobile, 'SignName' => config('services.aliyun.sign_name'), 'TemplateCode' => $this->templateCode, 'TemplateParam' => json_encode($this->templateParam), ], ]) ->request(); return $result->toArray(); } catch (ClientException $e) { // handle exception } catch (ServerException $e) { // handle exception } } } ``` 在此示例中,我们使用AlibabaCloud PHP SDK发送短信通知。您需要将`$mobile`,`$templateCode`和`$templateParam`参数传递给构造函数,然后在`toAliyunSms`方法中使用它们发送短信。 4. 配置通知 您需要在通知的`via`方法中返回一个包含`aliyun`的数组来指定通知通过阿里云发送。在Laravel的配置文件中,您需要定义一个`aliyun`通知通道来指

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值