‫Hello, my friends, and welcome again.
‫So we had to go to continue with the Firebase Cloud Messaging application in the previous video.
‫We have configured the project and we have set it up with the Firebase console and now we are going
‫to continue with the services.
‫So I create the class that will extend from Firebase Messaging Service and act as a background background
‫like whatever you want, like the job scheduler or it works like a postman, so it will deliver a notification
‫whenever it receives any push, any notification from the firebase.
‫So this app or it will be live or awake from by using the My Firebase Messaging service that will keep
‫the application in in red, in attention and ready to receive any notification displayed to the user.
‫How to create it.
‫We will go to here the Android App Master Coding.
‫This is the FCM.
‫This is the package.
‫I will create a new Java class.
‫I will name it as my Firebase Messaging Service.
‫You see, Android example have been solved.
‫Solved.
‫By this.
‫So I click on that to close the Firebase messaging app, close everything except Firebase message extends.
‫I will tell the Android studio that, Hey, I'm going to use this class as a messaging service.
‫So what we do, we will create some static final strings.
‫So first of all, this is this first variable, private static final string nametag equal to my Firebase
‫message service.
‫And now I will override on a message or received.
‫So there are two types of messages data messages, data messages and notification messages.
‫The data messages are handled here in the on message received method, whether the app is in the foreground
‫or background, where the data messages are the type traditionally used with the GCM, Google Cloud
‫Messaging, Cloud Messaging Notification messages are only received here and on message received method
‫when the app is in the foreground.
‫So we are going to use the remote message or this is the message.
‫This is the message that we are going to get when the user tap on the notification they are returned
‫to the app.
‫So messages containing both notification and data payloads are treated as notification messages, and
‫the Firebase console always send messages and notification messages.
‫Okay, so now let's start receiving these.
‫Messages.
‫I will remove the super and I will start by logging the or v propose tag
‫from
‫any mode or message.
‫To get from.
‫We need to to know from where we have received the message.
‫Check if the message contains a data payload.
‫So check for data payload and the message if a remote message or message does not get data.
‫Dot size greater than zero, then what we need to make log the or log v.
‫Tag
‫message.
‫Data.
‫Payload.
‫Message data.
‫Payload.
‫And the year we put message the get date.
‫Now that's a check.
‫If data needs to be processed by long running job, then it is true.
‫So if true, then what we will make scheduled the job.
‫I will create a method.
‫Method called schedule.
‫Schedule.
‫It's.
‫Joe.
‫Else what we are going to make, we are going to handle now also a method that we are going to create
‫in the next couple of minutes.
‫Now, let's check if the message contains a notification payload.
‫So I told you that we that we have two types of messages that data payload or a notification payload.
‫So here, check for
‫message.
‫Notification payload
‫in the message.
‫So if message dot get notification does not equal to now, then what we are going to make we are going
‫to log dot v tag.
‫Message notification body.
‫What we are going to make, we are going to display this message.
‫Message dot get notification, not get body.
‫So we are getting the button.
‫Now start on a new token.
‫So I will put it here.
‫Under.
‫I would override it.
‫Star on.
‫Or have you.
‫On you token.
‫This is the method that we need to override.
‫I will remove it.
‫And why we are using this.
‫If you want to send messages to this application instance or manage this app or app subscriptions on
‫the server side, send the FCM registration token to your app serve.
‫So here we will start by getting the token logged on token, then token
‫and we need to send registration to server.
‫This is the method that we need to send to use to send that token to the server side.
‫Now.
‫Let's add these methods.
‫Let's create them here.
‫So, private void scheduled schedule.
‫The job method.
‫This will schedule async work using the work manager.
‫You remember the work manager that we have added.
‫So one time work request work equal to one time work request builder and I will create a class called
‫my worker and next couple of minutes and I will use it to build this one time worker request.
‫So again, we are going to use the work manager in order to do the background jobs like connecting with
‫the Internet, waiting from waiting the data to be to be received from the internet and other things.
‫Work manager the gap instance.
‫This
‫and here.
‫Begin begin with
‫walk and in queue.
‫So this is the operation in order to start this job.
‫Now we if we need to handle time a lot allocated to the broadcast receivers.
‫We need to create a Private Floyd handle now and now this method will be handling now.
‫So log the V tag.
‫And short lived task is down.
‫Another method send private void send or registration to server and string token.
‫Here if if we need to become an advanced advanced cloud messaging application.
‫Programmer.
‫You can use these tokens, use these methods in order to implement to send the token to your app server
‫and then see at the server side manage these tokens and you can make filtering, you can make other
‫operations and so on.
‫So now as a basic introduction to these messaging services, we are going to use this as an empty.
‫And we don't need to send any tokens till now.
‫One last or the last method that we need to to create inside this Firebase messaging service class is
‫called send notification.
‫So private avoid send notification.
‫Strange message.
‫But
‫what we will do.
‫This is for sending the notifications.
‫Okay.
‫Intent I equal to new
‫intent.
‫Plus enter to import the package of intent.
‫This the main activity that class
‫I dot add the flags intent dot flag underscore clear, underscore clear, underscore top and pending
‫intent.
‫Pending intent equals pending intent.
‫Dot get activities or get activity.
‫From this.
‫Zero request code.
‫Intent.
‫Or I can put it like high and pending intent.
‫Dot flag one shot.
‫String.
‫These are for.
‫For the channels and for sending the notification to the correct channel.
‫So pay attention with me.
‫Stream channel ID equal to get stream.
‫What from where we need to get the string from or the string the default notification i.
‫The channel id.
‫You are a default.
‫Sound your eye out plus entertaining part of the package.
‫Ring tone manager not get default your AI ringtone manager type notification.
‫So here we are customizing the notification in order to look exactly as a notification.
‫Of the default rate using the default notification compared.
‫Notification Compact dot builder notification
‫compact builder
‫equal to new notification compact dot builder.
‫And here we specify this as the first parameter for the context and the channel ID for the second parameter.
‫Remember, guys think about subscribing to a channel and this will be discussed in the main activity.
‫But I am just giving you an idea for what we are going to make when subscribing to a channel.
‫It's it's very similar to the YouTube.
‫So you subscribe to Channel four, like, for example, master coding.
‫So when the user or the admin of the master coding channel put or publish a new.
‫Video.
‫You will be get a you will be notified for this subscription for this new upload due to your subscription.
‫If you don't subscribe to this channel, you will not receive any new notification when the admin of
‫this channel publish a new app or publish new video.
‫So I hope you understand it.
‫But don't worry, we will discuss it later when we talk about the main activity.
‫This.
‫Four sat small icon r dot adorable dot icon with a gradient or I can put.
‫Not the file
‫the set content type title get string or dot string dot FCM message.
‫Underscore message plus enter to create this field.
‫F c m message.
‫Okay.
‫It's very simple.
‫Now, set.
‫Set.
‫Content.
‫Text message.
‫Body set.
‫Oh, to cancel.
‫To true.
‫The set sound to default sound.
‫Your I the set content intent
‫pending and OC
‫notification manager
‫notification manager equal to I'm casting this notification manager
‫get system service.
‫Context.
‫Dot notification service
‫since Android or your notification channel is needed.
‫So we are going to check if the build version SDK is greater than or you.
‫So if build dot version
‫codes or version both SDK integer greater than or equal to build the version code zero.
‫Then what we will make, we would make a notification channel
‫channel equal to new notification channel.
‫Channel ID channel.
‫Human readable type
‫python.
‫You can put it whatever you want.
‫The name of this channel notification manager dot importance default.
‫Okay.
‫This is the channel.
‫Now, the notification manager does create notification channel and the channel.
‫And when we finish, we specify the notification
‫manager.
‫The notify four zero and notification builder dot build.
‫Okay.
‫This is what we have done in this class.
‫So this method we will use later.
‫And this is for the My Firebase Messaging Service.