Laravel Notification Channels

website·Indexed 34 days ago

https://laravel-notification-channels.com/

A collection of community-maintained notification channels for Laravel, enabling integration with various services including Discord, Google Chat, Twilio, AWS SNS, Pusher Beams, and others.

Tokens
47.8K
Snippets
312
Records
368
Agent score
70%

What's inside Laravel Notification Channels

  1. Route Discord notifications to a specific channel

    In any model that should be notifiable via Discord, implement a routeNotificationForDiscord method that returns the Discord channel ID (snowflake ID).
    class Guild extends Eloquent
    {
        use Notifiable;
    
        public function routeNotificationForDiscord()
        {
            return $this->discord_channel;
        }
    }
  2. Send a webhook notification in Laravel

    To send a webhook notification, use the WebhookChannel in the via() method of your notification class and define the payload in a toWebhook() method using WebhookMessage. Additionally, the Notifiable model must implement a routeNotificationForWebhook() method that returns the destination URL for the POST request.
    use NotificationChannels	extbf{Webhook}\WebhookChannel;
    use NotificationChannels	extbf{Webhook}\WebhookMessage;
    use Illuminate	extbf{Notifications}\Notification;
    
    class ProjectCreated extends Notification
    {
        public function via($notifiable)
        {
            return [WebhookChannel::class];
        }
    
        public function toWebhook($notifiable)
        {
            return WebhookMessage::create()
                ->data([
                   'payload' => [
                       'webhook' => 'data'
                   ]
                ])
                ->userAgent("Custom-User-Agent")
                ->header('X-Custom', 'Custom-Header');
        }
    }
    
    // In your Notifiable model:
    public function routeNotificationForWebhook()
    {
        return 'http://requestb.in/1234x';
    }
  3. Route Facebook notifications in a Notifiable model

    Instead of calling to($userId) manually in the notification class, you can define a routeNotificationForFacebook() method in your Notifiable model to automatically provide the recipient's page-scoped user id (PSID).
    /**
     * Route notifications for the Facebook channel.
     *
     * @return int
     */
    public function routeNotificationForFacebook()
    {
        return $this->fb_messenger_user_id;
    }
  4. Send notifications via Smsapi

    To use the Smsapi channel, return SmsapiChannel::class in the via() method of your notification class and implement the toSmsapi() method to return a message object (e.g., SmsapiSmsMessage or SmsapiMmsMessage).
    use Illuminate
    otifications\Notification;
    use NotificationChannels\Smsapi\SmsapiChannel;
    use NotificationChannels\Smsapi\SmsapiSmsMessage;
    
    class FlightFound extends Notification
    {
        public function via($notifiable)
        {
            return [SmsapiChannel::class];
        }
    
        public function toSmsapi($notifiable)
        {
            return (new SmsapiSmsMessage())->content("Buy now your flight!");
        }
    }
  5. Install Pushbullet notification channel for Laravel

    Install the Pushbullet notification channel package using Composer to enable Pushbullet notifications in Laravel 5.5, 6.x, 7.x, and 8.x.
    composer require laravel-notification-channels/pushbullet
  6. Install Laravel GCM Notification Channel

    Install the laravel-notification-channels/gcm package via Composer and register the service provider in config/app.php.
    composer require laravel-notification-channels/gcm

    // In config/app.php NotificationChannels\Gcm\GcmServiceProvider::class,

  7. Send direct messages via Discord notifications

    Discord treats direct messages as private channels. To send a DM, you must first generate a private channel ID using the getPrivateChannel method from the NotificationChannels\Discord\Discord class, which requires the user's snowflake ID. Store this private channel ID in your database and return it via the routeNotificationForDiscord method.
    use NotificationChannels\Discord\Discord;
    
    class UserDiscordSettingsController
    {
       public function store(Request $request)
       {
           $userId = $request->input('discord_user_id');
           $channelId = app(Discord::class)->getPrivateChannel($userId);
    
           Auth::user()->update([
               'discord_user_id' => $userId,
               'discord_private_channel_id' => $channelId,
           ]);
       }
    }
  8. Route Gammu notifications in a Notifiable model

    Instead of passing the phone number manually in the notification class, you can define a routeNotificationForGammu() method in your notifiable model to return the recipient's phone number.
    /**
     * Route notifications for the Gammu channel.
     *
     * @return string
     */
    public function routeNotificationForGammu()
    {
        return $this->phone;
    }
  9. Install AfricasTalking notification channel for Laravel

    Install the AfricasTalking notification channel package via Composer. The service provider is loaded automatically upon installation.
    composer require laravel-notification-channels/africastalking
  10. Configure Discord bot credentials

    To send notifications, you must create a Discord application and bot user. Add the bot's API token to your services.php configuration file and run the setup command to identify the bot on your server.
    // config/services.php
    'discord' => [
        'token' => 'YOUR_API_TOKEN',
    ],
    php artisan discord:setup