Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

FCM Mobile notifications for IOS using Asp.Net

Date- Jan 02,2022

7313

IOS Notifications AspNet

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; }
}

Now, use this code for sending notifications on IOS, Sender Id and server key can be taken from FCM registration.

FirebaseModel firebaseModel = new FirebaseModel();
firebaseModel.Data = new NotificationModel();
firebaseModel.To = "IOS Device Token";
firebaseModel.Data.Body = "Test Notificaton";
 WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
               tRequest.Method = "post";
               
               //serverKey - Key from Firebase cloud messaging server  
				var serverKey ="Your server key"
               tRequest.Headers.Add(string.Format("Authorization: key={0}", serverKey));
               //Sender Id - From firebase project setting  
				var senderId = "Enter your SenderId"; 
               tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
               tRequest.ContentType = "application/json";
               var totoken ="Enter your IOS Device Token";
               var payload = new
               {
                   to = totoken,
                   priority = "high",
                   content_available = true,
                   notification = new
                   {
                       body = firebaseModel.Data.Body,
                       title = firebaseModel.Data.Title,
                       badge = 1
                   },
                   data = firebaseModel
	  
               };
	  
               string postbody = JsonConvert.SerializeObject(payload).ToString();
               Byte[] byteArray = Encoding.UTF8.GetBytes(postbody);
               tRequest.ContentLength = byteArray.Length;
               using (Stream dataStream = tRequest.GetRequestStream())
               {
                   dataStream.Write(byteArray, 0, byteArray.Length);
                   using (WebResponse tResponse = tRequest.GetResponse())
                   {
                       using (Stream dataStreamResponse = tResponse.GetResponseStream())
                       {
                           if (dataStreamResponse != null) using (StreamReader tReader = new StreamReader(dataStreamResponse))
                               {
                                   String sResponseFromServer = tReader.ReadToEnd();
                                   //result.Response = sResponseFromServer;
                               }
                       }
                   }
               }

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 IOS.

S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Related Articles

How to refund payment using Paypal in Asp.Net MVC
Jan 30, 2024
Integrate Stripe Payment Gateway In ASP.NET Core 8.0
Nov 23, 2023
How to get fcm server key
Nov 23, 2023
How to Convert Text to Speech in Asp.Net
Nov 06, 2023

Comments

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page