Using Laravel Notifications

One of the easiest ways to send notifications to your users is to utilize Laravel Notificationsopen in new window. If you are already using them in your app, you'll find it extremely easy to configure them for Belltastic. Let's cover that now.

API key required

Make sure you have set up your API key in order for Laravel Notifications to work with Belltastic. You can read more about it on Setting up API key.

Available alternative

There's also another way to send the notifications without utilizing Laravel's Notifications. Read more about it on the Using Belltastic Classes page.

Setting up User model

In order for Laravel to know where to send the notifications, you must implement the routeNotificationForBelltastic() method on your User model:

class User extends Authenticatable
{
    /**
     * Get a Belltastic User to route this notification to.
     *
     * @return \Belltastic\User|array
     */
    public function routeNotificationForBelltastic()
    {
        return new \Belltastic\User([
            'id' => $this->id,
            'project_id' => 1,
        ]);

        // Alternatively, you can return a plain array
        return [
            'id' => $this->id,
            'project_id' => 1,
        ];
    }
}

Setting up Notifications

In order to have a Laravel Notification sent to Belltastic, all you have to do is add the 'belltastic' channel:

class SampleNotification extends Notification
{
    public function via($notifiable)
    {
        return ['belltastic'];
    }

To define the contents of the notification, you can either implement the toBelltastic($notifiable) (will take priority, if exists) or, alternatively, toArray($notifiable) method:

class SampleNotification extends Notification
{
    public function via($notifiable)
    {
        return ['belltastic'];
    }

    /**
     * Get the contents of the notification
     * 
     * @param  $notifiable
     * @return \Belltastic\Notification|array
     */
    public function toBelltastic($notifiable)
    {
        return [
            'title'      => 'Notification title',
            'body'       => 'And a longer body that explains more about the event.',
            'action_url' => 'https://your-cta-link.test',
            'category'   => 'system',       // or any other string that defines this notification's category
            'icon'       => 'https://example.com/icon.png',
        ];
        
        // Alternatively, you can also return an instance of \Belltastic\Notification
        // containing the data:
        return new \Belltastic\Notification([
            'title' => 'Notification title',
            // and other properties...
        ]);
    }
    
    // As a fallback, this method will be called if
    // the `toBelltastic($notifiable)` is not defined.
    public function toArray($notifiable)
    {
        return [
            'title' => 'Notification title',
            // and other properties...
        ];
    }
}

Sending notifications to Belltastic

Once you have set up both the User model and one or more Laravel Notifications, sending them is as easy as calling $user->notify():

$user->notify(new SampleNotification());