flutter firebase send notification to specific user

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone assalamualaikum here we'll  see how to send notification to a specific   user or to a specific device now to be able  to do that first make sure that you call this   request permission method which you can call it  anything but in the body itself you would have   this instance Firebase messaging instance and  you save it in a variable and then you call a   method which is called request permission and  after that you check the request whether you   really get the permission or not okay  that's the first step and after that   we have another method which is called get  token now this get token actually gets the   device token because if we want to be able to send  notification to a specific device a specific user   first you have to get the device token which  means the hardware token in this case this is   the emulator token but in real scenario it would  be a real hardware token but this method is same   regardless this is an emulator or a real Android  device or a real iOS device the idea is same   anyway so we get it and then we save it in  a variable which is called M token so here   we save it but at the same time actually we also  want to save it to a Firebase backend or Firebase   console so over here first we check if we have a  collection or not if we have the collection then   we check for a document but document is actually  our user certain user or username and for that   username actually in the document we have a field  which is called token and we save that token in   this field now we'll go ahead and take a look at  this token field and user tokens end user here I   see we have our collection which is called user  tokens then we have a user user 2 which we have   just seen now and that for user we have this token  once again this is the hardware or device token so   this is how you get token and save it so once we  have the token we saved it in Firebase database   and then we are good to go so the last method or  the last step to send a certain notification to   a certain device we have to create a method  the method name is called send push message   now this is the message that does the trick or  work to send a notification to a end user now   of course this method should be get called  on an on click event or a gesture detector   or ink wall or any kind of clickable widget so  in our case this is the clickable button over   here now this button actually inside a widget so  this button is actually inside gesture detector   now here is our gesture detector and inside  this gesture detector you see that we have   this send push message now over here first we  visit our Firebase collection which is called   user token and we get username from our device  now of course this username is like for example   user 2 which is this one typed in over here and  you can also uh type in here like this is a title this is on body like that so anyway so if we type  in the user like this so we get the username over   here and for based on that user we get the token  as you can see it's visiting our Firebase token   collection which we have seen over here so it  comes over here try to find that user for user   we get the token so you get the token then we get  actual the token the token filled and we save it   in a variable and once we save it then we send it  to this method now this method is a taking token   body and title which is to well now in this case  so token would be coming from Firebase backend and   then this is the title and this is the body and  interestingly enough we also have to use a HTTP   post method from our app so this is our HTTP  post method now inside this method actually   we have few sections the first one is it takes  the URI now this is quite important so this is   the URI error end point where we want to post our  message including this user related device token   and then we also have this authorization code in  the header the first one is Json or content type   and the second is authorization now this is the  server key that we have to get where do you get   this server to be able to get the server key first  you have to come over here project overview then   you select your device Android or iOS it doesn't  really matter and then come over here inside this   Cloud messaging and inside this you will get this  server key okay so this is the authorization key   the one we have seen just now over here so this  is the server key but you have to remember you   have to put right after key equal and then you  put your server key and then you are good to go   all right so this is our headers so headers  contain content type and authorization and after   that we'll have a body now the body itself takes  Json information over here as you can see anyway   so inside this we have this map now inside the  map actually we have some settings so we have   data and as well as notification sections okay  now at the same time as you see we are sending   both body and the title body and the title over  here now most importantly we are sending this   message this information to this token now one  second what is this token this token is this   one so because of this token we'll be able to  send notification to a specific device so now   we'll go ahead and click on this button and we'll  see that we get the notification from Firebase as you see we got it okay so that's how you  use that's how you send a notification to a   specific user the core idea is you have  to get this end point then you have to   get this authorization and after that you  send whatever the data you want but at the   same time you have to make sure that you  have the right token thank you so much
Info
Channel: dbestech
Views: 20,596
Rating: undefined out of 5
Keywords: flutter send notification to specific user, flutter tutorial, firebase, flutter, flutter firebase send notification to specific user, How do I send notifications to a specific user on Flutter?
Id: uQs-SnlEUnY
Channel Id: undefined
Length: 6min 3sec (363 seconds)
Published: Mon Sep 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.