Welcome back.
And this week, we're going to finalize the notification feature and pretty much finalized the application,
even though there is still a lot potentially to do.
So we still have the functionality, for example, to delete a user from the members list or.
Yeah, I mean, there are so many different things.
You can definitely extend the application to your liking.
Now, this is just the beginning.
Of course, if you look at complex and fully fledged project management tools, they have a lot of more
features, but basically all of them or most of them at least build up on things that you have learned
so far.
All right.
So by the time that you have gotten this far, you should be able to add all of those features to your
application.
But for now, the last thing that I want to show you is how to add a notification to display it inside
of the user's phone.
So I'm testing it right now.
I have the application installed on my phone.
So I'm going to create a new board, which I'm going to call notifications test.
Bought.
All right, that is created and then I will assign a member to it, which will be the member who uses
this emulator.
OK, who is logged in into the simulator?
Let me add that.
And we could see a little icon here at the top popped up.
Let's look at it.
And you can see assigned to the board notification test board, you have been assigned to the new board
by Frank.
OK, so Frank is the user that I logged in with on the application or in the application on my phone,
and that's exactly what I'm now getting here.
All right.
So now I can click on this and that Will sent me over to the project manager application, and that
is pretty much it.
So that is the notification that I wanted to show you.
And now let's get into it.
And by the way, if you want to learn more about push notifications using Firebase and Advance Best
clients, then I can recommend this link.
So here are a bunch of explanations for what's going on.
So here, for example, the notification you can see here, small icon is number one.
And this required a set with small icon than name then timestamp, which is now large icon with just
as I can hear the title and text.
OK, so all of that as explained in this article, also the difference between notification and data
messages and then the code as well, even though it is Java code.
But you can really easily apply that to Scotland as well.
OK, so we are going to build some of the stuff that you can see here in this video.
By the way, he uses an advanced rest client in order to trigger the event.
So there are multiple ways, of course, how you could do a notification.
One is from the server side, one is from client side.
And we are not going to do the feature where you actually get triggered a notification if the timer
runs up.
So, for example, we have due dates.
Right.
But those due dates, they currently don't really represent anything in the application.
They are just a date there.
Of course, you could implement the feature where the user gets a notification of the due date is coming.
So let's say a day before the due date or maybe a week before the due date.
But that would require for you to set up this whole functionality in the backend.
So this cannot just be done by the application itself.
OK, so this is why we are going to keep it simple.
We are not looking at the back end.
We are just going to add the feature that once you add a user to a board, that user gets a notification
on his phone.
And I think this is already good enough and this is already going to teach you the important basics
of that.
Of course, when it comes to anything that you learn here, you'll find something in the documentation
about it as well.
All right.
So you can check out the notification there.
Things are explained as well with also the same image that you see here with some explanations and expanded
notification if you ever need to add and so forth.
So if you want to create your very own notifications, there are a lot of settings that you can set
up.
OK, I'm just going to show you one way of doing it.
Of course, there are plenty of ways of doing it and things to consider and also things to consider
when working with different versions of Android and so forth.
All right, but now let's get into coding, so the thing is, we are going to require quite some constants
here, which we will need for the notification.
OK, so I'm just going to post them in here and go over them real quick.
So we will need the basic URL.
This is to have some basic URL.
If you want to send something, then the ACM authorization, which is going to be a string that we call
like that SCMP, which we're just going to call key DEVESON server key to which you need to use your
own server key here.
Then the title, the message, the data and the string for the key to, OK, so we have a bunch of keys
that we need and then we will assign data to that key.
So as you saw earlier, we need a title, we need a message, and then we need to data and who we want
to send it to.
Now, if you might wonder where to get the Afkham server key from, well, you can Google that, by
the way, just Google find your sender key and silver key that you will find that you need to go to
your project settings and they're on their cloud messaging.
You will find your server key.
All right.
So.
Here on the clock, you can get your server.
Q OK, so if you download my code and of course you want to replace this server key.
That you have their.
All right.
The next step that we will need to take is to create an async task inside of our members activity,
because that's where I want to trigger this whole thing, right.
So once we go to the members activity and we add a new member, that members should be notified.
OK, so let's go over there.
Let's go to the activities here and then members activity.
And I'm just going to create an amazing task at the very bottom here and in a class this time so that
I can use it inside of this member's activity.
So private in our class, send notification to user async.
Task, which should be an async task.
Of any void and string.
And then, of course, we need to implement.
Its members, so to sue that.
So here in parliament members.
And hers only to do in background what I'm also going to add is the en pre execute, so on pre execute
and which I'm just going to show the progress dialogue.
Nothing too fancy.
Just before we do that, we want to show the progress dialogue.
And after we do that, so the unposed executed pretty much we want to highlight the progress dialogue.
So unposed execute.
Hide progress, taluk.
All right, if you want to see what kind of Jason log you got there, you can, of course, also use
the log with the result of that post execute.
But now that's implemented in background.
OK, so this is where the magic happens and this is where a lot is going to happen because we need to
take care of a lot of things here.
So first of all, we will need a variable, which will be our result, which will be a string, and
then we will return.
Just result.
At the end, so that's the idea.
OK, but now, of course, result is empty and we need to initialize it well in order to initialize
it.
We need to prepare some more things.
So here we need a connection first, which will be an HTP Eurorail connection, which I'm going to assign
now to.
For now, it's going to be a nullable.
And then what we try to do is.
To set a you URL based on Constance.
That the base euro, OK, so.
Use this concept of the be prepared, which is this basic URL here of consent will oppose consent and
then.
We need to import Eurail today for that, of course, and that's going to be the job on it.
And then we can use our connection up here.
Do hope to zero Soheir connection is going to be your eldard open connection.
As an HTP Earl connection.
All right, then we can assign some settings to this connection, so do we want to do Output's?
Yes.
Do you want to do input as well to input?
Let's set that through as well.
Then.
We can add another sighting to it, which is instant's follow redirects, and I'm going to set that
to false.
So the sets, whether the HTTP redirect should be automatically followed by this instance and the default
values I value comes from the follow redirects, which defaults are true.
So that's why we need to specifically set that to false if we don't want to redirect and then we can
set the request method for this.
Connection.
And I'm just going to say I want to do a post request, so I'm sending information over to the server.
So before we used to get requests to get data, when we worked with Jason and other APIs.
But this time we are actually sending data because we want to send a notification to the server so that
the server then notifies the user's application that it should trigger something or his phone, that
it should trigger a notification.
OK, so we're posting or yeah, we're using this post request to post something and then we need to
set to request properties of that connection.
OK, I'm just going to post those three settings that we need to give it here because they are always
pretty much the same.
So we set to request property to content type and their the value is the application should slash Jason.
So we sent the data is Jason.
Then we say that the char set, the character set should be UTF eight.
So it just tells the server of which type of data the characters will be.
There are multiple different sets.
UTF eight is the most common one and it contains a lot of different characters.
So that's basically the idea here behind this UTF eight then set of property is except with application
JSON.
All right, so we have those three here, and this will allow us to.
Pretty much set post requests.
OK, so these are the default settings, which are pretty much always the same.
Now comes the one that we need to use in order to set the right authorization and everything.
OK, so set request property with constants dot F.C. m underscore authorization.
So what is Afkham authorization?
Well it's the key or this have some key authorization.
But now what is the string that we want to pass to it.
Well, it is our.
Constants that have s.m key.
Is equal to.
Constance, so here again, Constance thought.
I have seen them underscore Savitsky.
OK, so basically we're sending something like.
Have s.m.
He should be the server key that we have here.
So that's the length that we have here.
OK, so we need to pass that as information to the Afkham authorization key, so basically because property
always has a key and a value.
All right.
And the key is the same authorization which we had, which is just the value, the string authorization.
And then the value is a combination of the key and the silver key.
So basically saying.
Key equals and then whatever we have in here.
OK, so this whole party here pretty much set in their.
OK, so this is the information that we're sending over.
Or pretty much setting as our request properties for the connection, so you can see we need to set
up a lot of settings before we can start the actual sending, then another setting that I want to assign
is uses.
Caches, and I'm going to set that to false, so the default comes from default uses cash, which defaults
are true, but I don't want to use any cash here.
So now that we have all of that, we can go ahead and prepare a output stream of data because we're
sending data over, right.
So what we're gonna do is we're going to use AI data output stream and I'm going to call this W.R,
which stands for Writer.
So I'm going to use that here.
Connection dot output stream.
Of course, you can give it a more meaningful name than what I've used here.
So.
About.
Put as one word so you can put it together so that the output stream.
Then.
I will create a chase and request.
Which will be adjacent object.
OK, so I create the new JSON object.
Then I have a data object.
Which will also be adjacent object.
And now I'm going to add information to this data object so you can do that via put, so I will put
something at the position constants dot Afkham underscore key on the score title.
So this is the title piece, so to speak.
So what should be the title that the user sees when he opens up his notification?
Well, I'm just going to say something like assigned to the board.
To the port and then let's just use the board name.
And now you might wonder, why does the name come from?
Well, I want to get that one somebody.
Creates an object of this notification task.
OK, so I want to have the board name, which is going to be a string.
And then I also want to.
Get a token, because I'm going to need the token here as well, which is also going to be a string.
So here, VUL Port name and Wall Token.
OK, so the birth name is used now we use it when we want to.
Right.
The title of the notification, and then I want to also have the message, so.
Put constants that have s.m underscore key message.
And that should be you have been assigned to the new board or maybe just to the port.
By and then they user, all right, so your username will be.
I am assigned.
Members list at the position zero thought name.
So this is the assigner, so to speak, so the creator of the board.
And we've been assigned to.
It by the creator, so assigned to the board, by the creator.
OK, now we can go ahead and add that to the Jason requests, so we just prepared a Jason object here.
The state to object just contains this information and now we can put that data object to the Jason
request.
So we're just going to say put the constants that have the key data to be the data object.
All right, so that's the structure here and then we also need to pass into Tolkan, so put constants
that.
SCMP to.
Which is this one here and the token.
So that is what we are putting in there.
And at this point, we need to use our ritcher here, so use we are not right bytes.
And Boolean, but bites right by its.
With our Jason request.
To strength, so now we make a string out of our Jason request so that the bites can be written and
then we flushed the brighter and we cross the brighter.
As we are sending something to a server, things can go wrong, right?
So I want to get the http result code.
So http result, which will be an integer and it will be connection dot response code.
So whatever the response code is, let's say it's two hundred and we know, OK, everything is fine.
We got the data, if it's four four for example then file not found for example and so forth, or you're
not font or whatever I'm trying to do there.
So now I'm checking if the request code is http url connection dot http.
And you can see these are the different codes.
There are OK response codes.
So these are just.
In the church, as you see, so it's constants, for example, the one that I want to have is HTP,
OK?
And if you look at it, it's the value.
Two hundred.
So then you can check out this file here, your your old connection, dot Java.
You'll find the status codes here, two on one created, one or two accepted and so forth.
So everything with the two is a rather positive one with the three is more of a redirect thing and everything
with a four is just didn't work.
So four hundred is bad request, no internet connection and so forth.
Yep, so that's the.
HTP, real connection.
Now let's look what we want to do if that works out so.
What I want to do is I want to have an input stream that reads from this connection, so vital input
stream that goes and says connection, the input stream.
And then we create a reader based on that, but this time it's a Bufford reader.
With the input stream, reader input stream.
OK, so we need to pass all of this information to the reader so that it can do its magic and then we
need a ring builder.
So what it will do is it will create a bill string out of whatever is given to it.
So string builder.
VAR line string, so we've worked with readers and input streams and so forth, right.
So what it basically does is it gives us the details line by line.
And as these things can go wrong, we're going to have a try and catch here.
So what I'll try to do is to read every single line, just going to say while reader dot reline.
So this will be called as long as there are things to read and the line is equal it.
Just go ahead and append.
The string builder.
So here.
This should be inside of curly brackets, too, has to be happened, the current line.
With a line break, so here I'm just going to use a line break also with backslash and.
And by the way, this may not be known here, so the line should not be No.
All right.
Now let's catch what should happen if something went wrong, so usually we would get an IO exception,
so an input output exception and let's just print the stack trace for that.
And then finally, I will try to close the connection, so here, try.
Input stream.
That close.
OK, because we opened up an input stream and they should never stay open, so I'm trying to close it
usually -- that should work and then if something went wrong, then let's just have an Iowa exception
here as well.
Because that's one of the exceptions that can arise, you can, of course, also use the default exception
here.
All right.
And then at this point.
So after this.
Finally, blog.
That's where we can set the result to be as be thought to string.
And by the way, so here in this health blog.
And that is our statement with the period.
OK, so if we had a good result, that's cool, then let's do all of this magic.
But if we didn't get a good result, what should we do then?
Well.
I'm just going to set the result to be connection, that response message, so whatever the response
message is for us, I will use that.
OK, and then this whole thing here, this whole connection part where we try to even establish the
connection that everything else is inside of a tribe block, right?
So we are still missing the catch block.
Could see this is quite some code that we need here just for this notification.
And that is only in the async task class.
And we still have the other class, which we need to adjust.
So here, catch.
And we can have multiple different arrows here, so it's going to be a socketed time out exception that
could arise time out exception.
And in that case, I'm just going to set the result to Conexion time out, then we can have a default
exception, in which case I'm just going to.
Set the resolve to error.
With the budget, but message, so the error message, the exception message, so to speak, and then
finally in either way.
I want to check the connection and if it exists, then disconnect because you don't want to keep the
connection open, that results in a bunch of errors and on unwanted behavior.
OK, and then for this video, I will just do one more thing, and that is to go to our.
Function where we create the members or what we just say, member assign success.
So here when we have successfully assigned a member, that's where I want to send the notification.
So here I'm going to say sent.
Fiction to user async task, I need to pass in the board details and the token.
OK, so these are the two things that I need to pass in here.
And by the way, it's not the details themselves, it's just the name of the user who is assigned to
do or the of the board detail.
And then the token we get that from the ACM token so users have some token, if you look at it, the
user object.
Has the statement here, FCMB tokens, so we need to see talking of the specific user here and then.
We need to execute that ising task, so DOD execute.
And that's it for this video, because we didn't do any Firebrace magic yet, and that's what we're
going to do in the next video.
So here you can see use and registration to server needs to be implemented and so forth.
So.
I'd say let's have a break here as we just long enough see you in the next video where we are going
to finally finish this feature and then you have a fully working, pretty cool app.
So see you there.