19 FCM Push Notification | Chat application | Android Studio

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back to another video of chat application development Series in Android Studio in last video we successfully added the feature for profile picture we can upload the profile picture and it will be visible in our chat screen also in this video we are going to implement push notification so if one user sent the message to other user the other user should get the notification of that message so let's get started so let's close all this so to implement push notification we are using fcm that is Firebase Cloud messaging so go to tools Firebase from here we have to select This Cloud messaging click on setup and click on add fcm to your app it will add the dependency dependence is added you can minimize this now we have to create a service class which will extend Firebase messaging so with the help of that service class we'll get the notification so go to our project new Java class and name it as a fcm notification service and extends a class from Firebase messaging service so we have created the service class we have to Define it in manifest so that manifest will know that this service class is of messaging so go to manifest inside this application tag we have to mention the service so service name is equal to fcm notification service Android exported will be false and inside this service let's add intent filter so that we can get data intent filter inside intent filter we have action name will be com dot Google dot Firebase dot messaging event so you have to write it correctly otherwise it won't work so com dot Google dot Firebase dot messaging event so after adding this we are done with manifest we are done with service now we can reset the notification but for sending the notification we have to send it from console so we need the token so token how we will generate will generate token in main activity so whenever the user is logged in and comes to main activity we will generate a token so I will create a method for that here I will create a method that will be getfcm token I'll create this method fcm token so here we will get the HCM token so for that Firebase messaging dot get instance dot get token and you can add on complete listener so on complete listener if we can use Lambda so Alt Enter task dot is successful if we got the token let's say string token equals to task dot get result inside this result we should have token let's just print this token for now so my token will be token let's just print it and run the app so I'll run in both the devices now if you see unlock cat there should be a token so here as you can see here is the token so token is successfully generated and printed so this is of emulator one if you go to emulator 2 you can see the token over here so both the token are generated so now let's send the test notification so that we will get to know either notification is working or not let's copy this one token and go to backend where our Firebase project is there so here we have to go to engage click on messaging so it will add over here now click on create a first campaign click on Firebase notification messages create now we'll send the test notification from here so notification title test title let's say nutrition test will be text test text for now let's just add these two things to receive the notification we have to keep the app on background because while we are in the app we don't want notification we just want it when we are not using the app so if I am not using the app I'll go to send test message here you can add that token so I'll add one token add it and click on test go over the application as you can see we have got our first notification we have application name test title test text so we have the notification since I have copied the token of emulator 2 it is coming over here so our test notification is working fine since our notification is being triggered but we don't want notification sent from this console we want if one device sent the message it should trigger on another device so for that we need the token to be stored somewhere so we will store the token in user so firestore database in fire restore database we have users collection we have this create a timestamp phone user ID and username we'll store one more property that will be fcm token so let's go to user model and create another string that will be fcm token and go down generate getter and Setter click on OK so we have this getter and Setter we can close this user model now we have to store the token when it is generated so we have token over here we let's remove this print statement and directly store that in the user model so for that I will use your Firebase util that current user reference current user details dot update update the current user data with fcm token name will be fcm token and value will be token so it will update the token in the database now let's run on the board device we will go and see in the back end you can see fcm token is generated and for other user also fcm token is generated so now you can directly copy the token from here and send the test notification now let's again send the notification I'll copy this and go to Firebase messenger I'll open it on new tab so that I can access both go to your first campaign notification message here I will say title as title text as text send test notification if you enter if you add fcm token it has already added make the happen background and test so I have received the notification so if you click on this notification it will just open the app it will do nothing it will just open the app if we click on the notification it should open the application then it should take us to chat a screen so that we can directly see the chat whoever user has sent the message so whenever we click on the notification it will first open the splash activity so in Splash activity we will read the notification and navigate the user so what we will do first in Splash activity if the data will get as intent so get intent dot get extras is not equal to null first we will check if we got the data or not it is from notification we will get the data if it is not from notification we won't get the data so I'll put else over here and this Handler I will put inside else so if we are not from notification it will do whatever it was doing so if it is from notification let's say right from notification so here what we will do first we'll get the user ID so from where we will get the user ID use so while sending the notification we will send title as username text as the message and one more data we will send that is user ID so that we will get to know who has sent the message and we will navigate by that user ID so string user ID equal to get intent dot get extras dot get string so I will give the key user ID so from this we will get the user ID of the user so here we will get the user model from user ID so for that I will use Firebase util alluser collection reference dot dot document document ID will be user ID dot get dot add-on complete listener so I will add on complete listener use Lambda so here if task is successful that means we got the user then then we can go to search user recycler adapter so in search user adapter when we click on the any user item it will navigate to the chat screen so I will copy this come to Splash activity here I will paste it we need context that will be this and this context we don't require and here we will get the model so user model model equals to task.getresult.2 object usermodel.class so in this way with the help of user ID we got the user model and we will navigate it to chat activity so let's run it and test it from test notification so we are running the application I will go to background and I will go to this console here first I will take the token so I am taking the token of this Captain America user so I want to send to Captain America so I'll copy the token of Captain America go over here here I will add the token of Captain America so I will not send right now first what I will do I will give the title title will be username of sender sender username will be Iron Man will send the message will be high how are you and we need that data we need user ID so how to send user ID I can copy the user ID of this Iron Man from here copy the user ID of Iron Man and I will come over next select the target as this application our application scheduling will be now and here in custom data we will send the data so key will be user ID value will be user ID that is of Iron Man so I will go to notification again click on test select the token next so if you see I have got the notification over here so here username is coming message is coming if I click on this let's see it will work or not so if I click on it it went to chat screen so our implementation on click implementation is also working but if you notice if I click on back over here it is going to splice a screen because we are calling from splash screen so for that what we will do first we'll go to main activity then only we'll go to chat activity so here what I will do first I'll create another intent main intent new intent from this to mainactivity.class intent main main intent dot I will set flags that will be intent dot flag activity no animation and I will start this activity main intent so first it will go to main activity then it will start chat activity also one thing while we are checking the intent before that we will check if user is logged in or not so here what we will do Firebase due till dot is logged in and end first we'll check is logged in if logged in then only go to the chat activity otherwise it won't now this will start this activity and this activity and also we'll finish this Splash activity so now I will run the application Let's test so both our application running I will keep it on background and send the text message again so I will write here second send text message select token and click on test if you go here we have received the notification if we click on this now we are in chat screen if I click on back we are in main activity so these things is also working fine so one last thing we have to do is when we click on logout button the token should be deleted because we don't want token to be stored after logout also so go to profile fragment we have a logout button over here so here logout button is there firebaseutil.logout what I will do here first I will go to Firebase messaging dot get instance dot delete token so it is a task add on complete listener on complete listener here if task is successful I will cut everything and paste it over here so when we log out the token will be deleted when we log in again the new token will be generated and stored in the Firebase so that's all for this video we have successfully received the test notification from Firebase so next video will not use Firebase console to send the notification but we will send from the device itself so one device if send the message the other device should get the notification so that's all for this so if you like this video please hit the like button if you have any doubts and queries please comment below I will reply for you don't forget to hit the Subscribe button before going see you in the next video bye bye foreign [Music]
Info
Channel: Easy Tuto
Views: 18,088
Rating: undefined out of 5
Keywords: development, beginners, android tuto, Android Studio, Firebase, Chat Application, Real-time Messaging, fcm, firebase cloud messaging, fcm notification, notification, push notification, android push notification implement, implement notification android, how to get notification android, notification android add, fcm push, notification implement
Id: NF0RzhXDRKw
Channel Id: undefined
Length: 14min 3sec (843 seconds)
Published: Sat Jul 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.