laravel-wechat-notification maintained by if1024
Laravel框架下基于 laravel-wechat 使用微信公众号模板消息作为notification通道的composer包。
安装
$ composer require if1024/laravel-wechat-notification -vvv
使用
例子
<?php
namespace App\Notifications;
use If1024\LaravelWechatNotification\Messages\OfficialAccountTemplateMessage;
use If1024\LaravelWechatNotification\WechatMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
class TestNotify extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via(mixed $notifiable): array
{
return ['official_account'];
}
public function toOfficialAccount(): OfficialAccountTemplateMessage
{
$data = [
'first' => 'first',
'keyword1' => 'keyword1',
'keyword2' => 'keyword2',
'keyword3' => 'keyword3',
'remark' => 'remark',
];
return WechatMessage::officialAccount()->template('template_id')
->url('https://github.com/')
->data($data);
}
}
支持的 WechatMessage 方法
to(string $openid): 设置模板消息接收人的 openiddata(array $data): 设置模板消息数据template(string $templateID): 设置模板消息的模板 IDminiprogram(string $appid, string $pagepath): 设置点击模板消息后跳转的小程序,选填url(string $url): 设置点击模板消息后跳转 url,选填
在模型里使用Notifiable triat 来快捷发送消息:
public function routeNotificationForOfficialAccount($notification)
{
return $this->openid;//返回当前model内的公众号openid字段
}
然后这样发送模板消息。可参考官方文档:消息通知->发送通知
use App\Notifications\TestNotify;
$user->notify(new TestNotify());
批量发送消息
use Illuminate\Support\Facades\Notification;
use App\Notifications\TestNotify;
$users = \App\Models\User::all();
Notification::send($users, new TestNotify());
说明
- 如果
miniprogram与url同时存在,则优先使用小程序跳转,详情请参考官方文档 data()方法接收一个数组,其key为模板消息中的关键字,value可以为字符串或数组。如果为字符串,则默认颜色为#173177;如果为数组,则第一个参数为显示的数据,第二个参数为字体颜色 可参考颜色设置
感谢
感谢 github 上的各位大哥给的参考!感谢超哥的easywechat
License
MIT