How to send push notification to multiple users in firebase using C#.
I using the FCM to send notification to one user but when i try to send to multiple users i have Bad Request Exception.
Thanks in advance ..
string applicationID = NotificationConstants.GoogleNotificationData.GoogleAppID;
string senderId = NotificationConstants.GoogleNotificationData.SenderId;
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var data = new
{
to = "f3fMjgIVqog:APA91bHxfhY5zbCmHqkfG2igd499DIYVVbqvi6SUT_ZeiMa9W-abce0f9tEqIupgQHiTcoU2eZKA-dZboteeWsbOsrFWdtjjPBxzI3YJTSvPJSUiSOBicBd7xd1Hb2vtioSUNvMtz0-f",
data = dataObject
};
var json = JsonConvert.SerializeObject(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;
}
}
}
}
Your data should be like
string [] registration_ids = { deviceRegId };
var data= new
{
// to = deviceRegId, // uncomment to notify single user
registration_ids = registration_ids,
priority = "high",
notification = new { title, body }
};
Related
I have tried to implement push Notification in-between from C#.Net(MVC) Web to React Native (Android and IPhone). No Error showing in my code but not show notification in App.
I have tried as reference gave in Stack overflow already
try
{
string server_api_key = ConfigurationManager.AppSettings["SERVER_API_KEY"];
string sender_id = ConfigurationManager.AppSettings["SENDER_ID"];
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
tRequest.Headers.Add($"Authorization: key={server_api_key}");
tRequest.Headers.Add($"Sender: id={sender_id}");
var data = new
{
to = "AAAAaQ5neZA:................................XXXX",
priority = "high",
data = new
{
message = response,
name = loginUser.EmployeeName,
userId = uId,
status = true
}
};
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
Byte[] byteArray = Encoding.UTF8.GetBytes(json);
tRequest.ContentLength = byteArray.Length;
Stream dataStream = tRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
WebResponse tresponse = tRequest.GetResponse();
dataStream = tresponse.GetResponseStream();
StreamReader tReader = new StreamReader(dataStream);
string sResponseFromServer = tReader.ReadToEnd();
tReader.Close();
dataStream.Close();
tresponse.Close();
return sResponseFromServer;
}
catch (Exception)
{
throw;
}
i am trying to send a notification to a mobile device from firebase
i have created a project in firebase console and using the following block of code to do that
try
{
var applicationID = "AIz***********************KE";
var senderId = "52**********8";
string deviceId = "5a1e*********55";
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 = "test body",
title = "test notification",
// 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;
}
But i am Getting "The remote server returned an error: (401) Unauthorized."
Can any one please correct me where i am doing wrong
Thanks in advance
Srinivas.
I am trying to use FCM for pushing notifications to Android device from C# server.I am using below mentioned code for sending notifications and it worked perfectly fine but I need to send data payload as well but I don't know how to implement that.
public static void SendPushNotification(String user_id,string not_title)
{
try
{
string applicationID = "AAAAjpeM.......";
string senderId = ".......";
string deviceId = user_id;
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 = not_title,
title = "ABC",
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;
}
}
I have tried this but it did'nt works.
var data = new
{
to = deviceId,
data = new
{
body = not_title,
title = "Avicenna",
payload="1",
sound = "Enabled"
}
};
try this:
var data = new
{
to = deviceId,
notification = new
{
body = not_title,
title = "ABC",
sound = "Enabled"
},
data = new
{
payload="1"
}
};
I am using FCM Notifications in my android app.
the notification has to be sent to sent to a limited number of users (around 200 users) at the same time using .net pages
public static void SendPushNotification()
{
try
{
string applicationID = "ABC****xyz";
string senderId = "01*****89";
string deviceId1 = "def****jdk";
string deviceId2 = "lej****wka";
string deviceId3 = "fqx****pls";
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var data = new
{
//This line is the problem
to = deviceId1+","+deviceId2+","+deviceId3,
notification = new
{
body = "Notification Body",
title = "Notification Title",
sound = "Enabled",
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;
}
}
The to line is problem is where I concatenate multiple devices
How can I just send the notifications for these devices?
Thanks
For sending push notification to multiple devices, you have to use 'registration_ids' instead of 'to' parameter that contains array of device tokens. The limitation is that you can provide a maximum of 1000 device tokens by this method. Check this out for reference FCM Downstream Messages
I am using GCM for notifications on Android with c#, but now I want to change GCM to FCM.
What do I have to change for that? Or how can I implement FCM in C#? Any help is appreciated. I am currently using this code for FCM, I just changed URL but it's not working.
try {
msg = message;
var applicationID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var SENDER_ID = "xxxxxxxxxxx";
WebRequest tRequest;
//tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/x-www-form-urlencoded";
tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + message + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + notificationtoken + "";
Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
tRequest.ContentLength = byteArray.Length;
Stream dataStream = tRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse tResponse = tRequest.GetResponse();
dataStream = tResponse.GetResponseStream();
StreamReader tReader = new StreamReader(dataStream);
String sResponseFromServer = tReader.ReadToEnd();
notification = sResponseFromServer;
tReader.Close();
dataStream.Close();
tResponse.Close();
return sResponseFromServer;
}
catch (Exception ex)
{
notification.Status = false;
notification = "ERROR DESCRIPTION : " + ex.Message;
}
Try this hope this help you..........
try
{
var applicationID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var senderId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
string deviceId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
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 = "This is the message Body",
title = "This is the title of Message",
icon = "myicon"
},
priority = "high"
};
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();
Response.Write(sResponseFromServer);
Label3.Text = sResponseFromServer;
}
}
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}