> ## 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/ja/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 通知

若不綁定 User 模型即傳送通知,可使用 `Notification::route()`。傳送目標為 `userId` 或 `groupId`。

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

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

### 從 User 模型傳送通知

若要依 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/ja/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-TW/packages/laravel-line-sdk/index.md)
- [Socialite(LINE Login)- LINE SDK for Laravel](/zh-TW/packages/laravel-line-sdk/socialite.md)
- [Webhook / Bot - LINE SDK for Laravel](/zh-TW/packages/laravel-line-sdk/bot.md)
- [通知頻道 - Laravel Bluesky](/zh-TW/packages/laravel-bluesky/notification.md)
- [通知（Notifications）](/zh-TW/notifications.md)
