Firebase cloud messaging in C# - c#

I am trying to send push notifications from server in C#, i am using the correct registration token and API key but still getting the following response.
{"multicast_id":7864311304033595507,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
I am following this url to implement this solution Send push to Android by C# using FCM (Firebase Cloud Messaging)
Currently i am trying to send notification to a single device but also want to send to multiple device at once, I used to : "/topics/all" as given in the url but it doesn't work. What should I do if I have to send notification to multiple device at once?
Here is my code
try
{
string applicationID = "SERVER_KEY ";
string senderId = "SENDER_ID";
string deviceId = "ba92be2da78e7285";
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var data = new
{
//to = deviceId,
to = deviceId,
notification = new
{
body = "Bring your existing apps and games to the Windows Store with the Desktop Bridge",
title = "Bridge",
sound = "Enabled"
}
};
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;
}

My problem got resolved. I was using device id which is different then registration token id. So I have to use registration token id returned by FirebaseInstanceId.getInstance().getToken() method. Rest of the code is same what I posted in question.

Related

how to send millions of push notification from fcm in c#

I am able to send push notification to android and ios device using below code. Now i want to send more than 5 lack notification. I want to log response also into my db. So i have used threading but it is taking more than 12 hrs.
string applicationID = "dummyid";
string senderId = "##########";
string deviceId = regtoken;
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 = message,
title = "",
sound = ""
}
};
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;
}
}
}
status = statusmessages();
}
}
catch (Exception ex)
{
string str = ex.Message;
status = "failure";
}
return status;
Is their any way where i can send this notification faster.

notification not appear while send notification to android app via fcm c#

I am trying to send GCM Push notification (Cloud Messaging) to my android app via c# code. when i run this code it is running successfully. i am not getting any error, but notification is not coming on application. I want to send notification to all the devices not to any specific device.
public void SendMessage()
{
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
//serverKey - Key from Firebase cloud messaging server
tRequest.Headers.Add(string.Format("Authorization: key={0}", "My Server Key"));
//Sender Id - From firebase project setting
tRequest.Headers.Add(string.Format("Sender: id={0}", "SenderId"));
tRequest.ContentType = "application/json";
var payload = new
{
to = "android", //i want to send to all android devices.
priority = "high",
content_available = true,
notification = new
{
body = "Test",
title = "Test",
badge = 1
},
data = new
{
key1 = "value1",
key2 = "value2"
}
};
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(); //here i am getting message id but no notification.
}
}
}
}
}

Send push to iOS by C# using FCM (Firebase Cloud Messaging)

I used the following C# CODE to use FCM on the IOS device push, but the IOS device did not receive the push message.
I used the FCM push to successfully push to the IOS device.
Here is my C# CODE
try
{
//
string applicationID = "AIzaSyhYzPkQgYND6P5cpLvEMoR9TUbJpgJeks";
string senderId = "968869743756";
string deviceId = "/topics/ios";
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var data = new
{
to = deviceId,
notification = new
{
title = "body",
body = "title",
sound = "Enabled"
}
};
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;
}
sResponseFromServer message always be
{"message_id":9201392109823459675}
Am I missing something?
This is my first time to use FCM.

C# build server side ios push notification

I am .net developer making server side library to push notification on IOS client application using FCM firebase cloud messaging using this code.
{
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var objNotification = new
{
**to = "Registration token ",**
notification = new
{
title = "hi",
body = "FCM correct token tab closed"
}
};
string jsonNotificationFormat = Newtonsoft.Json.JsonConvert.SerializeObject(objNotification);
Byte[] byteArray = Encoding.UTF8.GetBytes(jsonNotificationFormat);
tRequest.Headers.Add(string.Format("Authorization: key={0}", "server-key"));
tRequest.Headers.Add(string.Format("Sender: id={0}", "sender id"));
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("Message was sent successfully");
}
else if (response.failure == 1)
{
Console.WriteLine(string.Format("Error sent from FCM server, after sending request : {0} , for following device info: {1}", responseFromFirebaseServer, jsonNotificationFormat));
}
}
}
}
}
}
How could I retrieve Registration token from FCM before send notification and use it to push notification ?
Thanks..

How to implement Firebase notification API for user segment and topic using .Net

Following code I am using to send notification for single device and its working fine. But I want send notification for particular group of client's
Apps.
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var objNotification = new
{
to = "Device_Token_Id",
notification = new
{
title = "Notification Title",
body = "Notification message"
},
priority = "high"
};
string jsonNotificationFormat = Newtonsoft.Json.JsonConvert.SerializeObject(objNotification);
Byte[] byteArray = Encoding.UTF8.GetBytes(jsonNotificationFormat);
tRequest.Headers.Add(string.Format("Authorization: key={0}", "Server_Api_Key"));
tRequest.Headers.Add(string.Format("Sender: id={0}", "Sender_Id"));
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();
Console.WriteLine(responseFromFirebaseServer);
Console.ReadLine();
}
}
}
}
Please let me know if anyone know how to implement this api to send notification for particular group of user or to everyone.
Assuming that you mean user segments available in the Firebase console, then the answer is you cannot target them from the Firebase Cloud Messaging HTTP API. At this point this is limited to the Firebase console.

Categories

Resources