Index
- Introduction
- The Android Application
- Sending from Firebase Portal
- The .Net Core Service
Code on Github
Console Application
-
Create a new .Net Core empty application and add the Newtonosoft.Json package
-
Then we define the data types for the message. Notice the JsonProperty to adapt the FCM style of Json data
public class NotificationData { public string ShortDesc { get; set; } public long IncidentNo { get; set; } public string Description { get; set; } } public class Notification { public Notification() { Sound = "default"; } [JsonProperty("title")] public string Title { get; set; } [JsonProperty("text")] public string Text { get; set; } [JsonProperty("sound")] public string Sound { get; set; } } public class NotificationMessage { [JsonProperty("to")] public string To { get; set; } [JsonProperty("data")] public NotificationData Data { get; set; } [JsonProperty("notification")] public Notification Notification { get; set; } } -
Then fill the main. The Server key is the one founded inside the FCM console, Settings->Cloud Messaging->Server key
class Program { static void Main(string[] args) { SendNotificationFromFirebaseCloud(); } public static String SendNotificationFromFirebaseCloud() { var result = "-1"; var webAddr = "https://fcm.googleapis.com/fcm/send"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr); httpWebRequest.ContentType = "application/json"; httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "key=PROJECTSETTINGS->Cloud Messagings->Server Key"); httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { string strNJson = JsonConvert.SerializeObject(new NotificationMessage { To = "/topics/ServiceNow", Data = new NotificationData { Description = "Detailed description", IncidentNo = 12345, ShortDesc = "Short desc." }, Notification = new Notification { Title = "ServiceNow: Incident No." + 12345, Text = "This is Notification" } }); streamWriter.Write(strNJson); streamWriter.Flush(); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { result = streamReader.ReadToEnd(); } return result; } } -
Just run and...surprise :) It works!
Adding the API
-
Add the following nuget packages:
- Microsoft.AspNetCore
- Microsoft.AspNetCore.Hosting
- Microsoft.AspNetCore.Mvc
-
Now should add the "Web" stuff. The WebStartup class is used to start a web application
public class WebStartup { public IConfiguration Configuration { get; } public WebStartup(IConfiguration configuration) { Configuration = configuration; } public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(); } } -
Now change the main to start the application...and quit when needed
class Program { static void Main(string[] args) { var host = WebHost .CreateDefaultBuilder() .UseKestrel() .UseStartup<WebStartup>() .UseUrls("http://*:5006") .Build(); host.Start(); while (true) { if ("quit" == Console.ReadLine()) { break; } } } } -
Time then to add the models for our project
public class NotificationModel { public string ShortDesc { get; set; } public long IncidentNo { get; set; } public string Description { get; set; } } public class NotificationResult { public string Result { get; internal set; } } -
And let's add a Controller
[Produces("application/json")] [Route("api/notifications")] public class NotificationsController : Controller { [HttpPost] [Route("")] public NotificationResult Send(NotificationModel messageData) { var result = "-1"; var webAddr = "https://fcm.googleapis.com/fcm/send"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr); httpWebRequest.ContentType = "application/json"; httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "key=PROJECTSETTINGS->Cloud Messagings->Server Key"); httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { string strNJson = JsonConvert.SerializeObject(new NotificationMessage { To = "/topics/ServiceNow", Data = new NotificationData { Description = messageData.Description, IncidentNo = messageData.IncidentNo, ShortDesc = messageData.ShortDesc }, Notification = new Notification { Title = "ServiceNow: Incident No." + messageData.IncidentNo, Text = "Notification" } }); streamWriter.Write(strNJson); streamWriter.Flush(); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { result = streamReader.ReadToEnd(); } return new NotificationResult { Result = result }; } [HttpGet] [Route("sanity")] public string Sanity() { return "OK"; } } -
Now with a simple curl (download here curl for windows) you can send notifications to the phone!
curl --header "Content-Type: application/json" --request POST --data '{"IncidentNo":122,"ShortDesc":"Short incident","Description":"Long description"}' http://localhost:5006/api/notifications