Code2night
  • Home
  • Blogs
  • Tutorial
  • Post Blog
  • Tools
    • Json Beautifier
    • Html Beautifier
  • Members
    • Register
    • Login
  1. Home
  2. Blogpost
18 Dec
2021

Sending FCM Mobile Notification in Asp.net for Android

by Shubham Batra

2505


FCM(Firebase Cloud Messaging)

Firebase is used for sending notifications or cloud messaging. For sending notifications on Android devices we must have a firebase account. After you create firebase account you will get your firebase server key. We will see the next steps

Controller-

So, we will use

https://fcm.googleapis.com/fcm/send

this api for sending notifications, we will see how to pass required data and server key.So for this api you need to add some data which we will serialize and send with the request . So your Firebase model must be like this

public class FirebaseModel
    {
        [JsonProperty(PropertyName = "to")]
        public string To { get; set; }

        [JsonProperty(PropertyName = "data")]
        public NotificationModel Data { get; set; }
    }
	
public class NotificationModel
    {
       
        [JsonProperty("title")]
        public string Title { get; set; }
        [JsonProperty("body")]
        public string Body { get; set; }
    }

So you have to add these models in your .net project. After adding these models, we have to create one http request to the fcm api. You can check out this code

FirebaseModel firebaseModel = new FirebaseModel();
firebaseModel.Data = new NotificationModel();
firebaseModel.To = "Android Device Token";
firebaseModel.Data.Body = "Test Notificaton";
var serverKey = "Enter your firebase server key";
var authorizationServerKey = string.Format("key={0}", serverKey);
HttpRequestMessage httpRequest = null;
HttpClient httpClient = null;

var jsonBody = JsonConvert.SerializeObject(firebaseModel);

                   
httpRequest = new HttpRequestMessage(HttpMethod.Post, "https://fcm.googleapis.com/fcm/send");
httpRequest.Headers.TryAddWithoutValidation("Authorization", authorizationServerKey );
httpRequest.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json"); httpClient = new HttpClient(); var result = await httpClient.SendAsync(httpRequest);

So here we are addng some dummy data with device token in the firebase model. Then we are serializing the data and sending to the fcm api.

After this, you can check you will receive one fcm notification on the device for which device token was specified.

So, this is how we can send fcm notifications using asp.net for android.

  • |
  • FCM , Cloud Messaging , Android Notifications , AspNet

Comments

Follow Us On Social Media - Like Us On Facebook

Best Sellers

product 1

Hand Hug Bracelet For Women Men Cuff Bangle Adjustable Lover Couple Bracelets

Can be given as a gift to your family, relatives, or friends

Buy $15.99
product 1

Teddy bear hug bear plush toy bear cub

Can be given as a gift to your family, relatives, or friends


Buy $49.99

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
Thank you for Downloading....!

Subscribe for more tutorials

Support our team

Continue with Downloading

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2023 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier