CREER UNE APPLICATION ANDROID #10 ? ENVOYER DES NOTIFICATIONS AVEC FIREBASE !

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
And well, hi everyone is Graven for this tenth episode on creating an application with android in the last part we had seen how to create view lists and raids that allowed us to structure our elements either in the form of a vertical or horizontal list either in the form of a grid with several columns today I propose that we discover together how to create a system of notification for our famous application to do this we will use a service that is called cloud messaging it will simply allow us to create notifications by specifying each time a title content and other more or less optional parameters to notify users of any information so this cloud messaging service belongs to a platform created by google which is called fire base one risks a lot use it in the next videos because it is a huge bank of resources and tools that a developer will be able to use as well in a web application as in an application mobile to give you some examples of free services that are available through this tool thanks to this platform we have for example analytics which will allow to know the statistics in real time of your traffic admob that allow it to make you money by offering advertising targeted at the heart of your application storage and data bez that allowed me to create for free a cloud messaging database that will send real-time notifications etc. there are plenty of other properties now that we've seen the theory let's get down to the practice the first step will be to connect our application to fire base in order to use this famous service of cloud messaging notification to do this we will go to the tools tab and then we will click on fire base at that level it will open to our right a small window on which we will find all services offered by this platform so in my case I visualize is the second service and I will click on the button setup fire base cloud manager it'll be a kind of tutorial that explains us step by step how to integrate this service our application so I reassure you right now we will not link it in full otherwise this video would have no interest however we will still focus on the first two buttons that will allow us to facilitate us the integration of this service without having to do many manual tasks so we will start with the first step we go we will click on the button connect to fire down so at that level it's going to open you a web page you will choose the google account of your choice you go ok well on Android Studio has accessed this google account then you will choose fire base here you are now on the main page of this platform you can go here in the console this is where you will find all of your projects for the moment there is no apart from a demonstration project however you can very well add a new one but manually so if you want to do dynamically without taking the lead you just have to go back to android studio and watch it propose here to now create a new project fire base that has the same name as our application so here we go to do this connect to fire base and it will come to create this project well then we can see that there is a small mistake since the project has just been created it will not be able to synchronize it watch if you go back to your fire base platform and do a little f5 to refresh the page you now have this project if now you retry the experience by logging in to fire base it now tells you that a project is existing and now offers you to synchronize it super the connection is well established second step we will add fire base this cloud messaging to our application to do this we will click on the button in question here is look at it we propose to add different lines this is called dependencies these are just external components to our applications to our program that will allow us to access new features so we'll accept the changes here if you will avoid making the sync changes manually is in progress And here we can see that the dependencies have been correctly installed well let's move on now to the second part of this video it will consist of recovering the event when our application will receive a fire base message if you go on your panel and then on your project you will have here all the services that are at your disposal and if you go to the end in cloud messaging this is where you will be able to send your message this message will include a content label and other parameters and when we are going to send the message it will happen is that it will be sent to the cloud and the goal would be to intercept it from our application to be able to subsequently display it as notification to our user so to do this we will go to the manifest file reminds you this is the place that allows us to configure our application and its different properties for example the icon the name the theme etc etc this is also the main MainActivity activity that is triggered when our application launches so how to receive this message from fire base and well it's very simple here we will have to use what is called a service it's going to be just a component of our application that will be able to run in the background that is even if your application is on standby we will still be able to do things so to designate what will be the triggering event we are going to have to create here what's called an intent-filter and specify here what action filtered so the action is named com.google.firebase.messaging_event like this so what will happen is that as soon as our application will receive this event automatically she will call or services which will be called myFireBaseMessagingService we will add a small dot in front exactly like we did for MainActivity which will avoid us specified the entire package and given that this class where this service does not exist yet we will come create it you're going to click on it and make an alt entry move and we'll create this class is here instead of inheriting a simple service from android we will inherited a FirebaseMessagingService if you want more details on the concept of inheritance I refer you to the video that deals with this subject to be able to complete this notion on your side and if now I write onMessage it will offer me the onMessageReceive that will trigger when I receive a message from firebase so we will start by retrieving this message by creating a string variable that I'm going to call myMessage and it will be equal to remoteMessage so that's the element I will have retrieved remoteMessage.getNotification (); which will contain all the information remoteMessage.getNotification () getBody (). which will allow to recover the body of the modification so the content so I propose to do now it's going to be here to make a small Log.d that will allow to display in console this message so what I'm going to do is put here a little tag it's going to be some kind of keyword that will get it back from the console as we might have a lot of things so I'll call it for example firebase message commas you just received a notification more ejected here the value of the message well we will be able to test a little bit of everything so you will start by launching your application we will also be able to choose between a physical device which you can very well connected your android phone taking care beforehand to have enabled developer mode and usb debug mode in the settings or then you can take an emulator here, but it does not exist by default but we will be able to create a new one so I'm going to pick up a phone for example a nexus 6 I made next we will take a relatively recent API level for example the 26 why not I do next, I'll rename it in test emulator and I do finish we can do ok and it will launch it well our emulator is finally ready we will be able to go in the logcat it will contain the log of our application you will choose here the emulator of your choice so the one that is connected the package is so fr.gravenilvec.notifapp in my case here we will choose the debug mode and we will filter by the name of the tag that was put earlier by FirebaseMessage so for the moment there is absolutely nothing but we will be able to test a sending of a first message to see if it works properly so you will return to the panel of earlier to write a new message at the level of the content we will put for example hello everyone is graven the wording is optional so we will not put it distribution date right now segment of users one leaves by default the application it's going to be the name of the package we can then add other parameters a little more precise if we wish and then send a message and we will send it's gone the notification has just been sent and look at the console I have you just received a notification to hey hi to all and graven so it works perfectly well now let's move on to the third and final step of this video it consisted of visually displaying this time our our notification that we will create as and when adding an icon actions etc for the moment we managed something console but it should be that his record visually in the notifications of the user or you agree with me at that level so the first step is going to be here to create a notification so for that I'm going to create a new type variable NotificationCompat.Builder it will allow to build a new notification so i'm going to call it for example notificationBuilder my variable this will be equal to a new instance of NotificationCompat.Builder and I will take here two arguments the first it will be the current context that I recover thanks to the little keywords this in java that target the current object commas and here we'll put along the canal so we'll do here alt entry and create constant field and I put here the name of this channel I'm going for example called MyNotifCanal for example we will now be able to skip a line and make a move notificationBuilder.setContentTitle (); which will allow us at first to put the title of the notification so I'm going to call it for example my super notif We skip a line here we will make a notification shotBuilder.setContentText (); who goes this time allow to put the content that we have already recovered thanks to the myMessage variable which retrieves the content of this notification, therefore the body of this notification so I will also simply put myMessage we jump from line and here we will be able for example to be an icon which will make it possible to represent the notification in itself so if you do not have an image you can go to the site flaticon which includes a lot of image interesting us for example I typed a bell for a bell I for example here take this alarm we will download it in png under the size 32 pixels I'm downloading we go for it slipped on the desktop now that the image is ready we will be able to add it to our application so you open the resource file right click on drawable then show in explorer that will allow to open the exact path so we will go inside and slide here our alarm.png super I will make a special video on how to make the responsive for the images to adapt to different screens so now to add this icon we will make a notificationBuilder.setSmallIcon (): and put here in R.drawable.alarm resources R.drawable.Alarme like those we can also see the small preview that shows us the icon we just chose super we will jump line and now will be concerned about sending the notification to do this we will need a new variable type NotificationManager that will allow to manage all the notifications of our application so I will give him for name for example notificationManager and it will be equal to getSystemService which will allow to recover the service of notification called a NOTIFICATION_SERVICE like this for now it returns us an object but if you click on it and you make an entrance alt hit it you proposes to cast it in NotificationManager since we are sure that this is the type of service now that we have it we just need to make a move notificationManager.notify (); here we will put a number for the notification would conclude for example and we will put the notification here constructed by doing notificationBuilder.build (); which will allow to build it so at this level we could say it's all and you are rather right however from the version oreo the notifications requires what's called a channel is simply a channel in which will pass the notification in question so what we will have to do is here one of the new conditions to check if Build.version.SDK_INT so the current version of android sdk is greater than or equal to Build.VERSION_CODE.O which corresponds to the API 26 so android oreo is a relatively recent version so it is normal that we take it into account so at this level we will create three variables the first of type string it will be the channelID so the unique identifier of the channel so for that I made reference to the files String then to the resource which will have as name notification_Channel_ID then i'll do a second variable channelTitle which will recover from the file Strings the value R.string.notification_channel_Title and finally string channel_Description which will retrieve from the strings file the value R.strings. notification_channel_desk so for now these three values ​​do not exist so we will come create them for that nothing more simple you will click always value in question make an alt entry and create value we will put it as name for example myCanal did ok the second value alt the entry we'll call it myTitle and finally the description alt entry This is a test channel perfect so if you do a check and you click the question element you will see that we have the three strings that were added in the file strings.xml which is in the file super values ​​so from these three values ​​I can create a new variable type NotificationChannel that will create a new channel new NotificationChannel we will first put the channelID ID the name channelTitle and finally the importance it will be NotificationManager.IMPORTANCE_DEFAULT we can do like that at different degrees of urgency of notification being a line here we're going to make a move to be channel.setDescription (); so that's the description here we will use the notificationManager to create the new channel we just made at this level and finally for our notification we created just above we'll do a setChannelID to say we want to refer to this new channel to send our use inside so finally what we must remember is that we can create different tunnels in which will pass notification who each have a name a title and a description and we can give them degrees of importance or not just So we're going to test that so our application is well started I can and create a new message and I will put them as value for example yo the devs how are you going i choose the package sent a message sent attention is gone and hop you can see my super notives yo the devs how are you and that's good added so it works perfectly however for the moment when I try to click on it nothing really happens because there is no action to associate so what I propose to do at first it will be redirected to a web page for example http://graven.yt so to do that we will not start creating a new variable type Intent it's just a new action so I'm going to call it intent this would be equal to a new instance of Intent that will take two parameters the first it will be an Intent.ACTION_VIEW to see a page, and here we will put the URL address of the site which will have to parser thanks to the URI.parse and we can put here our chain http://graven.yt like this so here I come to create a new action that will redirect to my website now that I have this action we will recover the current action by creating a new variable of type PendingIntent that I will call pendingIntent it will be equal to PendingIntent.getActivity (); the context of the application always with the this keyword which target the current object in java here we will put zero that will be the requestCode we will not touch that third parameter it will be here that we will put our action that we want to perform and finally the last it will be flags so optional components so now i've created this action here he'll have to add it by doing a notification shotBuilder.setContentIntent and the action that will be associated with this notification will be pendingIntent like this we will test it's gone so that's gone I'm going to create a new notification that will do as message gosur my site we see on the package sent a message sent it is gone we can see that the little icon has been added so the notification has arrived and if now I click on it look at it offers me to open chrome I chose this option and so here we can see I have been redirected to my web page since this famous notification so it works perfectly I will present a second action that can be done by clicking on this notification always with the intention it is for example redirected towards a new activity so I started if not to create a new activity virgin that I will call notifActivity we will create it I'm going to open in my layout ActivityNotif we will do here a Linea Layout we delete the automatic resource and we will do here a TextView that will wrap the content inside so that I had already shown you previously and as text we will put here from our file strings the resource that will be called welcome_notification_page so we will make a move alt entry and we will create the value and I put here welcome the notification did ok super and now we will be able to redirect to this activity which is named notifActivity to do that we will remove the arguments of intent we are going to put the first one which is the current context so getApplicationContext and the second parameter it will be the activity targeted notifActivity.class it's gone we test it I create a new message novelty on the application we see it on notifApp is gone and if now I click on look I have been redirected to the new activity so it works perfectly last thing for those who wish to add a vibration effect when receiving a notification it's possible you will start by going to the manifest file and here we will add a permission by doing use-permission which will be called vibrate simply to allow this mechanism if you want for security you must also authorized internet since it will be necessary to receive the notifications here we will create the vibration we will need a pattern that will correspond to the rhythm of this vibration so for that I will create a table long I'm going to call vibrationPattern this will be equal to the following values ​​I'm going to put for example 500 and 1000 so there will be two strong moments in our vibrations and now that I created this pattern I just have to make a shot of notificationBuilder.setVibrate (); and we will put and it's the pattern like this so here is a little bit what I wanted to show you today in any case I hope that this video was quite complete for you if it is the case do not hesitate to leave a like below of the latter commented if you have any questions to share if the heart tells you in any case it was serious ciao to all and the next
Info
Channel: Graven - Développement
Views: 64,651
Rating: undefined out of 5
Keywords: java, video, vidéos, graven, gravenilvec, programmation, informatique, creer une app android, créer des notifications android, tuto android fr, creer app android fr, notification firebase fr, firebase cloud messaging fr, envoyer des notifications android
Id: XreH_WToNNU
Channel Id: undefined
Length: 20min 34sec (1234 seconds)
Published: Sat Jul 21 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.