I want to send push notification from ASPNET server with firebase, however when I use this below code that I found from this , GCM turn me with an error that says "Remote server returned error (401), not acknowledged"
Could you help me for that ? How can I send push noitification from aspnet server to android phones ?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
namespace ConsoleApplicationFCM
{
class Program
{
static void Main(string[] args)
{
try
{
var applicationID = "AIzaSyB======UbYf6U";
var senderId = "143===2";
string deviceId = "APA91bHgVF0==========M9-ncvY80ZYVVeXziulC0l93p0cP4pFdnt84mY";
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"
body = "Selam",
title = "Title",
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;
}
}
}
}