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()를 사용합니다. 전송 대상은 userId 또는 groupId입니다.
use Illuminate\Support\Facades\Notification;
Notification::route('line', 'to')
->notify(new TestNotification());
User 모델에서의 알림
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());
userId나 groupId는 FollowEvent나 JoinEvent 등 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);
}