A demo illustrating how Laravel notifications work through various channels (e.g. SMS, email, Slack, database).
- Clone this repo.
- Run
composer install
. - Fill in values on the
.env
file and runphp artisan config:cache
. - Create a
database.sqlite
file under the./database
directory. - Run
php artisan migrate --seed
to populate the database with dummy data. - Start testing routes listed on
php artisan route:list
.
-
Sign up for an account (with free credits) on Nexmo to generate values for
NEXMO_*
directives on the.env
file. -
Run
php artisan config:cache
. -
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()
-
Visit
http://[url]/notifications/nexmo/[user-id-you-entered-previously]
and you should see this message:An SMS has been sent to [User]
-
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]
-
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.
-
Run
php artisan config:cache
. -
Run
php artisan tinker
and issue the following commands:$user = App\User::find([user-id])
$user->email = [your-email]
$user->save()
-
Visit
http://[url]/notifications/email/[user-id-you-entered-previously]
and you should see this message:An email has been sent to [User]
-
An email will be sent to the intended recipient:
-
Visit
http://[url]/notifications/db/[user-id]
and you should see this message:A DB notification has been logged for [User]
-
Run
artisan tinker
and issue this command:\App\User::find([user-id-you-entered-previously])->notifications
-
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", }, ], }
-
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 aUser
record. -
Run
php artisan config:cache
. -
Visit
http://[url]/notifications/slack/[user-id]
and you should see this message:A Slack notification has been logged for [User]
-
The webhook will send a message to the specified recipient, which might be either a user or channel. Look for this type of notification:
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:
-
Install the Redis data store service.
-
On your
.env
file, setQUEUE_DRIVER
directive to beredis
instead ofsync
. -
Run
php artisan config:cache
. -
Run
php artisan queue:work
. -
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:
-
Confirm that you are able to receive the said emails:
- 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.
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.
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.