jpcaparas / laravel-notifications-demo

A demo illustrating Laravel 5.4 notification capabilities via these channels: email, SMS, database, Slack.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Laravel Notifications Demo

A demo illustrating how Laravel notifications work through various channels (e.g. SMS, email, Slack, database).

Installation

  1. Clone this repo.
  2. Run composer install.
  3. Fill in values on the .env file and run php artisan config:cache.
  4. Create a database.sqlite file under the ./database directory.
  5. Run php artisan migrate --seed to populate the database with dummy data.
  6. Start testing routes listed on php artisan route:list.

Testing

SMS (via Nexmo) notifications

  1. Sign up for an account (with free credits) on Nexmo to generate values for NEXMO_* directives on the .env file.

  2. Run php artisan config:cache.

  3. Run php artisan tinker and issue the following commands:

    $user = App\User::find([user-id])

    $user->phone_mobile = [your-mobile-phone-number-with-country-code]

    $user->prefers_sms = true

    $user->save()

  4. Visit http://[url]/notifications/nexmo/[user-id-you-entered-previously] and you should see this message:

    An SMS has been sent to [User]

  5. You should receive an SMS like the one below:

    [User] <[user-email]> arrived home at 2017-08-08 21:34:18.[FREE SMS DEMO, TEST MESSAGE]
    

Email notifications

  1. Sign up for a free Mailtrap account to generate values for MAIL_* directives on the .env file.

    Note: You can choose your own email provider, but for this demo, we're going with Mailtrap, because it's the default provider shipped with Laravel 5.4.

  2. Run php artisan config:cache.

  3. Run php artisan tinker and issue the following commands:

    $user = App\User::find([user-id])

    $user->email = [your-email]

    $user->save()

  4. Visit http://[url]/notifications/email/[user-id-you-entered-previously] and you should see this message:

    An email has been sent to [User]

  5. An email will be sent to the intended recipient:

    email-notification


DB notifications

  1. Visit http://[url]/notifications/db/[user-id] and you should see this message:

    A DB notification has been logged for [User]

  2. Run artisan tinker and issue this command:

    \App\User::find([user-id-you-entered-previously])->notifications

  3. You should see something similar to the below response, confirming that the notification has been logged to the database:

    Illuminate\Notifications\DatabaseNotificationCollection {#697
       all: [
         Illuminate\Notifications\DatabaseNotification {#690
           id: "add9130d-0d66-4559-b213-ee392d1cbe79",
           type: "App\Notifications\ToDb",
           notifiable_id: "1",
           notifiable_type: "App\User",
           data: "{"user_id":1,"ip":"127.0.0.1","user_agent":"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/59.0.3071.115 Safari\/537.36"}",
           read_at: null,
           created_at: "2017-08-08 21:53:52",
           updated_at: "2017-08-08 21:53:52",
         },
       ],
     }
    

Slack notifications

  1. Generate a webhook for the SLACK_WEBHOOK_URL directive on the .env file.

    Note: Webhook URLs can be defined on a per-user basis. Simply define it on the slack_webhook_url property of a User record.

  2. Run php artisan config:cache.

  3. Visit http://[url]/notifications/slack/[user-id] and you should see this message:

    A Slack notification has been logged for [User]

  4. The webhook will send a message to the specified recipient, which might be either a user or channel. Look for this type of notification:

    slack-notification

Testing asynchronicity

Without the Illuminate\Contracts\Queue\ShouldQueue interface implemented, notifications are by default, synchronous and thereby slow, especially when the notification connects to a third-party API (e.g. email, SMS, Slack), as the API request is becomes part of the page lifecycle.

To solve this, you can use queues to dispatch notifications in their own separate, asynchronous processes, independent of the current page lifecycle.

To test asynchronous requests on email notifications:

  1. Install the Redis data store service.

  2. On your .env file, set QUEUE_DRIVER directive to be redis instead of sync.

  3. Run php artisan config:cache.

  4. Run php artisan queue:work.

  5. Visit http://[url]/notifications/email/[user-id]?async=true&total=2. This will dispatch an async email notification 2 times (you can increase that number, but be aware of repercussions).

    Notice that the page will load almost instantaneously because the notifications have been offloaded onto their own processes:

    redis-queue

  6. Confirm that you are able to receive the said emails:

    mailtrap-async-emails

Notes

  • For portability, I chose SQLite as the demo's default database. No one likes to set up a full-on MySQL database for a simple demo.
  • Nexmo SMS charges vary country-to-country. If you find yourself running out of credits during the demo, try upgrading to their paid tier or attempt to create a new account.

TODO

Ideas and feature suggestions can be added to the Backlog column on the Ideas project board. I'll try to action them when I have the time.

Attributions

This wouldn't be possible without being granted a role as Software Developer at Pixel Fusion, an award-winning product development company at Parnell, Auckland.

About

A demo illustrating Laravel 5.4 notification capabilities via these channels: email, SMS, database, Slack.


Languages

Language:PHP 95.8%Language:HTML 3.0%Language:Vue 0.6%Language:ApacheConf 0.6%