FCM (Firebase Cloud Messaging) Push Notification with Asp.Net
C# Server Side Code For Firebase Cloud Messaging
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
namespace Sch_WCFApplication
{
public class PushNotification
{
public PushNotification(Plobj obj)
{
try
{
var applicationID = "AIza---------4GcVJj4dI";
var senderId = "57-------55";
string deviceId = "euxqdp------ioIdL87abVL";
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var data = new
{
to = deviceId,
notification = new
{
body = obj.Message,
title = obj.TagMsg,
icon = "myicon"
}
};
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
Byte[] byteArray = Encoding.UTF8.GetBytes(json);
tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
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())
{
using (StreamReader tReader = new StreamReader(dataStreamResponse))
{
String sResponseFromServer = tReader.ReadToEnd();
string str = sResponseFromServer;
}
}
}
}
}
catch (Exception ex)
{
string str = ex.Message;
}
}
}
}
APIKey and senderId , You get is here---------as follow(Below Images)
(go to your firebase App)
Using FCM with Asp.net web api 2
Lets create console application as following:
class Program
{
static void Main(string[] args)
{
string resend ;
do
{
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var objNotification = new
{
to = "Token the device you want to push notification to",
data = new
{
title = "title",
body = "body",
icon = "/firebase-logo.png"
}
};
string jsonNotificationFormat = Newtonsoft.Json.JsonConvert.SerializeObject(objNotification);
Byte[] byteArray = Encoding.UTF8.GetBytes(jsonNotificationFormat);
tRequest.Headers.Add(string.Format("Authorization: key={0}", "your authorization key"));
tRequest.Headers.Add(string.Format("Sender: id={0}", "your senderId"));
tRequest.ContentLength = byteArray.Length;
tRequest.ContentType = "application/json";
using (Stream dataStream = tRequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
using (WebResponse tResponse = tRequest.GetResponse())
{
using (Stream dataStreamResponse = tResponse.GetResponseStream())
{
using (StreamReader tReader = new StreamReader(dataStreamResponse))
{
String responseFromFirebaseServer = tReader.ReadToEnd();
FCMResponse response = Newtonsoft.Json.JsonConvert.DeserializeObject<FCMResponse>(responseFromFirebaseServer);
if (response.success == 1)
{
Console.WriteLine("succeeded");
}
else if (response.failure == 1)
{
Console.WriteLine("failed");
}
}
}
}
}
resend = Console.ReadLine();
} while (resend == "c");
}
}
public class FCMResponse
{
public long multicast_id { get; set; }
public int success { get; set; }
public int failure { get; set; }
public int canonical_ids { get; set; }
public List<FCMResult> results { get; set; }
}
public class FCMResult
{
public string message_id { get; set; }
}
Using VB.NET to sent notification to android emulator get error 401
OK. I made a silly mistake by using API Key which is not actual Server key. Even though I have read that I should go to cloud messaging tab in project setting, I misunderstood that I have to look in cloud messaging tab in the side bar which is wrong. Here is the step where I found server key:
Click on the setting button near Project Overview
Choose the first option (Project setting)
Click on the second tab (Cloud messaging)
Use the server key and sender ID in this page
At first, I tried looking for server key in cloud messaging tab in the sidebar which is wrong location.
Related Topics
Using JSON File in Android App Resources
How to Remove Shared Preference While Application Uninstall in Android
Android - Play Sound on Button Click - Null Pointer Exception
Getting Certificate Details from an APK
Eclipse Ddms Error "Can't Bind to Local 8600 for Debugger"
Avoiding Content Type Issues When Downloading a File via Browser on Android
How to Click or Tap on a Textview Text on Different Words
How to Extract the Text from the Selected Item on the Listview
How to Close All the Activities of My Application
Android:Split the Screen in 2 Equals Parts with 2 Listviews
Android: Create Circular Image with Picasso
Adjust Layout When Soft Keyboard Is On
Menu Items Are Not Showing on Action Bar
How to Add New Activity to Existing Project in Android Studio
How to Capture an Image in Background Without Using the Camera Application