How to Send Notification in Android

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] notifications in Android first of all we can go to the resources layout activity main XML part and here we can go to split mode and I'll be changing the parallel to linear layout set an orientation to vertical now we can remove this text View and we can create a button View we can give a gravity sender on the panel layout let's set a text for the button we need to set an ID for the button now the activity main XML part is done we have to create a new activity so click here and go to new activity and create an empty activity now this activity will be used to open when we click on the notification so when we click on the notification we'll be opening an activity and this will be the activity that will be opening so we can give it a name we can click finish now we are having the new activity so let's go to the Activity notification.xml part go to split mode change the parent layout give a gravity and set orientation vertical now we can create a text View and we'll be showing some data that will be passing from the main activity set an ID for the text View we can give a text size now the layout part for the notification is also done so we can give a dummy text right now okay so now the layout part is done so let's go to the activity main.java and here we have to create an object for the button initialize the button we can set an on click listener for the button so here you can see we have initialized the button object and you have to make sure the ID is correct so if you click on control and click this ID using the mouse it will take you to the respective button view so you have to make sure the ID is matching perfect so you have to make sure the ID is correct then you can set an on click listener we'll be calling a function that will be making a notification so for that we can create a function inside the function create an object for the notification comback dot Builder equal to new notification compact dot Builder pass the context give a comma and then we have to pass the channel ID also I'll hit an enter from here so you can see the code properly now we can pass the channel ID also so create a string at the top so you can see I have created a string Channel ID and I'm passing this value so you can pass any channel ID here and we can use that ID here now we can set an icon for the notification for that go to the drawable and right click new Vector assets and now we can click on the clip part here we can choose an icon here I am going to use this icon click OK and we can change the name also click next and finish and you can set the icon like build a DOT set small icon and inside that we can pass the r dot drawable dot then we can give the name of the icon that we just created that is IC notifications and then we can call Builder dot set title set content title and then we can pass a title for the notification then call Builder dot set content text and this will be like a description for the notification then we can set it to Auto cancel so build a dot set auto cancel pass true you can actually call the next function without giving a semicolon so hit an Ender dot set priority and here we can call notification compact dot priority so we are going to pass the priority default right now so you are having priority high priority low priority Max mean so we'll be passing priority default give a semicolon now so you can actually remove this Builder from here and remove the semi colon remove the Builder and the semicolon so you can see you can actually code this like this and let's format the code okay so we can use it like this and after that you have to create an object for the intent so let's import the class new intent get application context give a comma and specify the next activity that is notification activity dot class give a semicolon now we have to set a flag so intent dot add flag and inside that intent dot flag activity cleartop give a semicolon so when we click on the notification we'll be opening this new activity so that's what's Happening Here so we can also pass some data from this activity to the new activity for that you can call intent dot put extra then give a name so let's give it a name data you can give any name here and then we have to pass the value for that name you can see we are passing some value this is the value that we are going to pass to the next activity you can actually pass some values like URL or something that you need to access on the next activity so for example if you want to pass an API URL from this activity to the next activity you can simply pass that here okay hope you understand then hit an end then we have to create an object for pending indent equal to pending indent dot get activity inside that we can pass get application context give a comma and hit and enter so the next parameter is an integer so let's pass 0 give a comma and then we can pass the intent so this is the indent that we have created here okay we are passing it here and then give a comma and then we can call pending indent dot flag mutable give a semicolon after that call Builder dot set content intent then pass the pending intent now we have to create an object for the notification manager so notification manager create an object equal to we have to use get system service inside that pass context dot notification service give a semicolon hit and enter from here and you can see this is showing an error so come before the get system service and we can cast this to notification manager so that's all so here we are simply casting it and after this we have to check if the Android version is greater than Oreo or not and if it is greater than that then we'll be creating a channel and we'll be setting it so for that we have added this much code so you can see here we are checking if the Android version is greater than Oreo and if it is created we are creating a notification channel here you can see we have created an object for the notification Channel and then notification manager this one this object dot get notification Channel and then we are passing the channel ID that we have created on the top okay this one you have to use the same channel ID and here if the notification channel is equal to null we are initializing it with the values you can see new notification Channel we are passing the channel ID and then some description and then we are passing the importance you can see importance equal to notification manager dot importance High okay then you can see we are setting the color and the vibration enable vibration to true and this line is the most important part you can see notification manager dot create notification Channel and then we are passing the notification Channel object so this is the most important part and that's all with this if condition so this is necessary if the Android version is above Oreo and after this if condition we can call notification manager dot notify inside that we can pass zero give a comma and then pass the Builder dot build so here you can see this line is also showing some error so if you hover over this you can see it is saying add permission post notification so this is for Android version 13 and above okay so for that we have to ask for permission on the top also so I will be doing that so first of all you can click here and type all tender and choose the option add permission post notifications click that and if you go to the Manifest you can see we are adding this permission to the Manifest so let's close the Manifest and now we have to ask for this permission so go to the top before the on click listener come here here I have added this much line so you can see we are checking if the Android version is above 13 can see and then we are checking if the permission is enabled or not post notification and if it is granted we are not going to ask for permission if it is not we'll be asking the permission to Grant the post notification so that's what this much does so we have to type this and add it to the top so after this the main activity.java file is completed you simply have to make a call to this function so come inside the old click and call the function make notification that's all now you can go to notification activity and here on the top we have to create an object for the text View we are doing slice the text View so text View equal to find view by ID r dot ID dot then the ID of the text view that was text view data then we have to receive the data that we are passing from this intent you can see we are passing some values here so we are using the put extra and we are passing this value so we can access that value here like so this is a string value so string data equal to get intent dot get string extra then give the name that we have used so if you go to the main activity you can see the name that we are using is data so use this name here so now you will receive the data that is this data that we are passing from here on this variable okay so now we can set that to the text View for that text View dot set text pass the data so that's all so now we can try running the application you can see that the app has opened and you can see it is asking us to allow for notifications so let's click allow and now we can click on make notification so you can see the notification is working so if you click on the notification it will open the activity that we just created so this is the new activity and you can see the data that we passed so some value to be passed here and if you go to the activity Main you can see this is the value that we are passing okay so hope you understand how to make notification in Android if you like to download the complete source code of videos that we are doing in this channel you can simply come to this URL I'll give the link for this on the description and you can come to this website and choose the project that you want to download and you can simply download the complete source code from this page so hope you understand how to make notifications in Android if you are having any doubt leave a comment and I will respond to them if you like this video give it a thumbs up and subscribe to this channel for more such videos
Info
Channel: Codes Easy
Views: 37,878
Rating: undefined out of 5
Keywords: codeseasy, notification in android, android notification manager, how to create notification in android, how to create notification in android studio, how to create android notification, how to send notification android studio, push notification in android, notification android studio, how to send push notification in android, push notification android studio, android notification manager app, android notification manager example, notification manager in android studio
Id: vyt20Gg2Ckg
Channel Id: undefined
Length: 14min 14sec (854 seconds)
Published: Fri Mar 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.