Vai al contenuto principale

Panoramica

Con LineChannel puoi inviare messaggi a LINE dalle Notifications di Laravel.
A seguito della chiusura del servizio LINE Notify, il supporto per LINE Notify è terminato. Attualmente, poiché si tratta di notifiche tramite Messaging API, sono presenti limiti sul numero di messaggi in base al piano tariffario. Consulta Prezzi della Messaging API.

Classe Notification

Restituisci LineChannel::class da via() e definisci il messaggio in 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');
    }
}

Modalità di invio

Notifica on-demand

Per inviare notifiche senza associarle a un modello utente, utilizza Notification::route(). La destinazione è un userId o un groupId.
use Illuminate\Support\Facades\Notification;

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

Notifiche dal modello User

Per gestire la destinazione per ogni utente, definisci routeNotificationForLine().
use Illuminate\Notifications\Notifiable;

class User extends Model
{
    use Notifiable;

    public function routeNotificationForLine($notification): string
    {
        return $this->line_id;
    }
}
$user->notify(new TestNotification());
Puoi ottenere userId e groupId da eventi webhook come FollowEvent e JoinEvent.

Tipi di messaggio

Testo

Puoi inviare fino a 5 messaggi.
use Revolution\Line\Notifications\LineMessage;

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

Personalizzazione del nome del mittente e dell’icona

withSender() deve essere chiamato prima dei metodi di aggiunta del messaggio.
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');
}
Puoi specificare nome e icona anche tramite i parametri di create().
use Revolution\Line\Notifications\LineMessage;

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

Sticker

Per gli sticker utilizzabili, consulta la Sticker list.
use Revolution\Line\Notifications\LineMessage;

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

Immagini

Specifica un URL pubblico.
use Revolution\Line\Notifications\LineMessage;

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

Video

Specifica un URL pubblico.
use Revolution\Line\Notifications\LineMessage;

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

Altri tipi di messaggio

Con message() puoi aggiungere un tipo di messaggio arbitrario. Specifica il 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);
}
Per le informazioni più aggiornate consulta il repository GitHub.
Ultima modifica il 13 luglio 2026