跳转到主要内容

概览

使用 LineChannel,你可以从 Laravel Notifications 向 LINE 发送消息。
随着 LINE Notify 服务终止,对 LINE Notify 的支持也已结束。当前基于 Messaging API 发送通知,会根据套餐存在条数限制。请参见 Messaging API 收费

Notification 类

via() 中返回 LineChannel::class,并在 toLine() 中定义消息。
use Illuminate\Notifications\Notification;
use Revolution\Line\Notifications\LineChannel;
use Revolution\Line\Notifications\LineMessage;

class TestNotification extends Notification
{
    public function via(object $notifiable): array
    {
        return [
            LineChannel::class,
        ];
    }

    public function toLine(object $notifiable): LineMessage
    {
        return LineMessage::create(text: 'test');
    }
}

发送方式

On-Demand 通知

如果不希望绑定到用户模型来发送通知,可以使用 Notification::route()。发送目标为 userIdgroupId
use Illuminate\Support\Facades\Notification;

Notification::route('line', 'to')
            ->notify(new TestNotification());

从 User 模型发送通知

如果需要按用户管理发送目标,可以定义 routeNotificationForLine()
use Illuminate\Notifications\Notifiable;

class User extends Model
{
    use Notifiable;

    public function routeNotificationForLine($notification): string
    {
        return $this->line_id;
    }
}
$user->notify(new TestNotification());
userIdgroupId 可以从 FollowEventJoinEvent 等 Webhook 事件中获取。

消息类型

文本

最多可以发送 5 条。
use Revolution\Line\Notifications\LineMessage;

public function toLine(object $notifiable): LineMessage
{
    return LineMessage::create()
                      ->text('text 1')
                      ->text('text 2');
}

自定义发送者名称与图标

withSender() 必须在添加消息的方法之前调用。
use Revolution\Line\Notifications\LineMessage;

public function toLine(object $notifiable): LineMessage
{
    return LineMessage::create()
                      ->withSender(name: 'alt-name', icon: 'https://...png')
                      ->text('text 1')
                      ->text('text 2');
}
也可以通过 create() 的参数指定名称与图标。
use Revolution\Line\Notifications\LineMessage;

public function toLine(object $notifiable): LineMessage
{
    return LineMessage::create(text: 'test', name: 'alt-name', icon: 'https://...png');
}

贴图

可用的贴图请参考 Sticker list
use Revolution\Line\Notifications\LineMessage;

public function toLine(object $notifiable): LineMessage
{
    return LineMessage::create()
                      ->text('test')
                      ->sticker(package: 446, sticker: 1988);
}

图片

请指定公开可访问的 URL。
use Revolution\Line\Notifications\LineMessage;

public function toLine(object $notifiable): LineMessage
{
    return LineMessage::create()
                      ->image(original: 'https://.../test.png', preview: 'https://.../preview.png');
}

视频

请指定公开可访问的 URL。
use Revolution\Line\Notifications\LineMessage;

public function toLine(object $notifiable): LineMessage
{
    return LineMessage::create()
                      ->video(original: 'https://.../test.mp4', preview: 'https://.../preview.png');
}

其他消息类型

通过 message() 可以添加任意类型的消息。请使用 setType() 指定类型。
use LINE\Clients\MessagingApi\Model\LocationMessage;
use LINE\Constants\MessageType;
use Revolution\Line\Notifications\LineMessage;

public function toLine(object $notifiable): LineMessage
{
    $location = (new LocationMessage())
        ->setType(MessageType::LOCATION)
        ->setTitle('title')
        ->setAddress('address')
        ->setLatitude(0.0)
        ->setLongitude(0.0);

    return LineMessage::create()
                      ->message($location);
}
更多最新信息请参见 GitHub 仓库
最后修改于 2026年7月13日