Send Android Push Notifications Using Firebase Cloud Messaging | Android Studio (With Source Code)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video i will show you how to set up firebase cloud messaging in an android application using android studio also i will show you how to send push notifications from firebase and how to receive the notifications on the android application so first let's create a new project here i will select empty activity and let's click on next so i will call it my restaurant application and i will use the java language so this is the minimum sdk that i will use now let's click on finish now the project has been created so let's set up the firebase cloud messaging so let's click on tools then firebase and here we have to click on cloud messaging now let's click on setup firebase cloud messaging and let's click on connect to firebase but here we obtain this error this is because we need to configure the application so let's click on ok then let's click on this button it is called project structure then let's click on modules then let's select app then let's select properties and in the compile sdk version we need to select a recent version and here we need to select at least sdk28 but in my case i will select sdk 31 also in the build tools i will select sdk 31 now let's click on ok and let's wait few seconds till the end of the synchronization now let's click on connect to firebase and here we need to build the application so let's click on build now the project has been built correctly so let's click on connect to firebase in this window we have to create a new project so let's click on add project in this window let's click on continue then in this window we can disable google analytics which is not required so let's disable it and let's click on create project now let's click on continue then let's click on connect and now the application is connected to firebase correctly so we can close this page now we need to add the firebase cloud messaging to the application so let's click on this button and let's click on accept changes of course we need to wait few seconds till the end of the synchronization now firebase cloud messaging is set up correctly so we need to obtain the device registration token so let's scroll down and here we can click on any links which will take us to the documentation website so let's click on this link for example in this page we have to click on setup an android client now let's scroll down and here we need to access the device registration token so let's scroll down and let's copy the source code so let's click on this button now let's go back to android studio so here in the oncreate method we need to paste the source code so first let's hide this area and after set content view we can paste the source code we need to import the missing classes also we can replace the log class with systemout println so we can remove this and you can call system.println instead also we can remove the second parameter then here let's remove this glass and let's use systemat printelet we can also remove this message variable and replace it with the token which is the device registration token so let's remove this line and let's replace message by token also we need to import the toast class and here let's replace the message by token we can also add a description of this token also it is possible to display this token into ned text so let's go to the layout let's replace this constraint layout by a linear layout so let's make a right click then convert view then linear layout then apply we can also modify the orientation of this linear layout so let's select it and in the orientation let's select vertical so let's modify the text of this text view and let's write device registration talk now we need to add a new edit text so let's go to text then plain text and let's drag and drop it also i want to provide this edit text with the full hi so let's go to layout weight and let's provide the value 1 for example now let's provide this edit text with an id so i will give it the id et like edit text token let's click on refactor and also i want to make this edit text multiline so i need to find the input type property so this is input type i have to expand it then i have to select text multiline now let's go back to our main activity and let's create a global variable of type edittext let's call it et token then let's initialize this variable so i will initialize it after set content view then i will fill this edited text with the registration token in the oncomplete method so just here after the toast i will fill this edit text now let's run the application so this is the device registration token that allows firebase to send notifications to this application so let's copy this token then let's go to firebase console so here let's type firebase console then let's click on this link it is firebase console now let's select our application which is this one so let's click on it and here let's scroll down and let's click on cloud messaging then let's click on send your first message so here we can close this notification and to send a new notification we have to provide the title so let's call it for example first test and let's provide the message that will be sent to the android application let's write for example this is my first test now let's click on send test message and here we need to provide the token so here let's paste the token and let's click on this add button now let's click on test but we can see that this application did not receive any notification this is because the notification will appear when the application is in the background so let's put this application in the background now let's send the test message again let's select this token and let's click on test and this time we obtain this notification and here we have the message this is my first test so we can remove this notification like this now i will show you how to receive the message when the application is in the foreground so let's go to the documentation website and here let's click on receive messages and in this page we can see that if we want to receive the messages we need to use a service that extends this class so let's copy the name of this class let's go back to android studio we can stop the application and let's create a new class so let's make a right click on this package then new then java class and here let's add extends firebase messaging service then let's import this class now we need to add the service to the application manifests so let's go back to the documentation website let's scroll down and let's copy this code let's go back to android studio then let's open the manifests file and just after the activity i can paste the source code that i have copied of course i need to remove the java word now let's close this file and let's go back to the documentation website so let's scroll down and we need to implement this method it is on message received so let's copy this source code and let's paste it in the service class of course we need to import the missing classes also we can remove the log class and we can replace it with systemat println then we can remove this parameter also i will remove these statements then i will replace the log class with systemat println then i can implement this method to display a toast to the user so let's copy this method let's call it just here so let's provide it with the from and with the body then let's implement it then let's display a toast so let's import the missing classes and let's run the application now the application is in the foreground so let's try to send a push notification to this application so let's click on send test message and here we can see that the token did not change so let's select it and let's click on test and here we obtain this toast now i will show you how to show the notification and the notification area so let's go back to the documentation website and here let's scroll down and let's click on this link so this is the on message received if i scroll down i can see that there is the method send notification that allows the application to display a notification in the notification area so i need to copy the source code of this method so let's scroll down and let's copy this method let's go back to android studio and let's create a new method called send notification which receives only one parameter now we need to import the missing classes then for the channel id we can provide any value so i will remove this function and i will provide any value so it will be of type string let's import the other classes now we can provide an icon for this notification so let's create a new icon so let's expand resource then let's make a right click on this resource folder then new then image asset so in this window i will select the icon type i will select notification icons then i will select the type clipart then i will click on this button in this field let's write message and let's press enter so we obtain this icon i will select it and let's click on ok also i can modify its name so i will call it ic state notification let's copy this name and let's click on next then let's click on finish so here i can use the icon that i have created instead of this value also i can modify the title so let's remove this method and let's provide a title to this notification for example let's write my new notification let's import the other classes now let's call this method when we receive a new notification so let's copy the name of this method and let's call it and the on message received so it requires only one parameter which is the message body so let's copy this and let's paste it here now let's run the application but first let's stop it now let's run it again so this is the registration token it does not change now let's click on send test message and let's select this token let's click on test so here we have this toast but also we have this notification and here we have this notification with the icon that i have created we can remove this notification now let's put the application in the background and let's send a new notification and this is the notification
Info
Channel: BoostMyTool
Views: 54,859
Rating: undefined out of 5
Keywords:
Id: M7z2MFoI6MQ
Channel Id: undefined
Length: 18min 9sec (1089 seconds)
Published: Fri Feb 18 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.