Resumen
Con LineChannel puedes enviar mensajes a LINE desde Laravel Notifications.
Con el cierre del servicio de LINE Notify, la compatibilidad con LINE Notify ha finalizado. Actualmente las notificaciones utilizan la API de Messaging, por lo que existen límites de mensajes según el plan de precios. Consulta las tarifas de la API de Messaging.
Clase Notification
En via() devuelve LineChannel::class y en toLine() define el mensaje.
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');
}
}
Patrones de envío
Notificaciones On-Demand
Para enviar notificaciones sin vincularlas a un modelo de usuario, utiliza Notification::route(). El destino es un userId o un groupId.
use Illuminate\Support\Facades\Notification;
Notification::route('line', 'to')
->notify(new TestNotification());
Notificación desde el modelo User
Para gestionar el destino por usuario, define routeNotificationForLine().
use Illuminate\Notifications\Notifiable;
class User extends Model
{
use Notifiable;
public function routeNotificationForLine($notification): string
{
return $this->line_id;
}
}
$user->notify(new TestNotification());
Puedes obtener userId y groupId de eventos de Webhook como FollowEvent o JoinEvent.
Tipos de mensaje
Texto
Se pueden enviar hasta 5 mensajes.
use Revolution\Line\Notifications\LineMessage;
public function toLine(object $notifiable): LineMessage
{
return LineMessage::create()
->text('text 1')
->text('text 2');
}
Personalizar el nombre y el icono del remitente
withSender() debe llamarse antes que los métodos que añaden mensajes.
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');
}
También puedes indicar el nombre y el icono como parámetros de create().
use Revolution\Line\Notifications\LineMessage;
public function toLine(object $notifiable): LineMessage
{
return LineMessage::create(text: 'test', name: 'alt-name', icon: 'https://...png');
}
Stickers
Consulta la lista de stickers para ver los que puedes usar.
use Revolution\Line\Notifications\LineMessage;
public function toLine(object $notifiable): LineMessage
{
return LineMessage::create()
->text('test')
->sticker(package: 446, sticker: 1988);
}
Imagen
Indica una URL pública.
use Revolution\Line\Notifications\LineMessage;
public function toLine(object $notifiable): LineMessage
{
return LineMessage::create()
->image(original: 'https://.../test.png', preview: 'https://.../preview.png');
}
Vídeo
Indica una URL pública.
use Revolution\Line\Notifications\LineMessage;
public function toLine(object $notifiable): LineMessage
{
return LineMessage::create()
->video(original: 'https://.../test.mp4', preview: 'https://.../preview.png');
}
Otros tipos de mensaje
Con message() puedes añadir cualquier tipo de mensaje. Especifica el tipo con 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);
}