This episode is for members only

Sign up to access "Building a Notification Preference System in Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
09. Fetching notifiable channels

Transcript

00:00
Okay this is great but you're probably wondering well when I do send a new notification that a new project has been created for example how do I work out how to send this to the user
00:10
by email and by slack? Well what we're not going to be doing in this course is setting up notifications to actually send this by email or slack but I'm going to show you how you can grab this data back inside of your notifications and we'll also write a test for this as well which
00:25
isn't strictly necessary but it will help us verify that we are actually sending this to the correct place. Okay so we are obviously going to start off with a actual notification so let's go ahead and make out a notification here and we'll just use project created as an
00:42
example. Now if we open up project created we could store this in the database so there is a concept of storing this via the database as well as mail whatever you need to do. Now we do have mail set up here we've called this email within our database
00:59
you would need to change that so we need to make sure that this is changed over to mail when we actually store this so that would be here so these would need to actually be mail the point kind of remains the same so I'm actually going to change these so I
01:14
don't really want to confuse anything in here when we get to this so let's change that to mail let's change the notification channel to mail as well because remember that needs to map up to what we have within our application let's just go over quickly and run our test
01:31
so let's run test on everything and that's all good let's go over to our notification controller test and look for where we've used email and we're going to change that to mail as well I just don't want to get into the point where we're confused
01:46
between the two things being the keys and the values let's change that and email to mail and a few more to go and we should be good email okay so next up let's go over to our notification channel cedar that needs to be mail let's just run
02:07
our test again just to make sure that's all good and yeah okay great okay so back to what we were doing before so we've created our project created notification now if we were to store this in the database what we can actually do
02:22
is provide a method inside of any of our notifications called database type so we can create out a database type method now into this we get the notifiable thing notifiable that we're sending to that's
02:38
most likely going to be the user and in here we return a string and this is where it gets stored in the database so for example I would call this project underscore created and when this got stored in the database under our notifications table
02:53
this would then go ahead and be called project created now we might have a conflict in the fact that we have this called notifications we probably do so you might want to rename that table and that model again it doesn't really matter for now
03:07
but this name here the whole point of this is to map up to the notification type that we have selected in our table just here so the reason that this is helpful isn't because we're going to go ahead and store this value necessarily
03:23
anywhere because we might not be choosing to store these in the database but so when we get the via so the the array of things we're sending this through we can go ahead and look this up by this database type so what do I mean by this well we want
03:41
to choose how we want to send these so normally what we would do is we would have an array of mail slack sms maybe database whatever you want us to send in this is the array that's now going to come from the database in the user's preferences
03:56
so rather than actually hard code this we're going to say notifiable which is the user by the way notification preferences so what I'm actually going to do is if I always know I'm going to send this to a user I'm going to type in this to
04:10
user so we get the notification preferences autocomplete here and then we're going to say where because we need to scope this down to where the type is the same as the type we have here which is the database type
04:23
so we're just going to say this database and type and passing notifiable because that needs that then we're going to grab the first result back because we're only going to have one per notification type then we're going to grab the pivot here
04:38
and we're going to grab the channels which is going to return to us an array that looks something like this mail and slack so rather than hard code this now whenever we send this notification off to that user who has selected or not within our interface which
04:56
notification preferences they want they're then going to receive that on them channels they have selected or this is just going to be an empty array and it's not going to send okay so now that we've done this let's write a test so we can set the user up with these
05:10
preferences and actually make sure this gets sent to the right place based on the notifications that they've sent and really you only need one of these tests you could write the same test for every single notification type you create
05:23
or you could come up with some common functionality to perhaps reuse this selector here this database query selector we may adjust things later but let's go ahead and write a test for this so php artisan pest test and let's say notifications and project created test
05:43
okay let's open up the project created test test here and let's think about what we want to do here well we want to send a notification there's a couple of ways that we can sort of simulate that and make sure the channels are being
06:00
sent to are the user's preferred notification preferences pretty straightforward but let's type these things out okay so what are we going to call this let's say uses preferences
06:20
for the user to determine channels something like that okay so let's figure out how we want to do this well we need to set up our fake notification functionality so to do this we're going to use eliminate support facade notification and
06:37
fake so we don't actually send the notifications then we're going to go ahead and create our user with some attached preferences already so we can make sure that these come through so let's say user factory with
06:49
attached and we're going to go ahead and create that user now attached is going to be or has attached i think it is that's why we're seeing an error there yeah has attached
07:04
uh this is going to be the notification so the models which now needs to be pulled in locally because we have two things with the same name and we're just going to have a standard notification for a notification group we could set
07:17
this up by the way to always happen but we'll leave that there for now and what channels do we want this to be for so let's say channels and let's say mail and slack and that needs to be an array in there so that's what's going to be
07:34
inserted within the pivot table and we need to provide the relationship name which is notification preferences okay so we should now have a user if we just die dump on this user notification preferences first let's go and
07:52
access the pivot and channels let's run this test so tests feature notifications and project created test and there we go yeah mail and slack so we know that we've got the right channels so there's a couple of ways that we can actually send the
08:08
notification or fakely send the notification since we've got notification fake sent up and check the result of this let's do the hard way first and i'll show you a much easier way to do this so we're going to say user and notify much like we would do within our
08:21
application when we use this we just new up the notification that we want to send in this case we're testing project created then we can say notification so the facade assert sent to and we can assert that this was sent to the user
08:37
and we want to say the project created notification is sent so let's go ahead and just run our test here and yeah sure enough we get green but we want to dive into this a little bit more deeply so if we want to do that we can create our a closure in here
08:52
we can go ahead and type in the notification which is project created and we'll just give that name of notification we'll bring the user into scope within this closure and then we're going to return true now let's just go and run our test here that does pass but now we have the
09:08
opportunity to either return based on a condition or in our case we can continue to perform expectations with test so i'm going to say we'll grab that notification and check via so the via method that we have over in here so via
09:27
here passing in the notifiable so we need to pass the user into that and then we can just expect this to contain email and slack or mail in this case and slack so that's one way to do it let's go and run that test again and yeah we get an additional assertion
09:44
and we now know that based on this that we've added that will get sent via them channels now this is one way to do it so we could do it like this but there is a much easier way that we can do as well so let's go ahead and
10:02
comment out what we have here and let's go down here and try this out so the easiest and fastest way to do this would be to new up the project created notification and just access the method that's all we need to do so we just say
10:19
expect and notification via pass that user in and you guessed it to contain mail and slack we don't even need to send a notification to check that it's going to go to the right place because we already know that via
10:37
when we return that array from via it will always send it to that place so let's run our test again and yeah sure enough we get green that test is going to run a little bit faster as well not necessarily all the time but it will generally run faster because we're of course not notifying the user
10:54
we don't have any fakes to deal with and we're not asserting that this is sent to we're just newing this up and checking the return value of a method so there we go we don't need to write this test for every single notification that we create but if you were
11:06
testing if a certain notification was being sent you could just very easily slip in this additional expectation for each of your tests to make sure it's being sent to the right place
10 episodes1 hr 23 mins

Overview

Need to store advanced notification preferences in your application?

In this course, we cover displaying a matrix of notification groups, types and channels with the ability to sync preferences cleanly and quickly.

Once you’re done, your users will be able to choose what notifications they receive, and how.

Oh, and we’ll write tests to back everything up, look at how to use notification preferences in Notification classes, and how to set defaults for when users register.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!