Welcome back and this video, we are going to start creating the notification functionality in our application
and therefore.
I can recommend to check out the cloud messaging slash Android slash client to understand better how
to set up a firebase cloud messaging client app on Android, even though we're not using a messaging
app.
But it's still useful when it comes to using notifications.
So whenever you want to use that, the first thing you need to do is to add to gradle settings or entry
here.
So inside Bill Gradle.
For the upgrade of module app here, we need to add a new implementation here, which is going to be
the following.
So implementation combat Google or Firebase Kolan Firebase Monist messaging Kolan 20 21. that one is
the current version and my case and we need to sync at once.
We added that.
Even though, of course, the version might change, so here, if you check out this documentation here,
you will always find the latest version.
And then you will need to edit your manifest, so you'll need to add that to your manifest, even though
in our case we will need to make some adjustments.
So let's go over to the manifest file.
And let's at the service here inside of the application.
And for some reason, I didn't copy the starting service part, so let me just give you the final version
directly.
So here this is how it's gonna look like.
I'm going to call this FSM, my firebase messaging service.
Android exported this set falls and then this is the action that we're using, so Android called a name
called Google Dot Firebase Dot messaging on this core event.
OK, so now.
We will also need to add some metal techs to our android manifest here and what this area will disappear
as soon as we create this class.
Here is my firebase messaging service Cross, and we'll do that in a second.
But first, let's add some metal techs and metal techs.
We can add them at the very top.
So here.
Metuh.
Data should be, for example.
The one that you can find here.
So basically we can also just copy that.
And posted to our manifest here.
And then this one is optional.
Let's put that one in there as well so you can just click the copy button and then let's add this metadata
and here as well.
OK, so then you will have these three metadata text with their information in there.
And at that point, we can create a class that is still missing and why we get this error.
So here, let's create this class inside of the folder, which we will also create, because you can
see already it says here that it should be the ACM folder.
OK, so let's create a new folder or a new directory in our project here.
Right, click new package Afkham.
And.
Inside of that, we want to create a new content class, which I'm going to call home Firebase Messaging
Service.
All right, now let's look at dendrite manifest, the error seems to disappear.
Even though we need to make sure that this class is inheriting from.
The firebase messaging service.
So basically everything here except to my.
And that's important as well.
Now we need to add brackets here towards the end and implement our part based messaging service class.
Now there are, of course, multiple methods that we can use here and override.
So let's start with the on message received.
OK, so on message received.
Here, I'm going to rename this one to a remote message because I don't like the name.
So here.
So it's a little more consistent, just p0, it's not very useful.
So when we get the message, this function will be called.
So there are two types of messages.
There are data messages and notification messages, and the data messages are handled here in the on
message received, whether the app is in the foreground or background data messages are the type traditionally
used with GCM.
Whereby GCM stands for Google Cloud messaging notification messages, however, are only received here
and on message received when the app is in the foreground, when the app is in the background automatically
generated.
Notifications are displayed when the user taps on the notification.
Then he's used returned to the application and messages containing both notification and data payloads
are treated as notification messages.
All right, the firebase console always sends notification messages if you want to know more than check
out the firebase documentation for that.
So thing is, if you want to handle such a message.
You can add a log entry here.
So log D with a tag.
Messages from Colin.
Dawla.
Remote message thought from.
OK, so if you want to know where the message comes from, it's important log here, it's import tag
as well.
So here is the way where we can now use to or retell remote messages, then we can also check if the
remote message.
That data is not empty.
Then.
We can also have another log entry, so, for example, if it's not empty, we can just say something
like message, data, payload.
And then we split their remote message data, so remote message dot data.
Then we can check if the message contains a notification, so remote message dot notification.
Tartlet.
And here, the notification is another blow and you can also have another long statement, so let's
copy that.
Message notification body.
And here we can get that by using.
EITE.
Taut body.
All right, so it is our value that is given to us, so the remote message notification.
So you can see that here, this great out area.
All righty, so that is the function on message received then.
We can add another and new token override, so here, this one, p0 will be the token that we need to
pass.
So let's replace the stupid name here with token so called if the instance ID token is updated and this
may occur if the security of the previous token had been compromised.
Note that this is called when the instance ID token is initially generated.
So this is where you would retrieve the token.
All right.
So if you get a new token, well, that's the function that would be called.
And then here we can run a little log entry.
So let's say we want to know about the new token.
Then we would just refresh token.
And here is where we would send the registration to the server with the new token.
OK, so if we have or should create a new token, well, that's where we would then also want to send
the following function.
So private fun, send registration.
This is one that I could get myself.
So this is not an override here.
Registration.
To the server and you, we need to pass in the token, which is a string nullable.
OK, so in this case, well, we need to implement the functionality to send the registration to the
server and so forth.
But in the new token that we would call this function OK, and we need to pass on the token.
Like that.
All right, and at this point, I realized that I imported tag, but I would like to use my own tag.
OK, so I don't want to use this content values tag.
I want to have my very own tech, so if you want to use your own tech, what you can do is you can,
of course, create a variable which is called like that, so you can go ahead and create a companion
object, which you call tag, and I'm going to call my tech my firebase message service.
So then if I do that, of course, I want to get rid of this constant value tag so that I don't have
any interference here.
OK, so the tag for the log should be the tag that I have here and my companion object.
OK, and then.
There is one last function that I want to show you in this video, but as you could see, people really
need to dig deeper into it later on.
So I will create a new function here.
Private fun sent notification with the message body, which is of type strength.
So if you want to send a notification, this function should then be called.
So I'm going to do that is with an intent going to say, OK, let's create an intent where I need to
pass in the context, of course, or that's important here real quick.
Then I will pass on the activity, which is intent should be sent.
So main activity.
So the user should be sent to the main activity once he clicks on the notification as the Java.
And then.
I will add a flag to this or add flags, which will be intent, that flag activity clear top.
So activity.
Clear top this one, as you might remember, this is how we made sure that specific activity is said
to a specific position inside of the stack.
So, for example, if we have two activities and we want to put one at the top, then we would give
it this.
And here.
OK.
And then has the notification is something that is sent to the user and you might be in another application
and we cannot just open an intent from the other application to our application, we need to use a pending
intent.
So here what we can do is we can say pending intent, which should be pending.
Intent does not get activity and let's import betting and therefore so here.
Get activity, and he had a single bullet to pass in the context.
Which is going to be this.
Then the request code.
And the intent and finally the flags and here I'm going to use pending intent, not flag.
One shot.
And this flag says that this activity should or this intent should only be usable once.
All right.
And by the way, if you want to know more about planning and tents, you can check out here, there's
a good description.
So basically, the idea is that you should or by giving a pending intent to another application, you're
granting it the right to perform the operation you have specified as if the other application was yourself
with the same permissions and identity.
So you should be careful about how you both your pending application or pending intents and depending
contents itself is simply a reference to a token maintained by the system describing the original data
used to retrieve it.
OK, so we're using this pending intent and then we will create a channel ID for which I'm going to
create a string.
So let me import are here.
And let's create this string, so let's go over to our strengths here, resources, values, strengths,
and let's create this new new string here.
Right, then we can come back to our channel idea.
So now we have this channel idea and we gave it the name, right?
So it's this default notification channel name, whatever we entered here, which is pojman on the court
notification channel.
So I called it and then.
Now that we have the technology, we can add the default sound urai.
Which is the ring tone manager, don't get default your eye.
What should be done notification?
Ring tone manager type notification, so you can see here the type alarm type all the time, ring tones.
So the user has specified what type of notifications he wants to have, for example, for a notification
such as an ASMs.
And that's the one that you want to use.
So we don't want to use a notification that should be used for alarms only.
So the user would be confused why we are using that feature.
And then we can create the actual notification, so here, if we want to create a notification, we
need to use the notification builder and.
Therefore, we can use notification compact dot builder.
And now here for the build up, we need to pass on a lot of information.
Or we can pass in a lot of steps, so to speak, to add to the border.
So first we need to add the context and then the channel ID.
Which is this idea that we have created here.
And then we can set an icon for this notification, so said small icon.
So this is the icon that is displayed in your notification bar.
So you see the little icon.
And here I'm just going to use Aadhar to rubble.
Thought I see.
That I see notification, at least that's how I'm going to call it, so let's create it.
So go to new.
Victor Asset.
And here we can select the.
Android, there's one here, so let's just use the default android.
I'm going to use this one.
As has said, I see notification here.
You can, of course, play around with the color.
So let's use any color.
So it's a little different than the default, the green one.
All right.
So it's going to be this one here in my case.
Then you can set.
The content title, so the title of the.
Notification and just going to say Title four now, of course, you could create a string for this,
but I'm going to keep it simple this time, then the context text.
So the text that you want to display and it's just going to call is one message then set out to cancel
to true.
So.
Setting this flag will make it so the notification is automatically cancelled when the user clicks it
and the panel.
And yeah, that's pretty much it.
Then we can set the sound and that will be the default sound urai that we just prepared.
So this one up there.
And then we can set that content intent, so if the user clicks on it, then it should open up our.
Main activity, so here pending intent is using our intent, which then uses our main activity, so
basically the user, when he clicks on the notification, should be sent to our application.
Now we can go ahead.
And create a notification manager by using that system service with the context.
That notification underscore service and as the.
Notification service saw here as a notification manager, to be precise, so on notification.
Manager.
And then since Android Orio not notification channel is needed as well, so we can check if built.
Thought version thought as Dick and his creator equal build.
Their version codes thought, oh, so whatever the Android O version code is, we need to import build
here.
Let's do that and then we can set the channel to be notification channel.
So we had to channel it, but we didn't have the channels, so now we need to pass in the channel Heidi.
Then the name, so I'm going to say channel.
Pro-gay Manaj.
Title.
And then the notification manager importance, so here, notification manager, importance, default.
So now let's import this notification channel here class so that we can use it and then we can go ahead
and use the notification manager to create the notification channel based on our channel.
Are we prepared?
All right, and at that point, we can use the notification manager to finally notify.
All right, so notify here, we need to pass.
I'm just going to use zero here.
And then our Nottie Frication Belder Dot Pilt.
OK, so this is a very long function, there's a lot of stuff going on and things that we have not touched
on yet, but basically this is how you can prepare a notification.
And when we call this function now, it will send a notification, so to speak.
All right, so this is just a build up and and the next video, we're then going to actually use this.
But first, we will update the token itself.
OK, so see you in the next video.