Fcm (Firebase Cloud Messaging) Push Notification with ASP.NET

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)

Step. 1

Step. 2

Step. 3

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:

  1. Click on the setting button near Project Overview

    Sample Image

  2. Choose the first option (Project setting)

    Sample Image

  3. Click on the second tab (Cloud messaging)

    Sample Image

  4. Use the server key and sender ID in this page

    Sample Image

At first, I tried looking for server key in cloud messaging tab in the sidebar which is wrong location.

Sample Image



Related Topics



Leave a reply



Submit