Push notifications and Firebase data messaging in Android 2020 | Maid Rondić | Skillshare

Playback Speed


  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Push notifications and Firebase data messaging in Android 2020

teacher avatar Maid Rondić, Professional Android developer

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

5 Lessons (36m)
    • 1. Introruduction to the course

      1:12
    • 2. What is push notification and why we use it

      7:27
    • 3. Integrating Push Notifications

      12:57
    • 4. Sending custom data messages with Postman

      11:43
    • 5. Subscribing to a topic and sending data

      2:25
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

31

Students

--

Projects

About This Class

Hello people!

In this course you will learn what are push notifications, how to integrate them and use them in your own projects. You will learn how to send push notifications both from firebase and postman. 

You will learn how to send data message that will not trigger showing notification on user's device and you will still be able to make some action in the background according to the data sent in JSON.

Application doesn't even need to be running in order to trigger some action from backend!

Meet Your Teacher

Teacher Profile Image

Maid Rondić

Professional Android developer

Teacher

Hello, I'm Maid.

I am mobile and backend developer with Chemistry R&D background. Coding and puzzle solving geek. Passionate about learning and sharing knowledge in the best possible fashion!

I have experience in many different technologies, platforms and programming languages including java, kotlin, php, python and more.

i tend to create "straight to the point" courses and keep them boring as little as possible.

See full profile

Class Ratings

