Masonite Notifications
Introduction
Masonite Notifications can easily add new notification sending semantics for your Masonite project. These notifications could be sending an email or a slack message. This package is designed to be extremely simple and modular so allow new notification abilities to be added to it through third party integrations.
Installation
In order to get started using Masonite Notifications, we first have to pip install it:
And then add the provider to our PROVIDERS
list:
Thats it! Let's see how it works!
Usage
There are a few concepts we'll need to cover so you can fully understand how Notifications work. First we will cover the high level stuff and then slowly work down into the lower level implementations. For the purposes of this documentation, we will walk through how to setup a welcome notification so we can send an email when a user signs up.
Creating a Notification
In order to use it we can create a notification class. We can do this simply with a new craft command.
This will create a notification in the app/notifications
directory. Feel free to move it wherever you like though.
This will create a class like so:
Building Our Mail Notification
Let's now walk through how to build a notification so we can send the email to our user.
Since our notification inherits from Notifiable
, we have access to a few methods we will use to build the notification. We'll show a final product of what it looks like since it's pretty straight forward but we'll walk through it after:
Notice here we are calling a few methods such as driver
, panel
, line
, etc. If we send this message it will look like:
Not bad. We can use this logic to easily build up emails into a nice format simply.
Options
Let's walk through the different options to build an email notification and what they do.
Sending the Notification
Now that we have built our notification above, we can send it in our controller (or anywhere else we like):
Notice here we simply specified the Notify class in the parameter list and we are able to pass in our awesome new WelcomeNotification into the mail method.
NOTE: The method you should use on the notify class should correspond to the method on the notification class. So for example if we want to execute the slack method on the WelcomeNotification then we would call :
The method you call should be the same as the method you want to call on the notification class. The Notify
class actually doesn't contain any methods but will call the same method on the notification class as you called on the Notify
class.
Sending Via
You can also send multiple notifications and notification types with the via
and send
method like so:
This is useful for sending mail more dynamically or just sending multiple types of notifications in 1 line.
Queuing the Notification
If you would like to queue the notification then you just need to inherit the ShouldQueue
class and it will automatically send your notifications into the queue to be processed later. This is a great way to speed up your application:
Building Our Slack Notification
Out of the box, Masonite notifications comes with Slack support as well in case we want to send a message to a specific slack group.
NOTE: In order to use this feature fully, you'll need to generate a token from Slack. This token should have at minimum the channels:read
, chat:write:bot
, chat:write:user
and files:write:user
permission scopes. If your token does not have these scopes then parts of this feature will not work.
Going back to our WelcomeNotification, we can simply specify a new method called slack
.
Notice the new slack method at the bottom. we will use this method to build our slack notification. Again we will show you a full example and then run through all the methods:
Options
Sending a Slack Notification
Now that we have built our notification above, we can send it in our controller (or anywhere else we like):
Notice here we simply specified the Notify
class in the parameter list and we are able to pass in our awesome new WelcomeNotification into the slack method.
Building Integrations
The Notifiable
class is very modular and you are able to build custom integrations if you like pretty simply. In this section we'll walk through how to create what are called Components
.
What are Components?
Components are classes that can be added to a Notification class that extend the behavior of the notification. In fact, the Notifiable class is just a simple abstraction of two different components. Let's look at the signature of the class that we have been inheriting from.
The Component classes are the classes that have our methods we have been using. If you'd like to see the source code on those components you can check them out on GitHub to get a lower level understanding on how they work.
Creating a Component
Let's walk through a bit on how we created our MailComponent by creating a simplified version of it. First let's create a simple class:
Now let's add a line and a subject method to it:
and let's use these two methods to build a template attribute
Since we returned self we can keep appending onto the notification class like we have been.
The actual MailComponent class is a bit more complex than this but we'll keep this simple for explanatory purposes.
The Fire Method
Whenever we insert the notification into the Notify class:
This will call the mail method on the notification class (or whatever other method we called on the Notify class).
Once that is returned then it will call the fire_mail method which you will specify in your component.
If you are created a discord notification then you should have a fire_discord
method on your component and you will call it using notify.discord(WelcomeNotification)
.
Since we want to call the mail method on it, we will create a fire_mail
method:
Passing Data With Protected Members
Sometimes you will want to pass in data into the fire_mail
method. In order to keep this simple and modular, any keyword arguments you pass into the Notify class will be set on the notification class as a protected member. For example if we have this:
It will set a _to
attribute on the notification class BEFORE we get to the fire method.
So using the example above we will be able to do:
We can use this behavior to pass in information into the fire_mail
method while keeping everything clean.
A practical example is sending the message to a certain user:
Notice here we now have a _to
member on our class we can use because we passed it through from our Notify
class.
Sending The Mail
Ok so finally we have enough information we need to send the actual email. The fire_method is resolved by the container so we can simply specify what we need to send the email.
Our notification class will look like:
Our Notify class will look like:
and our fire method will look like:
Remember the _to
class attribute that came from the keyword argument in the Notify
class.
Last updated