Implement Push Notification In .NET MAUI (Android)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello guys welcome to my channel in this video i am going to implement push notification in dotnet mavi application for the an android platform so first let me create application in firebase console so here you just search for firebase console in google and then just click on go to console and here you need to create project like if if you have already project just click on that existing project or click on add project to add new project and after creating project just you will see something like this your project name and here you will see one option of add app so just click on that and here you can add ios application android application so right now we are going to implement push notification for android so just click on android and here you need to add package name and app name so let me see here package name so we created here application name as a push notification demo mavi so when i click here on edit project here in edit project file you will see here app identifier so this identifier you can change here identify whatever you want or you can like right now i'm just going to use this identifier here so i'm just adding this identifier name and here i'm just adding app name as a push notification demo and then just click on register app okay so here now uh google service dot json file is generated so you just need to download this file now this file i'm just going to add in project so let me add so here in android i'm just going to add this google service dot json file now after you added this file so if you see here all the project configuration detail is available here so our project package name is this now we need to set this file as a google service json so in a build action we need to set as a google service json but you cannot see that option so for that you need to install some plugin so let me install so this three plugin is required so this jammer dot google dagger will enable that option uh google service json option so let me install one by one and also i'm going to install this jambrian.firebase messaging to implement messaging stuff so here in dependency in a dotnet 6 android packages i am just going to click on manage nuget package and first i am going to install this jam ring.google play service dot base package then i'm just going to install this jamdine.firebase messaging package and the last one that is jamming.google.dagger so this will enable the build type so now let me see here in a google service.json i'm just going to set now so see here this google service json option is now visible so just set this with action of a google service file to google service json now here in android folder i am just going to create now services folder in that i will add firebase service so here i am just going to create new class called firebase service and that class going to implement firebase messaging service and this is service so i need to add here annotation uh services and in that i need to set export it otherwise it just going to true exception like uh you not enable exported flag so here i am enabling it and also i am adding here intent filter this com.google. messaging event now let me override one method on token so when the token is generated so this uh matter is going to call so i will add this token in preferences so in application we can use like uh we need to save this token in either database or you can save in like a firebase messaging system wherever you want so you can save this token so let me just save this token in first preferences so i will check like if preference is that contain device this device took on then i'm just going to remove this device token and then i will add okay now this token i'm going to check here in main page like when the application launched so that time when you install application first time it's just going to generate token so for verifying it here i will check like okay so now let me run the application and let's see it's a generating token and adding in preferences or not so here i'm just adding debug point so we can check like it's a generating new token or not okay so when the application long that time it's now it's generating token and we are adding in preferences so here let me check in when it navigate to main page okay so when i read an application that time we will get this like a preferences contain this device token then uh we will get this so let me just rerun the application to see that it's token is available here or not okay so now it's accessible from preferences now here in a main activity i'm just going to create notification channel so when the notification is received that time we can display that notification so let me implement that okay so here i am going to override on create method and here i'm just going to create the notification channel and this i am just going to call from here so when oncreate method is called then that time i'm just going to create notification channel now here i'm just going to add logic related to creating channel and here i need to pass channel id and notification important so let me create here variable so here i'm just going to specify channel id as a test channel and that i will pass here and here you can specify the name so i'm just uh this notification so notification channel name so i'm just specifying here this notification channel and here you are here you can set that notification channel important so i'm just setting as a default okay so here i'm just getting notification service and from notification service i just going to create notification channel and in that i passed the channel that we created here okay and now it's showing here this call is reachable on android 21 and later so notification channel is only supported on android 26 and later so here i'm just going to add condition like so if the operating system os platform version is like a 26 or above then this condition is going to execute okay so here notification channel is created now here in firebase service i need to handle on message received so when someone send notification that time here on message received matter is going to call so let me just overwrite now here i'm just going to create another method send notification here i will pass message body then message title and if someone send data also so that i'm just going to pass in dictionary format of like android data is pass in this format like dictionary key and value so that i am accepting here now i'm just going to create notification builder so that will use for displaying notification so here i need to pass current uh current contact so i'm just passing this and you other um i need to pass channel id here so that will i get from mainactivity.channel id now here i will set now one by one property so here title will be the title that we patched from here so this method like when notification received this send notification matter with call and from this object this variable i'm just going to set title and another property that is necessary is that small icon like if you're not saying then it's just going to throw exception when notification is received so we need to set here icon so i'm just setting from resources okay from the resource um [Music] from the resources i'm just going to set app icon and here content tag so this will be the message body and channel id so whatever the channel id we specified in main activity so that i am just going to set here and also you can set a priority here like if you want to set notification priority then you can just specify priority here now i'm just going to display this notification so for that purpose i am just going to create notification manager and from this notification manager object i'm just going to notify this current notification that we created so here we need to pass notification id and the notification so let me create notification id so here i'm just setting any value you can send anything like a unique notification id here and that you just need to pass here and this notification builder we created so notification builder dot build so it is going to display the notification now now this send notification i am just going to call here on message received okay so from this remote message i'm just fetching notification detail and now i will call this send send notification and in that i will pass notification dot title and notification message so let me just pass first notification dot body so that will be the notification message body and here the title and other thing like uh if some data pass in dictionary format so that will be available in message dot data so that i will show you like how you can add this data in intent and from that you can handle the navigation like if someone uh send notification and on the click of notification you want to navigate to particular page that you can handle using this data now here on main page i'm just going to send notification to this current device token so let me add logic related to this here in this counter click method okay so let me create here who's notification request and here i'm just going to set now notification is equal to new notification body in that i will pass notification title and here notification body now here in registration id uh you can pass list of device token so right now i'm just going to pass current device token but you can hatch list of device token from api and then you can add here in registration ids now let me add url that i want to send notification so you can use this url to send notification so now i'm just going to create a request object so here for calling this api i need to add authorization header so in that i need to pass server key so that you will get from just click on your project then click on project setting and here on cloud messaging option you will get server key so let me add here server key in authorization so default request header then authorization and here i am just going to add this authentication header value so here in schema you can set key and then in parameter just add your key in this format so key equal to whatever the server key that you decide here so let me add that server key okay so after adding server key you can call client dot post async and in that request url and here new string content and in that i am just going to pass content in string format so for that let me convert let me this serialize this push notification request i think i need to install newton's of json plugin so let me just install it let me just disable this preview version okay so it's installed now here i'm just going to use json convert dot serialize object and in that i will pass this post notification request now here incline dot post the sync i will just set as a new string content and in that i will pass this serialize request and let me set encoding and that request will be the type of application slash json so here i will check like if response dot [Music] status code is equal to okay uh http status code dot okay then i'm just going to display alert okay so here now sending notification code is ready now let me run the application and let's see on the click of this counter button it's a sending notification or not do okay so now here on the click of counter button it displaying notification sent message as well as a displaying notification here and here it's only displaying notification body not the notification title so let me see here in firebase service so here in notification builder okay i'm setting here content title to time so here i need to set content tags is equal to message body now let me rerun the application so now it's going to display notification title as well as notification text okay so here now on button click uh it's displaying notification title as well as notification body now i'm just going to implement like on the click of a notification i'm just going to redirect to particular page like if the application is totally closed and notification is received so on the click of notification i will redirect to particular page so for that here in mainpad.jaml.cs here i need to add data so we created here data object so in that i need to pass key and value in dictionary format so let me create a object for that so here i'm just going to create android notification object so here i created like android notification object because in data for ios you need to pass in a key will be the string and type will be the object type and for android you need to pass in like a key will be the type of string as well as a value also the type of string so for that here i just created as a android notification object dot add here i will add like a navigation id and value will be the i'm just right now setting as a navigation id and that value is equal to 1 and this i will set here in data object okay so here now this added in push notification request now on the click of notification we need to handle this data support that here in firebase service so we are passing this data in message.data so let me now handle so for that i'm just going to create new intent so i will just use for each of like from this data so whatever the data is passed from this main page uh in a form of dictionary so from there i'm just going to add value so let me just and that i am just going to add in intent dot put extra and in that i will just pass p and value so i will just look for each loop of this keys from this dictionary and i will add an intent that put extra so in intent i added all the values now let me create here pending in time so here i need to get pending internet activity so i'm passing current uh current contacts and then notification request code as a notification id and then i need this intent i'm just going to add here okay now this pending intent i'm just going to set here so i will set set contain intent and in this i will set this pending intent so whatever the data patch from notification request so it's going to be added in this intent in pending intent and that i can now access here in main activity so let me just add logic related to this in main activity so here i will check like if intent dot extra is not null then i will just patch that this pending intent key like here we added in intent.put extra we added key and value so i will access here i will check like if intended key is equal to this notification id then i will just set that in preferences so let me add logic related to this here okay so here i will check like let me just okay so from the key like all the key from intent dot extra i will now check like if key equal equal to uh this id like navigation id then i will just set this information in preferences and here let me fetch value so okay so from navigation id i will just patch that value and i will add it here okay so if any key exists with uh name of like navigation id then i will remove it and i will again set notification sorry navigation id and that value here now here in main page like when the page is launched so first page we are opening uh this main page so here i will check like references that contain key uh contain navigation id key then i will navigate to some other page so let me just add temporary some page here dot net mavic content page and this page i'm just going to register here in app cell okay so now here i will check like here i am passing like id like this but you can pass anything like a page name to navigate or user id or anything you can pass so right now i just did like this way and here okay now let me add another also id and i will navigate to some other page so i'm just going to add another page also here new [Music] page 2 yeah and this i am just going to register here so if the navigation id is one then it's uh navigate to new page one and if navigation id2 then it navigate to new page two and after that i will clear the preferences so i will just remove this navigation id because if we not remove then every time it's just going to pass navigation id and it's going to navigate to some other page okay now let me run the application and let's see it's navigating to other page or not okay so here now let me send notification okay so like when the application is open and uh if i click here on notification that time like it's uh throwing accepts and when the application is open here in main activity like uh this uh saved instance uh i think as you know i'm not so like white showing exception uh in uh jamming it's working but in dot net now it's not working so like if anyone knows solution related to this and uh related to this uh please let me know uh so let me just close it so let me again send notification so now i'm just sending notifications and i just close this application and i will click here on notification then it's just going to redirect to new page one because here in main page the demo i am passing notification id1 so now let me pass to two so this time it's going to navigate to this new page two so let me rerun the application and let's see it's navigating or not okay so now let me just close application so notification will stay there okay so here i just send notification now i'm just going to close application and here when i click on notification it's just going to redirect to new page two okay so that's all for today i hope you like this video and thank you so much for watching
Info
Channel: Programming With Pragnesh
Views: 16,719
Rating: undefined out of 5
Keywords: Push Notification In .NET MAUI, Implement Push Notification In .NET MAUI, .net maui, .NET MAUI, Push Notifcation, net maui, maui c#, maui tutorial, maui .net, maui, C# maui, dotnet maui tutorial, blazor tutorial, blazor crud, .net maui app
Id: gBbbctEvbOk
Channel Id: undefined
Length: 40min 23sec (2423 seconds)
Published: Sun Sep 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.