20 Notification device to device | 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 have successfully received the notification and also Implement on click on that notification so that we will navigate to chat screen we have also sent the test notification from Firebase console in this video we will not use Firebase console we will send directly from the device so for that we need the API and we should call that API so let's go to backend first and if you go to Project settings over here click on Project settings you can see Cloud messaging so here we need the API key so This Cloud messaging API is disabled right now so we have to enable it first so go over here manage API in Google Cloud so click on enable over here so it is enabled you can close this and let's reload this so as you can see it is enabled and we have the server key this key is required when we are calling this API from the device so to call the API from the device we can use volley library or oksdp Library so we are using OK Institute library so let's add that so okay HTTP Library go to this GitHub page so here you can read the documentation I'll copy this dependency go back to the project in build.gradle paste it click on sync now we have to implement so to implement the OK HTTP we can read the documentation like this we can Implement so now let's write the code to call the API so I'll close all this and I will go to chat activity where we are sending the message so while we are sending the message we have this method send message to the user when we successfully send the message we'll call a method that will be send notification and we will send message from here so I'll create a method void send notification which will take message so this will take the message and call the API so I will create another method that will be call Api so for this API we require Json object so Json object Json object so here we will call the API so we can copy some code from here if you go over here you can see post to a server you can copy these two line from here and paste it we don't require this public static final import so we have these two object now we will need the url url will be string URL it will be https fcm dot Google apis.com slash fcm slash send you can get this API from Google you can search it over here fcm fcm API URL you can copy this URL from here so once you added the URL we need request body so body will be request body dot create we have Json object so jsonobject.2 string and we'll use that media type Json now we need request so request request new request dot Builder request.builder and we will add the data over here so First Data will be URL that will be URL second will be post that will be our body third will be header so header so here we need the API key so header will be authorization and the key will be Bearer key so that we have to write Bearer space we have to paste the key so go back to the project here we have created this key in Cloud messaging API copy this key and paste it over here after b error one is space you have to give and paste it we have added the key now we'll build so we will get the request object now with this request object we can call so client dot new call request so it will call the API the API will be this URL where we are sending the Json object so first we have to create the Json object also so what will be the Json object we have to pass notification title notification body we have to pass the data that will be user ID so let's create that Json object first in send notification method so here we required username current username we required message and we require current user ID and other user token so we required these things so we require these things so first we will get the current username message we already have current user ID we will get and we will get the other user token so for current username what we will do Firebase util dot current user details dot get dot add-on complete listener so we'll get from Firebase if task is successful here user user model current user equals to task.getresult.2 object usermodel.class from here we will get the reference of current user also we need the other user app same token so from where we will get that so that we will get from since other user we are passing already over here as you can see we have other user user model so let's see what data we are sending so let's go to Android util we are passing these as a user model we are passing this as an intent so we have username phone number user ID let's send fcm token also so fcm token model dot get fcm token Here Also let's set fcm token so user model dot set fcm token intent dot get string extra fcm token so in this way we will get the fcm token in chat activity also so here what we will do now we have everything we just have to create the Json object and call the API so let's add try and catch over here so that we don't get exception while parsing the Json so inside this will create Json object Json object equals to new Json object so we have a standard format to add the data we have to First add the notification we have to add the data and we have to add a token so for that what I will do I will create another Json object that will be notification object new Json object and another Json object that will be data object new Json object first I will add data inside notification object where I would put title so put title title will be current user username because we are sending our username as title notification object dot put body will be message whatever the message we are writing so these two things we require inside notification object so in data object data object dot put we will put user ID this should be the same key what we are adding on Splash activities so it should not be different it should be same key same spelling and same case so it should be user ID and we will send the user ID of my user so current user dot get user ID so we have created the notification object created the data object we just need to add in this Json object so Json object dot put notification as notification object dot put data as data object and third thing is dot put token as to so we have to put the token over here with the name to that means two to whom we want to send so token will be other user dot get fcm token in this way we'll send the notification to other user with our name our message and our user ID now we have to call the API with Json object in this way when we send the message it will call the send notification send notification will create the Json object it will call the API and this API will hit the notification so let's run it and test it it is working or not so I'll minimize one application so I'll go this application on background I'll send message from here from Captain America to high Iron Man send it let's wait for the notification I have not received the notifications maybe our API is not being called so after this call Api on new call so we have to any queue also so here we will write NQ callback method will be implemented we can write log over here to get to know if it failed or response but let's run it now let's keep this app on background let's send message again now we have got the notification as you can see I have immediately got the notification here my username and we have the message over here so Captain America had sent high again I can open it to open this I can click on back and we are in main screen so we'll send from this device Also let's go back on this device and here hi hi cap so I have received the notification so we are done with most of the things in our application you you can create more users and start testing everything in next video we'll change the launcher icon of the application so that this icon will be changed and also while we get the notification that icons will be visible right now we are getting the circle as you can see as you can see we have this circle icon over here we'll change the launcher icon and also that icon so thank you for watching this video If you enjoyed 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 [Music]
Info
Channel: Easy Tuto
Views: 8,452
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, device to device, send notification from device, how to send notification from device
Id: YjNZO90yVsE
Channel Id: undefined
Length: 11min 11sec (671 seconds)
Published: Thu Jul 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.