Send Push Notifications with FCM using C# (Topics & Tokens)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in previous video we've seen how to implement receiving push notifications in xamarin forms for ios and android in this video we're going to see how to send push notifications from code first let's have a quick look at what we're about to see in this video so you don't get disappointed if it's not in there what you're looking for um so here we can see a little bit of c sharp code it's running in a console application where we create our firebase app and we are going to send a notification through code either through a token so a specific token for a specific device or through a topic so i've implemented it with the topic right now and whenever i click run on this application you can see our sample application is still installed on the emulator and you can see that the push notification is sent successfully and we are here receiving a push notification based on a topic so that is what we're going to see in this video and how to implement it let's go check it out before we go anywhere you know what time it is i have to do a big shout out to a couple of my members it's three this time oh my gosh i can't believe this so you can join this channel as a member for a small fee you will get early access to videos but mostly you know you're supporting the work that i'm doing here and i'm very thankful for each and every one subscriber but especially a little bit more for my members so i need to thank nordic nerd i hope that's not your new name but thank you so much for joining our membership um i also want to thank scott hutch thank you so much for joining this channel as a member and a special shout out to ruth okoboto she's been a member for a longer time but she just upgraded to the second tier which gets you a shout out in this video so thank you very much and let's go check out all about that push notifications right now now like i mentioned there are already videos about receiving push notifications on ios and android so i would very much recommend check these out first because there i set up this firebase project for the first time so i'm going to skip over that for now and i'm going to assume a little bit of knowledge right there so you might want to check those out first but if you did then you know that we've been working with this push notification sample project so this is the firebase console and here you have different kinds of projects you can see some other projects that i've been testing on but this is the one that i'm actually using so the push notification sample and whenever i go in there you can see here that i have on the top the android and the ios app already set up now it doesn't really matter we're not going to look at the actual apps in here that's again something that is handled in the other videos what we're going to do here is go to the little cogwheel icon here which is the project settings and then we go to project settings and whenever we are in there you want to find here at the top the service accounts tab so what this does is it will create a key for you that is a service account so basically an api key with some other metadata inside of a json file that can be used as a service account so whenever you're going to you know access this functionality from the firebase console you can do that through code you can see here for through node through java python go c-sharp is not in here but don't worry that's what i'm going to show you and with that key you can access these services programmatically so that's exactly what we want to do basically now you can have some more fine grade control over the permissions and whatnot for that key so be sure to check out the documentation you can here see manage service account permissions it will take you to another site and you can see all about it but for now you know i'm just going to take the happy path and just click here generate new private key and you can see that it warns me like watch out this is going to give access to your projects firebase services and of course you know we're using a free service right now but this can you know incur some costs on your credit card or whatnot so you really have to keep this confidential and do not ever commit this to a public repository so you know save it somewhere securely and let's just say generate key right here and then you can see that it downloads it to my local device and we're going to you know keep it there for a little while while i set up the rest of the project and then whenever it's time i'm going to add that file and you can use that to communicate with the firebase services so more on that later just remember to do this first so here we are in visual studio 2019 for mac this project is already implemented i'm going to repeat myself a couple of times here i've done that in other videos you can find the links down in the video description but this you know it's running right here you can see the example page on the left in code on the right is running on the android emulator so everything that i'm going to show also works on ios but it's just easier to show it with the android emulator right now and what we're going to do actually this is running and all the code is implemented to receive the push notifications so if you remember from the other videos in the application output it's kind of small probably for you you can here see the token which is the token for this device that token is used to send a message to this specific device so we have that and we're going to use that in a little bit but of course you know sending messages to specific devices might not always be what you want so we're also going to see a little other way to do this now the token is here so that's good uh let me pop open the solution explorer here on the left let me pin that for a little bit so here we have the projects we have our shared project we have our android our ios project and you know just for this sample sake i'm going to right click here on my solution add new project there we go and i'm going to add a little console project right here so a console application let's make that net five doesn't really matter and let's keep the same naming convention so xf for examine forms fcm firebase cloud messaging push notifications dot sent there we go so location looks good let's click create and there we go here we have our little project it just says hello world now doesn't do anything fancy but this is what we're going to use to send actual notifications now this is a console project which you know if that suits your needs that's great but the code that i'm going to show you here might just as well live in your asp.net webmvc web api what's all them things called they can live in any kind of web project you know you can even send push notifications check out the documentation also linked below through like http requests so you can implement it through basically every framework every text tag every programming language that is out there so but i'm going to show you in c sharp code of course but what i just want to say is like you know you can trigger these push notifications in a way that is suitable for your scenario right so you can take the code that i'm going to show here uh make it suitable for your needs and put it in a server application or a console application or do whatever okay so that being said uh we're going to first install a little plugin i love me plugins so let's go to right click and say manage nikit packages and then i'm going to search for firebase admin which is a official package from google so you know they know their stuff this is all from them um latest version at the time of recording two one zero so that looks so good add package it will think about that for a little bit and whenever it's finished we can get code in here so successfully added that is great now we have a lot of stuff that we can use to send actual messages so the first thing we want to do is go to create a firebase app actually so let's get that using in here using firebaseadmin dot create so we are going to create this app it returns something but the way this kind of works is with a couple of static classes and whatever that you can just use and singletons and whatnot so you just have to use this create and not actually get the app value unless you somehow need it in your scenario and we have to specify new app options in here so we can actually you know get our credentials in there and authenticate with our firebase servers so that the the servers know it's us and we're actually authorized to send push notifications um so you can do this in a couple of different ways actually let me unpin this one so we have a little bit more space there we go what we can do here is say credential is and here we have a couple of different options um so what we can say is google credential which lives in another namespace so google credential so we need to import that using let intellisense help me here for a little bit and here we have all kinds of things so from access token which is probably not the most ideal way because you have to refresh it yourself we can save from file that's the one that we are going to use from json so you can just put a json string in here basically the contents of the file that we are about to use and it will extract all the credentials from there from a stream or you can do the get application default which is actually the recommended way from the documentation but that requires to set your path to the file in a environment variable on windows or on linux so that is really specific to kind of a server scenario which might not be something that you want so i found the more most compact way is to do it from file so you can just point it at the file that we deliver with these executables and then it will know um where it will come from but you know environment variables might be a little bit more secure because this is important credentials but again look at your scenario and look at what is going to work for you i'm going to show you this from file and from file then we have to specify the path to that file so let's make this private key dot json there we go and then we are done setting up our app and actually let's just dive in and set that file in here immediately so let me save this and go back to our solution and for our new second little project i'm going to add a existing file and here you can see in my downloads folder i have the json file that we just downloaded you can see the private key you can see some other things in here so this is the one we want let's open that let's move it to this directory i don't need it for anything else so there we go and now this file is part of our project now we need to rename this so let's right-click rename because i used private key instead of that unreadable file name so there we go and of course the other important thing that we need to do is copy this to our application output so that it can find it actually with just the file name of course you can put it anywhere on your file system as long as the path can be found by the program that you're about to run but what i'm going to do is right click set the quick properties you can go to the properties on visual studio for windows and also set the i don't know what the actual property is called but just make sure that it says copy to output directory i think you have options like always or only when there's changes or something like that but i'm sure you'll figure it out you're a smart person so quick properties copy to output directory what this does is it copies this json file to the output directory where all the binaries are from this program so now we've got said that set up so this should work fine um now actually let me just copy a little bit of code because you know you don't you're not here to watch me type you're here for solutions um so let me get something that i also got from the documentation so don't be scared i'm pasting in here a little bit and i'll go over this with you so you can see reds quickly so let's first import all these usings using firebase admin messaging i'm using this dictionary and i think that's about it oh here we have the send async but we'll fix that later oh i actually did the result here which is kind of dirty but you know it's needed for the scenario so let's go here um the registration token comes from the client fcm sdk so this is where we supply the token that we've seen in our application output so here we put in the token and that we're going to send the notification to so in this case we're going to send it here right this is the message so this is the message that we're actually going to send and if you think back to when we send it from the firebase console you could also actually provide extra data and that is the way that you can do it with this so you just have a key value pair for extra data that you want to send them that you can use for processing on your application then here this is that token that we're going to specify with which device we're going to send it to and this is the actual notification with a title a body and of course there's other options there as well i've already seen some comments on how to do set an icon for our notification or how to send an image with our notification if that's stuff that you're interested in please let me know in the comments and i'll make a video on that as well now another thing that you can do is get the actual response so we can see like what the firebase servers are saying um did the notification complete successfully or not and that response we're going to write back to the console so that's basically what we're doing now there's a couple of things going on here we can also say here new multicast message multicast message and then the token is replaced by tokens and then you can set a list of tokens so then you can send it to multiple devices at once so that's cool right so that's already a step forward to sending it to multiple devices at once but still you need to know all the tokens which is not great maybe so i'm going to show you another way a little bit later on but let's now focus on one token first so multicast message there we go let's just keep this one i'm going to inspect the application output and get the token from our running android application so do that put it in the add token right here save that for a little bit and i think all the rest looks pretty good so like i said this is a little bit dirty um sent async you shouldn't do the result thing here normally with async await uh but because i'm doing this in a console application in dot net 5 at least i think it dot net 6 this might be fixed you cannot async this this main method so that's why i'm doing this result here typically you don't want to do that you want to do an await here and that works better but for now this works fine and with this all in place i think i can put this in the background just to show you it actually works and then i go to my solution to my new send project and whenever i right click and i say start debugging project it will start another debugging session just for this project it will come up it will build all my things and it will actually start outputting all the things here authenticating with firebase services um and here we go successfully send message and we can see it here with this dot popping up we've sent this notification test from code here is your test that is the thing that we've seen here in code so that is pretty awesome now we know how to send a notification through code um okay one other thing that i promised you is like you know you need to have these registration tokens like this that is not great so what you can also do is add topics and topics can be like channels right it can be like let me actually clear this notification so that we can see it comes up in a little bit so with topics you can let apps subscribe to a certain topic it's just as simple as that so whenever your user says hey i'm interested in news then you'd let them subscribe to the topic news or i'm interested in the topic tech then you let them subscribe to a topic tech you can define those topics and you can determine which notification gets sent to which topic that's cool right and the topic is nothing more than basically just saying this is my string identifier for this topic so what i'm going to do now first is go to our shared code actually here our app example example cs because we need to let our app subscribe to a topic first so let me stop running this for a little bit and unpin this one there we go and then we can say cross firebase push notification there we go dot current dot subscribe i think and we have a couple of things here so subscribe we can subscribe to a list of topics actually all at once we can check out what are the topics that we're actually subscribed to so you probably want to double check that you don't double subscribe to topics i don't know if that's possible i didn't test it actually but you know better safe than sorry let people just subscribe once um you can also unsubscribe so maybe you have a toggle in your app which says i want to subscribe to these notifications now and whoops i changed my mind let's turn it off and let's unsubscribe or you can say unsubscribe all and just get rid of all the subscriptions on topics right then and there so this is all stuff that you need to figure out in your application logic so for now let's just say subscribe and it also has an overload for just one topic so let's say the topic all so you know that's something that you can do if you want an easy way to reach all of your users basically just create a topic all for instance and all your devices will be in there so that's like your main channel for notifications and from there you can split them out in different ones so that's pretty cool right now our app is subscribed to all so let me just get this up and running while i implement the rest because this is in our app right and this is in our shared code so this works automatically for ios and android that's pretty cool now let's go back to my program cs for my little console application while this is booting up here there we go it's coming back up there we go you can see the token is right here again in the application output but i don't need it this time i'm not going to do anything with that so let's put this app in the background and what i can do now is basically get rid of this registration token um let's put this again token here and actually let's put this in in comments so we get an error here and now instead of token we can say topic so i can send this to a topic and the only thing i need to do is set it to all and now whenever i run this so right click on the send start debugging project it starts running it does its thing gives me the output right here as you can see on the device it comes back up with the exact same notification but now i'm not sending it to a specific topic i'm not sending it to a specific token but i'm sending it to a specific topic which is cool because that topic can have thousands of devices at once um now this is there i think there is even another way that you can do um you can also send it to a condition um to be honest i haven't really found any good documentation on the condition so again if you think back to the other videos on the firebase console you could set a condition on like the application has to be this so you can send it only to android users or only to ios users or it's kind of like a query language where you can write your own condition for sending a push notification but there's i didn't find a lot of documentation on how to actually create that condition so um i think it's powerful tool but i didn't really figure out how to use it but topics you know is just as cool so now you know how to send push notifications from c sharp um to the implemented ios and android apps and with that i've basically created a whole course on how to send push notifications receive it on ios android send it from a server side i've seen a comment asking about like maybe implement uwp so if that's something that you're interested in please let me know in the comments i will probably do a video on how to customize the notifications or a couple of videos and also you know maybe slap at the front a little introduction to push notifications because i also noticed that a lot of people you know know what push notifications do but not really how they work under the hood so that is probably coming or if you're watching this a little time in the future then they might already be there on the playlist that this video is on thank you so much for watching another one of my videos please like this video if you've liked it consider becoming a member of my channel as i've mentioned in that shout out at the beginning of this video and subscribe to my channel at the very least if you've seen a couple of videos that you liked and helped me reach that next milestone other than that i'll be seeing you for my next video keep coding
Info
Channel: Gerald Versluis
Views: 46,838
Rating: undefined out of 5
Keywords: firebase push notification, push notifications, xamarin forms push notifications, xamarin forms push notifications sample, xamarin forms push notification firebase, xamarin forms firebase cloud messaging, firebase xamarin forms tutorial, push notifications xamarin forms, push notifications xamarin forms firebase, send push notification firebase, fcm push notification in asp.net c#, fcm push notification xamarin forms, fcm topic messaging, firebase push notifications using c#, .net
Id: VI1wgekz5ZM
Channel Id: undefined
Length: 21min 13sec (1273 seconds)
Published: Tue Jun 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.