> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# 通知 Channel - LINE SDK for Laravel

> 将 Laravel Notification System 与 LINE Messaging API 集成以发送通知。

## 概览

使用 `LineChannel`，你可以从 Laravel Notifications 向 LINE 发送消息。

<Info>
  随着 LINE Notify 服务终止，对 LINE Notify 的支持也已结束。当前基于 Messaging API 发送通知，会根据套餐存在条数限制。请参见 [Messaging API 收费](https://developers.line.biz/en/docs/messaging-api/pricing/)。
</Info>

## Notification 类

在 `via()` 中返回 `LineChannel::class`，并在 `toLine()` 中定义消息。

```php theme={null}
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()`。发送目标为 `userId` 或 `groupId`。

```php theme={null}
use Illuminate\Support\Facades\Notification;

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

### 从 User 模型发送通知

如果需要按用户管理发送目标，可以定义 `routeNotificationForLine()`。

```php theme={null}
use Illuminate\Notifications\Notifiable;

class User extends Model
{
    use Notifiable;

    public function routeNotificationForLine($notification): string
    {
        return $this->line_id;
    }
}
```

```php theme={null}
$user->notify(new TestNotification());
```

<Info>
  `userId` 和 `groupId` 可以从 `FollowEvent`、`JoinEvent` 等 Webhook 事件中获取。
</Info>

## 消息类型

### 文本

最多可以发送 5 条。

```php theme={null}
use Revolution\Line\Notifications\LineMessage;

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

### 自定义发送者名称与图标

`withSender()` 必须在添加消息的方法之前调用。

```php theme={null}
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()` 的参数指定名称与图标。

```php theme={null}
use Revolution\Line\Notifications\LineMessage;

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

### 贴图

可用的贴图请参考 [Sticker list](https://developers.line.biz/en/docs/messaging-api/sticker-list/)。

```php theme={null}
use Revolution\Line\Notifications\LineMessage;

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

### 图片

请指定公开可访问的 URL。

```php theme={null}
use Revolution\Line\Notifications\LineMessage;

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

### 视频

请指定公开可访问的 URL。

```php theme={null}
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()` 指定类型。

```php theme={null}
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);
}
```

<Info>
  更多最新信息请参见 [GitHub 仓库](https://github.com/invokable/laravel-line-sdk)。
</Info>


## Related topics

- [LINE SDK for Laravel](/zh/packages/laravel-line-sdk/index.md)
- [Socialite（LINE Login）- LINE SDK for Laravel](/zh/packages/laravel-line-sdk/socialite.md)
- [Webhook / Bot - LINE SDK for Laravel](/zh/packages/laravel-line-sdk/bot.md)
- [通知通道 - Laravel Bluesky](/zh/packages/laravel-bluesky/notification.md)
- [通知（Notifications）](/zh/notifications.md)
