基于 EasyWechat 和 Laravel notification 发送微信小程序模板消息

EasyWechat 自带了小程序的模板消息功能,我在这个得基础上开发了一个适用于消息通知 的扩展,喜欢用Laravel notification的可以关注下~ 仓库地址

安装

composer require ywmelo/template-message

使用

新建通知

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Ywmelo\TemplateMessage\Message\WechatTemplateMessage;
use Ywmelo\TemplateMessage\Channels\WechatTemplateMessageChannel;

class WechatTemplateMessageNotification extends Notification
{
    use Queueable;

    public function __construct($formId)
    {
        $this->formId = $formId;
    }

    public function via($notifiable)
    {
        return [WechatTemplateMessageChannel::class];
    }

    public function toEasySms($notifiable)
    {
        return (new WechatTemplateMessage)
            ->setTemplateId('template_id')
            ->setPage('page')
            ->setFormId($this->formId)
            ->setData([
                'keyword1' => 'keyword1',
                'keyword2' => 'keyword2',
                'keyword3' => 'keyword3',
                'keyword4' => 'keyword4',
            ]);
    }
}

发送

$user->notify(new WechatTemplateMessageNotification($formId));
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 1

小程序模板通知文档快要改版了 以后要用订阅的方式了

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!