微信 template message for Laravel Notification
huo-zi/laravel-wechat-notification is a Laravel package for 微信 template message for laravel notification.
It currently has 15 GitHub stars and 255 downloads on Packagist (latest version v1.0.6).
Install it with composer require huo-zi/laravel-wechat-notification.
Discover more Laravel packages by huo-zi
or browse all Laravel packages to compare alternatives.
Last updated
这是一个laravel框架下基于laravel-wechat使用微信/企微应用消息通知作为notification通道的composer包,使用前请先熟读laravel-消息通知
目前已支持:
composer require "huo-zi/laravel-wechat-notification"
notification的通知类中可以使用如下方法:public function via($notifiable)
{
/*
* 支持的via列表
* official_account:公众号
* mini_program:小程序
* open_official_account:开放平台公众号
* open_mini_program:开放平台小程序
* work:企业微信
* open_work:开放平台企业微信
*/
return ['official_account', 'mini_program'];
}
public function toOfficialAccount()
{
// 订阅消息可使用 SubscribeMessage::officialAccount()
return WechatMessage::officialAccount('app_name')->template('templateId')->url($url)->data(['fisrt'=>'...']);
}
public function toMiniProgram()
{
// 一次性订阅消息可使用 SubscribeMessage::miniProgram()
return WechatMessage::miniProgram('app_name')->template($templateId)->formId($formId)->data([
'first' => ''
//
]);
}
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);
}
public function toOpenMiniProgram()
{
return WechatMessage::openFlatform($name)->miniProgram($appId, $refreshToken, $accessToken)->template($templateId);
}
public function toWork()
{
return WechatMessage::work($name)->message($message)->ofAgent($agentId);
}
public function toOpenWork()
{
return WechatMessage::openWork($name)->work($authCorpId, $permanentCode)->message($message)->ofAgent($agentId);
// 同样也支持创建好开放平台对象后使用及闭包创建work对象
$messenger = (new WechatOpenWork($openWork))->work(function($openWork) {
return $openWork->work($authCorpId, $permanentCode);
});
return $messenger->message($message)->ofAgent($agentid);
}
triat Notifiable的模型里增加获取对应openid/userid的方法:可以参考官方文档里发送邮件通知时自定义收件人
openidpublic function routeNotificationForOfficialAccount($notification)
{
// 返回当前model的公众号openid
return $this->openid;
}
openidpublic function routeNotificationForMiniProgram($notification)
{
// 返回当前model的小程序openid
}
openidpublic function routeNotificationForOpenOfficialAccount($notification)
{
// 返回对应开放平台公众号用户的openid
}
openidpublic function routeNotificationForOpenMiniProgram($notification)
{
// 返回对应开放平台小程序用户的openid
}
useridpublic function routeNotificationForWork($notification)
{
// 返回当前model的企业微信userid
return ;
}
useridpublic function routeNotificationForOpenWork($notification)
{
// 返回对应开放平台企业用户userid
return ;
}
使用Notifiable特性的notify方法可以给用户实例发送通知:
use App\Notifications\WorkNotify;
$user->notify(new WorkNotify(new Text('你好啊~')));
使用Notificationfacade 给多个可接收通知的实体发送通知
Notification::send($users, new WorkNotify(new Markdown('#你好啊~')));
Licensed under The MIT License (MIT).