NOTIFICATIONS - Android Fundamentals

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome back to new video but this video will be about showing notifications which is a way to notify the users about something that happens in your app and it will be a little bit more code in this video but you will need it very often so make sure to watch it till the end I've already set up a little activity layout here which is just a simple button and when we click on that button later on we want to show our notification so make sure to set it up like that then we can jump into our main activity and before android oreo it was actually quite easier to create a notification but since then we need what is called a notification channel and we need to create it first and in this channel we will post our notifications and this channel will hold the configuration we want to have for our notifications so once you've set that notification behavior with the channel you cannot change it afterwards the first thing you don't want to do here for the notification channel is we want to globally define a channel ID and a channel name the channel ID must be unique and it is used to differentiate between several notification channels so let's write well channel ID and set it to channel ID it really doesn't matter to what you said it you can choose whatever you want here and same counts for the name so let's write well channel name and set it to channel name whatever after after that we want to create a function to actually create our notification channel so function create notification channel that function doesn't take any parameters and as I told you before Android or e oh we didn't need to create that notification channel but after we need so we should check if that app is currently running on android earlier or later on art and we can do this by simple by writing a simple if statement here and we can check if build dot version dot sdk end and then we need to import that build here by pressing Alt + Enter if that is greater than or equal to bill dot version codes dot o which is which stands for Android or EO so if that is the case we know that our app is currently running on android oreo or later in this if statement we want to create our channels so we write valve channel and set it to a new notification channel we have to pass the channel ID first then we have to pass the channel name and then we have to pass the importance for our channel depending on which importance level you would you choose for your channel it will change whether your notification is directly shown as a heads-up notification and also if it comes with a sound effect so we have different options here we can choose that by writing notification notification manager that important high importance low importance mean default and so on so I will just use default here I think that will just show our notification but without any sound effects if you want sound effects then you have to choose higher not higher importance then I will actually move this into the next line and call apply on that because we want to do something with a channel we just created and in here we can just modify the behavior of our notifications we post in this channel and just to show you an example what we can do here is we can change the LED light color when our notification shows up so we cannot test this on only on our emulator of course but if you test this on your smartphone then you will see that if this notification shows up and your screen is turned off then your LED light will blink and we can change the color by writing light color and set it for example to color dot red or colored green you can just try wrong with it and we also have to call enable lights and set it to true after we have created that channel we need to create a notification manager which will actually create a notification channel so let's write rel manager and set it to get system service and write notification servers in here and it will insert contact start notification service but just writing get system service and then passing the notification service is not enough because this function actually returns an any object because you can get several different system surfaces from this function and this is a very general function so if we click on our manager and press ctrl Q then you can see this is actually in any object and in Kotlin everything isn't any object everything inherits from the Annie class and because of that we need to cast that get system service return type to a notification manager to actually tell Kotlin that we want to use the return type as a notification manager so let's write as notification manager afterwards and then finally we can go in the next line and write manager dot create notification channel and here we need to pass the channel we created a both and then we of course need to go into our on create function and call that function whenever our app starts so we only need to do this once we only need to create this channel once and not before we show every notification or so so just call this once and on create and we're good to go the next step is to actually create our notification we want to post in our channel we do this by watching well notification and we set it to notification compat dot builder you will need to pass the context so we can just pass this this and we have to pass our channel ID in which we want to post this notification which is just our channel ID we created above here and because this is a build or we can just apply many different functions on this with which we can modify our notification here for example we can set the content title which is the title of our notification and we can just call it awesome notification then we can set the content text which is the description of our notification this is the content text we can set an icon to our notification with said small icon and I actually already included that icon in my project so you can just do it you this are a drawable dot IC star i call it if you don't know how to do this then just go to your drawable folder right click new image asset and then you can add and I can of a pool of many different icons and we want to set the priority of our notification to something don't confuse this with the importance of a notification channel the importance just describes if the notifications in this channel should use sound effects or if they actually should show up in the status bar and the priority just means how important this specific notification here is so if there if we set the priority to low and there would be another notification that has a higher priority and both would show up at the same time then the n-words the in the Android system would schedule the higher priority notification before the lower priority notification so we can change this priority by writing notification compat oops modification compare dot priority high for example so by writing this we make sure that our notification actually shows up and finally what we need to call is dot build because if we would just leave it like that then this would return a builder but we want to we want it to return a notification in this build function if you click on it and click ctrl + Q then you can see this returns a notification here and this is actually what we want here after that we need to create a notification manager again but slightly different than we did before so let's write well notification manager and set it to notification manager comp ad dot from and test the context to it which is this and now we are actually finally able to show our notification that we created above so we want to add an onclicklistener to our button show notification and in this we want to call notification manager which we created the world there are notify there's notified function will actually show our notification when we click on that button and for that we also need to create another idea for our notification these are many IDs and names here I know well let's go up here and create an ID for our notification to differentiate between different notifications and I'll just set it to zero this ID is an integer and this ID is a string so don't confuse them there we can pass notification ID and as a second parameter we need to pass our notification that we created both so let's write not a vacation here and now we are able to run our app and display our notification so let's see what happens when we click on that show notification button and as you can see or it did have a sound but yeah the notification showed up it's our tutorials app with a little star icon and it says awesome notification that's the content text and also if we close our app or minimize it then the notification will still be alive and if we click on it then nothing will happen but normally if we click on notifications the app will open from which the notification is from but we haven't implemented that behavior yet so let's actually do that because you usually want to open your app when the user clicks on that notification so the problem right now is that we are not of our own app anymore when we show that notification I mean when we show it we are but if this notification is inside of our notification bar and we close our app that single notification does not belong to our app and therefore we can't just execute code as we want from that notification bar because actually this notification is shown by another internal app in Android which is called the notification manager so what we've done here in our code is that we got the system service of the notification manager and which is actually another app and we taught this other app to show our notification that we pass to it so that other app is actually not allowed to execute code from our from our app to start our activity for example when we click on that notification and to solve that problem and what has the concept of pending intents a pending intent allows another app to execute a piece of code from our app so we can just use pending intents in this case to execute the piece of code that will open our activity when we click on that notification so if we want to create a pending intent we first need to create a normal intent and you should know how to do that so we write well intent is equal to intent test this has context and as a second parameter we want to pass the activity class that we want to open this is just main activity double : class the Java and after we've created that intent we can create our pending intents about pending intent and set that to task stack builder dot create here we need to pass the context and then we want to call that run after that which is a cotton scope function used when we want to do some additional configuration on that test stack builder in this case and after that we can just return that pending intent that we created in this run block and if you have the same error as I do here if you halt want to create this call retries API level 16 but our account minimum is 15 that function just needs a higher API level than we have chosen as minimum if we want to change that minimum API level we can go into our builder Gradle file module app and go scroll up here to minimum SDK version the current minimum SDK version is 15 and I'll just set it to 16 here sink that Gradle file again and go back to main activity and the error is gone in here in this run block we want to call add next intent with parent stack this will just add this activity that we will open on that notification click this will add it to our activity back stack so we are actually able to click on the back button when we opened this activity from the notification and still be able to go back to previous activities here we want to pass our intent that we created a book and after that we can call get pending intent we have to pass a request code for that I will just pass 0 here and as flags we want to pass pending intent dot flag update current that flag just means that when the spinning engine already exists then we want to update we want to keep it and we want to update its data with our new data and finally one last thing we need to do is to add this pending intent to our notification we created below here so let's just call set content intent and pass or offending intent and that's it we can run one or app now so there we go let's click on show notification then it shows our notification as usual but if we minimize our app and click on our notification then our main activity will open again so I hope this tutorial was helpful for you if so please leave a like and comment below also if there's anything I can improve on please leave me that feedback in the comments that would be really helpful for me to improve on my content and yeah have a good day see you in the next video bye bye
Info
Channel: Philipp Lackner
Views: 38,304
Rating: undefined out of 5
Keywords: android studio, notification, notifications, display, app, development, programming, programmer, alert, kotlin, 2020, ui, tutorial
Id: urn355_ymNA
Channel Id: undefined
Length: 15min 38sec (938 seconds)
Published: Thu Mar 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.