Local Notifications in Android - The Full Guide (Android Studio Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys ando welcome back to new video in this video you will learn about local notifications in android i do have one very old video about that in my android fundamentals playlist but that is quite outdated and i think i can explain that quite better nowadays and with more details i will show you how to update notifications so what we'll basically build is um a basic app you can click on a button and then a notification will show which is simply an increment counter here we can drag this down and then there is an increment button if we click that then we basically just update that notification so just something very basic you need and very many apps you will learn how to do that and i also show you here and there how you could customize this notification so how you could change it how you could maybe add an image here and things like that but right at the beginning i want to make clear that this is about local notifications and not about push notifications because especially beginners often confuse these two push notifications are those that the server sends to a device and that requires some more setup that usually requires some kind of um either third-party sdk that supports that or you use something like firebase fcm but there is a lot more setup involved if you actually want to show push notifications in your app and also if the app is actually um completely closed here you will actually see how you can yeah kind of just show a notification locally from within your app by for example clicking a button or by yeah just some other kind of event that fires could also be that you use something like alarm manager to show a notification for example if you have some kind of calendar app that a notification is shown as at a spec at a specific time but it's just not the case that the server decides when to show this notification so before we actually jump into coding i want to talk about a concept that is um yeah very relevant for notifications and that is called notification channels so whenever we actually want to send notifications or use notifications in android apps we need to create something called a notification channel at least for starting from some kind of android version and i think it's quite an abstract concept so um i actually want to quickly talk about that here what a notification channel is it's basically something that summarizes related kinds of anime and not animations notifications of course and we can see these notification channels in the app settings so if we actually minimize the app here and we hold on it go to app info then click on notifications you will see we have a counter notification so for our yeah basically whoops this notification here or that's not the notification that is the so-called notification channel and for each notification channel the user can actually decide to toggle this notifications off so if i don't want these counter notifications i can turn this off and you can see the notification disappeared you can turn it on and then it would reappear if i click the button again we can click on this and we can also give this notification channel a description as you can see here so this channel is used to display the current counter so the user just knows what it's used for if you for example take a look at other kinds of apps like instagram we go to the app settings go to notifications then you see there are a bunch of more notification channels so these are basically just kind of main categories so for direct messages and if we toggle this on then yeah we have specific notification channels for direct messages for message requests so we can uh very individually turn these off and on if we for example don't want notifications just for requests we could turn these off but leave the rest on and things like that so for each main category of notifications in your app you want to create a notification channel in our app here we actually um want to create just one notification channel for our account notification but that's just something that is required in android and we typically do that that we create a notification channel in our application class which we don't have yet so in our root package you want to go ahead and we want to create something my app for example and that will inherit from application and in here we want to create a notification channel in oncreate so as soon as our application is created that means as soon as the user starts it for the first time or starts it in general that will be called so oncreate and here we want to have a function called for example create notification channel we can press alt enter create this function here and actually implement it as i said that is only required from let me see from build um build version that sdk end if that's greater than or equal to what is it build version codes.o from oreo onwards only then we actually need to create these notification channels because before that they didn't exist so we can say val channel is equal to notification channel and here we need to set that up so first of all each channel needs um an id which we can create a constant for for example i will do that in a separate class that we will also use to display the notifications which i will call counter notification service select class and in here we will use a companion object to just define a constant we can access from all of our project so for example counter notification or counter channel id for example and that can be anything like just counter channel that's just used to identify the channel you want to send a notification into that's nothing that the user will actually see then we can go back to my app and we can pass this here so counter notification service dot counter channel id the next argument will be the name so the name of the channel which will be counter here that is what you've seen in the app settings so which will which basically the main category for notifications which you can toggle on and off and we need to pass an importance so we can define how important notifications are that we send into this channel so there are different levels of importance um some of these only display if you actually um long click on the app icon and most of them actually also show in the in the status bar but depending on your importance they will of course um be shown in a more prominent way to the user like at a at a higher level or so so we can say notification manager a dot important i will choose default here you can choose hi if you have um if they are very important or low or minimum whatever you like so that's it for the channel we can actually also set a description as i said that will be something the user will see so we could say um used for the increment character notifications and that is what will be displayed if the user actually goes to the app settings and clicks on your channel and then to actually create that channel because right now we only created the instance but we also want to tell our actual notification manager that it should create this channel so we first of all need to get a reference notification manager is get system service i'm going to say context.notificationservice and i need to cast that as a notification manager and we can use that to create our notification channel where we simply pass in our channel make sure you actually call this here on create and then we're actually good we just need to go to the manifest and actually add this my app class here as a name in the application attribute so our app actually knows that we have this application class and it will actually use this to call and create when our app is actually launched cool so now our next task is to actually write functionality that helps us to display specific notification for that i want to go into the counter notification service in a real project i would probably write some kind of abstraction for that like an interface that contains a function like show notification but i really don't want to confuse you with that stuff it's totally fine if we just use that as a class here and rewrite a function to actually show the notification we also want to actually have a private valve context in the constructor since we again need to get access to the notification manager which we need to show the notification just like here so we also need this get system service function which we can only get if we have the context here in the my app class um the context already comes from this application but in our counter notification service that doesn't work like that so we want to show the notification with a specific counter and in here what we'll do is we will basically just create that notification so val notification is equal to notification compat that builder and here we need to pass the context on the one hand and on the other on the other hand we need to pass our channel id so it knows in which channel we actually want to send this specific notification we create here which is our counter channel id and now just with a normal builder pattern here we can customize this notification so for example we can set a small icon which you actually need to do for every notification which you can do is by simply specifying a drawable resource so we can go to our ras folder drawable right click and we create a new vector asset i will choose my favorite icon here which is the baby changing station which probably only gets used here in my tutorials and nowhere else uh we can click next and finish and i think it always messes up this tint here because um there is no tint attribute and no color control normal attribute in our themes file so we can just remove this maybe that is not the case for you but you're totally fine removing this you can close this and we can set this icon here to our small icon so our ic baseline baby changing station then what else can we set here we can set the content text um actually is it content title let's start with the title first which is well the title of our notification so we can just say increment counter for example and then we can set the content text which is the description of the notification to the counter or the count is counter just like that so whatever we pass for the counter here we will yeah set the description according to that and there are of course a lot more things you could uh set up here one thing that i want to show you or that i just want to demonstrate or tell you that this exists is set style so if you have been using an android device for quite some time then you will know different types of notifications we will only create a very basic one that displays some text and contains an action button to actually increment our encounter but there are of course also for example notifications that display um like a chat history there are notifications that display an image which you can basically drag down and actually see the full image there are notifications for music players um with like um go to the next song play pause and stuff like that and if you want such a more um specific notification for your use case then you can use the set style function here to actually define the style um for example i think it's a big big text style yeah that's for example a style that is used for for example for emails so if you have the gmail app then you usually get a little preview of an email you got but you can drag it down to actually see a lot more from that email so that would be this big text another occasion and there are a lot more of these specific types of notifications you can all see in the android documentation but i just find it important to mention this that there are these different types of notifications you can actually use here but something you pretty much always want is that you want to define something that should happen when the user actually clicks on the notification because usually you either want to launch your app so the user actually gets to your app sometimes you want to do some other kind of action so how does that work and that is a whole other concept here that we need to talk about which is called pending intents so you might be familiar with intents already an intent is basically kind of a package that defines an intention of the user of of whatever you want to do for example it has a specific action you can put in some specific data like an id or whatever and then send that intent to some kind of component of your or of another app we can define that here for example our activity intent create a new intent pass in our context and pass in our main activity which we want to launch and then we can go ahead and say set content intent and that is basically the intent that will be sent when the user clicks on the notification and you can see it takes a pending intent and we right now only created a normal intent so we can't pass our activity intent here because that is not a pending intent a pending intent is kind of a wrapper around a normal intent that kind of allows an outside application or an outside framework you can say which would be the nullification manager to execute some code of your app and actually send that intent to your app so what we can do is we can say val activity pending intent is equal to pending intent dot get activity so you see we have different options here we have get activity we have get broadcast we have get service so depending on which component you want to trigger when the user clicks on the notification you want to use something different here if you want to send the intent to a service you would use get service to a foreground service you can use get foreground service but since we want to launch our activity and send that intent to activity we use get activity and that now takes some parameters the context at first the request code that's basically just something android will use to kind of identify the specific pending intent i'll just pick one here the intent that we actually want to send which is our activity intent and we can define some flags um let me first of all specify that here and then i will talk about that so we first of all want to check if the build version sdk end is actually greater than build version codes m and then we want to add the pending intent flag immutable and else we don't want to specify any flags so what are these flags used for well they define what should be done with that pending intent in the end so for example if we take a look here there are also some other flags like flag update current that means if you already sent such a pending intent with the same request code and the same kind of data and to use this flag update current and then send another intent which again has the same request code it will simply update the current intent but you would also for example send an intended with the same request code and actually cancel the currently sent intent and there are a lot more of these flags that just specify how how you want to treat it or how this depending intent should be treated by the system we need to use flag immutable here because if we don't do this we actually get a warning and our app will crash since starting from android i think api level 31 or so um the pending intents actually need to have either the flag immutable or flag mutable usually you want to pick like immutable which just means that the um whatever receives this intent cannot directly manipulate that intent which i really don't want to over confuse you with here um usually you're totally fine using this flag just attach it to your intent and then you're good now since we created that we can also pass it here to set content intent and now that works so now if you would click on the notification that we will show we would actually get to the activity what's next next is that we actually want to add some kind of action as i showed you we want to have a button increment and we click on that we want to increment our counter so how does that work well we can say add action and if we take a look here that takes an icon which i actually don't know where it displays that definitely not in the notification uh maybe on some kind of special device like maybe tablets or so i don't know about that actually but yeah we need to provide that we need to provide a title which is yeah the text of the button let's actually just i'll just pass my baby changing station again for the icon and the title will be increment and as you can see we again need to pass a pending intent so for the content intent it was pretty clear what we needed to do since we wanted to launch our activity so we just use get activity and pass this intent but now what we want to do is we actually want to have a counter variable in our app and when the user clicks on that we want to increase that counter variable and it's not as easy as like just passing a call back here and saying like counter plus plus it doesn't work like that instead we again need to specify a pending intent that we send to a component of our hub and that component needs to increment our counter and the easiest way that can be done is using something called a broadcast receiver so you can kind of see this action as sending a broadcast sending some kind of event which this receiver can actually receive and then trigger some kind of code in our app so to do that i actually want to go to a root package and first of all create a counter object which is simply singleton that contains the counter value so we can easily access this from all of our app and then we're going to go ahead and say uh counter notification broadcast or just count a notification receiver which will be a broadcast receiver you can see we actually need to implement a function which is called onreceive here which will be triggered when this this specific receiver actually receives the pending intent we fire we can rename these fields here first of all and we can rename this intent and in here we actually want to increment our counter and update our notification so we can create an instance of our notification service counter notification service because that's what we use to actually show and update a notification we pass the context actually let's make this non-nullable that will not be null here then we can use this service to show a notification and here we need to pass our new counter so we can simply increment our counter.value and pass the incremented value to our show notification function and that's actually how it works to update an existing notification we just call show notification again here and later we need to make sure that the idea of this notification is actually the same of the notification we use when we want to update that so that's how android will actually kind of realize if we want to update an existing notification or if we want to show a new notification in that case we would need to create a new id but here in this class since it's all about the same notification we actually want to pass the same id but we'll get to this point here later in this function so we can go ahead and say val increment in 10 for example here we actually create a pending intent again and this time we say get broadcast because we want to send this to our broadcast receiver so we say get broadcast again pass in our context give it a different request code than our other intent here so these two can be distinguished and we create an intent which we create using our context and our counter nullification receiver class and then we can again include this line here for the flags and we are good so now we take this increment panning intent and pass it to our action and since that will then be fired it will trigger our counter nullification receiver it will trigger this on receive function and here that will trigger our show notification function again with our updated counter value something we need to make sure if we create a broadcast receiver is we need to go to our manifest and actually register that here right below this this activity by using a new receiver tag here and adding our counter notification receiver cool and then back in our service we now created this now created this notification um there are of course more ways to customize this there is also for example set ongoing which you might want to use then uh you the user wouldn't be able to swipe this away which you might know from some apps and yeah there is a lot to experiment with you can set a sound you can i think you can also set some kind of light somewhere so if the device actually supports a specific um actually has an led light which will blink you can also set the color of that you can set a number if you have some kind of chat for example and you say there are five more chat messages you haven't read there's a large icon and there's a lot more to experiment with here i highly encourage you to do that to actually just try out what you can do here i will just cover the basics here after add action we can call that build and now we need to take this notification and simply pass it to our notification manager so to [Music] here we actually haven't gotten an instance of that yet we can simply get that here private val notification manager is context get system service context dot notification service as notification manager like that then we scroll down and we say notification manager that notify that's the function that will actually show the notification and as i said we actually need to pass an id here of the notification i'll just pick one you just need to make sure if you i want to update that notification you need to use the same id since we only use one notification here um using one as the id will always refer to the same nullification but if you show multiple you actually need a way to actually yeah reference the specific notification you want to update so if we have one then yeah simply pass the notification here and i think that is pretty much it we of course need to go to our main activity and in here we want to simply have a box if you use xml simply add a button and then on click listener we can say fill max size and just add a button here when we click this button we want to actually have our notification manager here actually our notification service let's just call it service is counter notification service pass in and let's actually use the application context here and when we click on our button here and on click we want to say service show notification with our counter.value we can give this button a little text of let's say show notification and that should pretty much be it if we now launch this on my device and switch back to that device wait until the build is actually finished and we actually hope that everything is going well looks like that it's launching okay i didn't center it but doesn't matter if we click the show notification button then you can see there is our notification it says the count is zero and if we click increment then we can actually increment that counter so i hope that got clear i think especially the stuff with the pending intents is or can be quite confusing as a beginner um again it's just kind of a wrapper around a normal intent that the notification manager will use to actually send to some component of your app and the component can be an activity can be broadcast can be a service can be a foreground service or something similar which then will receive this intent that you pass to the pending intent or this intent and actually do something with that if you would want to attach some kind of data to that intent you send for example to the broadcast receiver you could also do that here using dot apply and then oops using put extra you can say my extra and for example attach some kind of value like any integer or string and then that would be attached to this intent which will be sent to the receiver and here you get a reference to this intent as well so you could yeah retrieve that extra again that is how you would attach some kind of data i hope that helped you to understand notifications on android a little bit better if so and you are not a subscriber of this channel yet then i would be very happy about having you as a new subscriber so we can maybe reach the 100 000 subscribers at the end of this year that would be amazing yeah other than that i wish you an amazing rest of the week enjoy your day and see you back in the next video bye you
Info
Channel: Philipp Lackner
Views: 47,752
Rating: undefined out of 5
Keywords: android, tutorial, philip, philipp, filipp, filip, fillip, fillipp, phillipp, phillip, lackener, leckener, leckner, lackner, kotlin, mobile, notifications, notification, manager, update
Id: LP623htmWcI
Channel Id: undefined
Length: 27min 27sec (1647 seconds)
Published: Wed Aug 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.