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

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.

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.
}
}
}
}
}

How to set custom android app notification icon when notification in send from webpage

android application receives notification properly, but it shows white square box for notification icon.
I want to set proper notification icon.
here is my c# code, I want to send notification from webpage button click to android application.
public string SendPushNotification(string deviceId)
{
string response;
try
{
string serverKey = "AAAAu0:******cw"; // Something very long
string senderId = "1:******";
// Also something very long,
// got from android
// string deviceId = "//topics/all"; // Use this to notify all devices,
// but App must be subscribed to
// topic notification
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 = "Greetings",
title = "Augsburg",
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}", serverKey));
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();
response = sResponseFromServer;
}
}
}
}
}
catch (Exception ex)
{
response = ex.Message;
}
return response;
}
public string all()
{
con.Open();
string query = "SELECT distinct token FROM tbl_user";
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.CommandType = CommandType.Text;
dr = cmd.ExecuteReader();
string token = "";
while (dr.Read())
{
string response = SendPushNotification(dr["token"].ToString());
}
con.Close();
return token;
}
Put this line in the manifest file-
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#mipmap/notification_icon" />

Firebase cloud messaging in 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.

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..

Categories

Resources