Laravel notifications are not being saved when using custom channel

1.1k views Asked by At

I have done this (https://laravel.com/docs/5.7/notifications#database-notifications), I ran the migration, I created a toArray() and a toDatabase() functions in the notification, and the notifications are correctly being sent, however, notifications are not being saved. My HablameChannel::send() method can send many messages because a certificate can have many phone numbers to notify, so I send the same notification to all of them.

This is my channel code:

<?php

namespace App\Channels;

use App\Models\Message;
use Illuminate\Notifications\Notification;

class HablameChannel
{
    /**
     * Send the given notification.
     * Envía las mensajes de
     *
     * @param  \App\Models\Owner  $notifiable
     * @param  \Illuminate\Notifications\Notification|\App\Notifications\CertificateToExpire $notification
     * @return void
     */
    public function send($notifiable, $notification)
    {
        // Se consultan los mensajes de la entidad notificable, en este caso el certificado.
        $messages = $notification->toHablame($notifiable);

        // Send notification to the $notifiable instance...
        $messages->each->send();
    }
}

This is my notification code:

<?php

namespace App\Notifications;

use App\Channels\HablameChannel;
use App\Models\Certificate;
use App\Models\Message;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;

class CertificateToExpire extends Notification
{
    use Queueable;

    /**
     * Certificado por el cual se va a notificar que está a punto de caducar.
     *
     * @var \App\Models\Certificate
     */
    protected $certificate;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(Certificate $certificate)
    {
        $this->certificate = $certificate;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [HablameChannel::class];
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  \App\Models\Owner  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return $this->toHablame($notifiable)->toArray();
    }

    /**
     * Obtiene los mensajes a enviar por Háblame SMS.
     *
     * @param  \App\Models\Owner  $notifiable
     * @return \Illuminate\Support\Collection|\App\Models\Message[]
     */
    public function toHablame($notifiable)
    {
        $template = setting(
            'plantilla_de_vencimiento',
            'CDA DEL CESAR de la 44 le recuerda que la Revisión Técnico-Mecánica del vehículo'
                . ' de placa {number_plate} está por vencer.'
                . ' Visítenos en la CL 44 N 23A - 46. 3205739223'
        );

        $engine = new \StringTemplate\Engine();

        $body = $engine->render($template, [
            'number_plate' => $this->certificate->number_plate,
        ]);

        $now = Carbon::now();

        return $this->certificate->recipients_to_notify->map(
            function (array $recipient) use ($body, $now) {
                return $this->certificate->messages()->create([
                    'receiver_name' => $recipient['receiver_name'],
                    'to' => $recipient['to'],
                    'body' => $body,
                    'reference' => 'Certificados a punto de vencer.',
                ]);
            }
        );
    }
}

This is the code where I call for the notification:

$certificate->owner->notify(new CertificateToExpire($certificate));
1

There are 1 answers

0
andreshg112 On BEST ANSWER

Sorry, I just had to add the database driver to CertificateToExpire::via() method.

public function via($notifiable)
{
    return [HablameChannel::class, 'database'];
}