Expectations Met?
  • Exceeded!
    0%
  • Yes
    0%
  • Somewhat
    0%
  • Not really
    0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Introruduction to the course: Hello, everyone. Welcome to the course on push notifications and fire based cloud messaging. So let's first see what we are going to build. So we'll build some application called Push up demo. As you can see, we now removed it from the task so it is close to and we will know, send some notification to the device. And even though the up is closed that you just saw, we will receive notification right here like this, and we have sent some tackle, some description and an image as well. So once you click on this notification, it will open the up, and you will also learn how to handle data from the modification so that you can trigger some specific action that you want to do. You'll also learn how to send those kinds of requests with post mint so that we can customize, request and send data message instead of notification. So if you like it so far, you can continue watching. See you then 2. What is push notification and why we use it: Before we start any coding, we will first see why do we use push notifications and what they are? Firstly, let's consider this example. Let's say that we have a mobile device right here on. Let's imagine that we're building check application on that. We have server here. We're restore all the data about the user's as well as messages. No, let's had one more mobile device. He, um let's imagine that these user wants to send the message to this one here. So what's going on is that when these user sends the message, it first sends request to the server like this. And then if this device he's connected at the moment, do the server as well. It will get notified about the new message, because if we use fire base as a database, for example, we can set Listener, which will listen. This specific node or child, for example, received messages, and once that field gets updated, it will automatically trigger the event. Inside. The up on this user will receive changes. But let's imagine that this device isn't currently using our app. So let's hated the APP is in the background or it's killed from the task. No, this relation doesn't exist now. Listeners are not observing field messages here. And even though that field gets updated, we can see changes until we open the app, of course. But what we want to achieve is that even though the up is in the background, we want to see some kind of identification or something that will tell us, Hey, something's going on on the server. Go take a look or we want to show some info that specific to the action that just happened on the server. So in that case, we must use firebase cloud messaging or push notifications. And how it works in theory is that we have here something called Cloud and these clouds has both connection with server on A with devices and now this cloud observing changes inside the server. Um, when something gets changed, it will send the request to the device so it will push that request or, let's say, notification that will notify the device that something has happened. This club can push notification, toe all the devices or only to specific ones. So we can determined that by using something called token when these device firstly richest her on the server. It will also send fire based token, and the token will be attached to the specific user. And then when that user receives the message, this cloud will know which user to notify about the message. Because of that token, so far based cloud messaging can be used in many cases, but it's usually used for chat application messages. When we want toe instantly, notify the user about changes or the message is received. We can also use that when we want to send some update about upcoming events or about some feature that we just added to the R AP, and then we can build that notification and send all of the clients in order to let them know that we have some new feature inside the up. So now he's a question. How is our app supposed to get this request If we remove the up from the task when we create our app later on, you will see that inside manifest file off it, we will register service and that service will be in charge off receiving requests even though the APP is in the background so that service will be constantly working in the background. No matter if the APP is running or not on Because of that, we are able to receive notification even though we don't use the app. Let's imagine that we have the app about water drinking and we want to remind the user every single day they should drink water. So we will then write our own custom script and we will deploy it on this cloud and that is called cloud function. And we can set it periodically to trigger you notification every single day, a specific time. And in that way, push notification will be pushed every single day to all of the users and then they will see notification that will remind them toe during the water. We can also target a specific group off users, and for that we use something called topics, so every user can be subscribed for some topic. And then when we send notification from the cloud server, we will say that those notification will be sent only to the users that are subscribed. Toe that specific topic when we send that push notification and when the APP is in the background, user will see that notification up here popping up, and maybe sometimes we don't want to show that notification. We just want to trigger some action to check something on the user's device that's relevant to us in that particular situation. We don't want to send push notification, but we want to send data message. Data message doesn't regard notification of the system, and it will not be prompts to the user so we can do something in the background without even showing the notification to the user. We will see later how to send data messages and how to send push notifications. 3. Integrating Push Notifications: No, let's go and create new enjoyed studio project. We will choose empty activity and for the name of the APP, I will put Bush demo app and click on Finish. No. One build process is finished. Lets go to your Web browser and let's typing fire base. Choose this link over here. No click on Go to the council and let's add new project here for the project name. I will use bullshit demo up. Click on Continue. We will leave Google analytics enables because cloud messaging won't work or device. Go continue. We will choose default and click on Create project. No Cally, continue and let's click on Android and we will use same package name as it is in our interest to you. So let's check. You can find package name right here in Maine Activity. I will cope it basic Here. Those fields are optionals. I won't put anything. Click on Register up. Now we need to download Google Services that Jason file and it's used for configuration that will tell our app how to communicate with this far bays account with this firebase project. So click on it. Find it in Finder Koppett on open 100 studio. No click on this android here and choose project expand First folder and inside APP folder I will face it. And OK, so as you can see, here is something for about firebase u R l about how package name and etcetera. So I will close it now And though click on next and those are dependencies that we need to add Two are up. So I will first copy this one here and now open injury studio. And firstly, let's change this to enjoyed to make it easier. No expand Breydel script and we will choose will open both of these because we will need to add it both of them. So firstly, we will add it. This one here inside those toe curly braces Well, based this class back No, we'll go back and we will copy this one here because the 1st 1 is already included And this is inside these other files. As you can see, this 1st 1 is already included. So we'll just add the 2nd 1 Onda, we need to implement these Firebase analytics dependency and we will add it right here. So dependencies in this file right here Not in the 1st 1 No, we will click on next. Now you can wait to see if the app communicated with fire base. So we will need to sink now. And we will around our application to see if this was successful. No, we can around the up Now let's trick. If it will change, I will minimize this. No app is he stole successfully. But as you can see, we still don't have No, We will wait a bit more to see if anything changes here. Sometimes it just keep spinning on. It doesn't say anything, even though everything is working. Okay, so you don't need to wait for it. Just like on skip this step. Now we will enable cloud messaging. It's a scroll. Or if everything is closed like this, find gross section and then choose cloud messaging. Now click on How do I get started to see dependencies required for our project. Click on android right here and choose Set up an android client and we need this one last dependency. So we'll copied and we'll baste it right here and sink now. Now it's inked. So now we need to create a class cold. My cloud messaging service. Click on OK, in this class must extend far based messaging service. And now we need toe declare a service inside our manifest file. So now we'll Beck. Now we will go back to the Web browser to see how to do it. Exactly. So here is the explanation. So this part here it must be copied, and we will paste it here inside the application tax. And instead of this name here, we will choose name of the class that we just added here. So services basically something that we re just too right here so it can listen for events that can happen in this case, event is sent from the far base and we will know check if it works now, run the EP and wait for it. Now, when the APP is running, we need to close it first. Only in that way we will be able to see push notification. Now let's go back to the Google home and click on Send your first message here we specified title off the identification and let's say test title. This is the body call notification. This is the body or the message on you can see, here is a preview how it will look like on android or IOS. No click one. Next. Here is the target, so we're targeting users here if you click on topic. That means that Onley users that are subscribed to the specific topic will receive this notification. I will show you later how to do that. But what we want toe target All the users that have are up. So let's click on Becky Jemima for next. We will send it right now. We can also make delays or scheduling currently going to review and publish, and I watched the device. As you can see here, is not vacation. And here is the title that we specified and the body off it. Now you can also put some image. Let's see how to do it. Well, let's click on new notification title text and let's find some image. Let's do this fish, for example. Let's open it in new tub. What's copy this link basted here and click on next Knicks on the review publish and wait for it. So here say, scheduled means that it's not yet delivered, but now it is. So here it is and you can see the image and if we collapse here, is here it ease. And if he opened notification, it will bring the up like so So now let's he was going to happen if we send this notification while APP is in foreground or while it is opened so we can just duplicate this one, for example, it will create same notification and just go review and publish. And now you can see nothing happens. And that's because those notification of works on Lee when our app is closed or in background, you can see if we even close it from the task entirely. And if we send the up, what's he was going to happen? It works even though up is close. But problem is, if the APP is running, there is no chance that we can show denotes vacation. So how to deal with notification that when our up is running and what is the purpose of these push notifications? Actually, we will see the next video 4. Sending custom data messages with Postman: the last video use so that we weren't able to display identification. If the APP is in the foreground later, you will understand why it is happening and what can we do about it? No, let's first go inside class that we created. And you can see that there is some warning that we should implement a new token method. So we should say on your talking and press the enter key. So what this method do he's that it register token inside the far base for these specific device for these specific up inside these device. And we can print out the Stoke in here, for example and relate her own can send notification specifically toe a single device. For example, like this when we put title energy vacation, we can click on San text message here and we can add that's talking right here. And in that way, only single device can be notified about something so we don't need to send notification toe all devices. The point is that only first time when the app is run, these new token method is called. So since we already called it now, it will not do anything if we try to do something with this drink. So let's try. Let's try to see this string token, what's printed out inside the council like this. So this should be called when we run the act. As you can see. As you can see, there is no output about this token. So it's because only first time when we run the app this on you talking method is called. If we want to retrieve this token later on, we need to do it manually and let's see how it's done. Let's open main activity to get token. We really use a log statements pointed out in the concert. Let's make time to be talking and to get the token will use far based instance I d get instance and get joke. Now let's run the up. As you can see, we can type in here token. DC's the token for this device. We can compete and we can compete. And here, when we send notification, we can paste this token. Add it now, when we pressed us but first closed the gap and let's do it again, you can see that we have notification. So this is the way this is the way to san notification on Lee toe one specific device. Now let's see how to handle notification message how to get this text from here inside our app, for example. So let's first see how to do it when the APP is in the foreground. So in this class here we can override one more method called on message received and to display this message, we will use log statement once again and we will see no message and remote message dot Get notification, Get but and let's run, dear. So this statement here should this play text which we have written inside this notification text body here. So no act is running. I note that this will only work when the APP is in the foreground when we use far based cloud messaging. And if you go and test and typing message, you can see that we got the message here and remember that it only works when the APP is in the foreground. A notification is not displayed, but we get the message here now Let's he was going to happen just to make sure when the app is closed. So if we send the test again you can see the notifications showed, but no new message has been received here. So why is that behind the scene? One Jason object is being sent to our device from fire base. So to show you how it looks like I will open post mint behind the scene. Jason object is being sent to our device. So I will give you an example how it looks like I'm using postman now. It is used to send a request to the servers to send a request from postman toe the device by using fire base. We need to use this link here. Did you can rewrite And also we need to use out or ization in order to communicate with the fire base. So you need to click on Heather's section for the key. You need to add authorization like I did above and contents type. Now for the authorization you need to write key equals. And this weird thing you can find here inside the fire bays by clicking on these settings, project settings and cloud messaging. And here is the token that you need to copy and based in here and for the content type. You just right application slash Jason Like suggested. And that's it. No, real manually. Right? Jason, that is being sent. It looks like this open curly braces Key is to And here we specify the token the two generated before what we know right it here now we was comma. Now we see notification We open curly braces one more time and we know specify body and we can see some text and we can special file So title title from postman just changes Come No , let's go back to our app And let's see was goingto happen When we sent this You can see notification popped out again entitles from postman So you can see that we send these notification manually from this postman And we didn't use firebase ey from the VIP. So android system is reading this Jason when it when it receives because we specify here not vacation on. That's by default. When we sent it from fire base, Andrew assistant will only handle it on this plate Notification if the up is in a big round And if the APP is in the foreground sees there is identification. It will not display one because by default it knows that user is looking at the up already and there is no need to display notification. But you also seen that is the APP is in the background. We see notification, but we can't get the message from the notification. So how to change it? How together. So instead of these notification, here we will see data and instead of those two will just put our own key and value a pair. So we can say action, Toby, some action. And to receive this message, we will now use remote message. Get data don't get and we use key that were specified here. In this case, it's action. And if you run the up now and send this request from the postman, we will see that this message from here will be shown no matter if up is in the foreground or in the background. So let's send request now. You can see that here in look it, we see we can see the message. And if we closed the up and we send this request once more, you can see that messages received also, you can see that well, that we're using this single key or token and how were supposed to send these data toe all devices since we do it. Since we do this manually, we can specify toe all devices from here. But we can specify topic that devices are subscribed to. So in the next video, we'll see how toe subscribe our users to some topic in order to send them notification. 5. Subscribing to a topic and sending data: now to subscribe device on some topic we will use far based messaging. Don't get instance, not subscribe. Topic. Let's call this topic My topic simple is that and to now, just to remind you, topic is something that we can specify here. If you write my topic here and we go next and Sandy's notification, it will be sent toe all of the users that are subscribed to this my topic. And now let's see how to do it from postman. Because Onley with postman or with other service that can send requests, we can send data cloud messaging instead of notification, and we use data cloud messaging because only in that way we can handle the data in background as well as in foreground. So now, instead of this token just right topic and right, my topic, we need to say topics. And we also need to add slash here and now, with recent, we can see that this message appears to you. We can change it so you can see the difference and send once again. And here it is. And, as you can see up is in the background on. If I opened the up and do it again. You can see it. You can see that We got a message once again. So when we use data messaging, we can get message. It doesn't matter if the APP is in the foreground or in the background. So please play are all notification. You just create Unification Channel on similar things on with data that we get from here, we can simply this plate in our own notification. So that's basically it. See you so