自己造了一个laravel框架下基于laravel-wechat使用微信消息通知作为notification通道的composer包,使用前需先熟读laravel-消息通知
目前已支持:
- 公众号模板消息
- 小程序模板消息
- 开放平台公众号模板消息
- 开放平台小程序模板消息
- 企业微信消息
- 企业微信开放平台消息
使用方式:
0. 添加composer包:
1
| composer require "huo-zi/laravel-wechat-notification"
|
1. 在继承了notification
的通知类中可以使用如下方法:
1 2 3 4 5 6 7 8 9 10 11 12 13
| public function via($notifiable) {
return ['official_account', 'mini_program']; }
|
1 2 3 4
| public function toOfficialAccount() { return WechatMessage::officialAccount('app_name')->template('templateId')->url($url)->data(['fisrt'=>'...']); }
|
1 2 3 4 5 6 7
| public function toMiniProgram() { return WechatMessage::miniProgram('app_name')->template($templateId)->formId($formId)->data([ 'first' => '' // ]); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public function toOpenOfficialAccount() { return WechatMessage::openFlatform($name)->officateAccount($appId, $refreshToken, $accessToken)->template('templateId'); }
public function toOpenOfficialAccount() { return WechatMessage::openFlatform($name)->officateAccount(function ($open) { return $open->officateAccount($appId, $refreshToken, $accessToken); })->template('templateId'); }
public function toOpenOfficialAccount() { return (new WechatPlatform($openPlatform))->officateAccount($appId, $refreshToken, $accessToken)->template($templateId); }
|
1 2 3 4
| public function toOpenMiniProgram() { return WechatMessage::openFlatform($name)->miniProgram($appId, $refreshToken, $accessToken)->template($templateId); }
|
1 2 3 4
| public function toWork() { return WechatMessage::work($name)->message($message)->ofAgent($agentId); }
|
1 2 3 4 5 6 7 8 9
| public function toOpenWork() { return WechatMessage::openWork($name)->work($authCorpId, $permanentCode)->message($message)->ofAgent($agentId); $messenger = (new WechatOpenWork($openWork))->work(function($openWork) { return $openWork->work($authCorpId, $permanentCode); }); return $messenger->message($message)->ofAgent($agentid); }
|
2. 在使用了triat
Notifiable
的模型里增加获取对应openid/userid
的方法:
可以参考官方文档里发送邮件通知时自定义收件人
1 2 3 4 5
| public function routeNotificationForOfficialAccount($notification) { return $this->openid; }
|
1 2 3 4
| public function routeNotificationForMiniProgram($notification) { }
|
1 2 3 4
| public function routeNotificationForOpenOfficialAccount($notification) { }
|
1 2 3 4
| public function routeNotificationForOpenMiniProgram($notification) { }
|
1 2 3 4 5
| public function routeNotificationForWork($notification) { return ; }
|
1 2 3 4 5
| public function routeNotificationForOpenWork($notification) { return ; }
|
3. 可参考官方文档:发送通知
使用Notifiable
特性的notify
方法可以给用户实例发送通知:
1 2 3
| use App\Notifications\WorkNotify;
$user->notify(new WorkNotify(new Text('你好啊~')));
|
使用Notification
facade 给多个可接收通知的实体发送通知
1
| Notification::send($users, new WorkNotify(new Markdown('#你好啊~')));
|
License
Licensed under The MIT License (MIT).
GIT地址:laravel-wechat-notification