I execute this code to push notifications to mobile device using FCM library
public string PushFCMNotification(string deviceId, string message)
{
string SERVER_API_KEY = "xxxxxxxxxxxxxxxxxxxxxxx";
var SENDER_ID = "xxxxxxxxx";
var value = message;
WebRequest tRequest;
tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
tRequest.Headers.Add(string.Format("Authorization: key={0}", SERVER_API_KEY));
tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
var data = new
{
to = deviceId,
notification = new
{
body = "This is the message",
title = "This is the title",
icon = "myicon"
}
};
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);
dataStream.Close();
WebResponse tResponse = tRequest.GetResponse();
dataStream = tResponse.GetResponseStream();
StreamReader tReader = new StreamReader(dataStream);
String sResponseFromServer = tReader.ReadToEnd();
tReader.Close();
dataStream.Close();
tResponse.Close();
return sResponseFromServer;
}
now, how to send message to multi device, assume that string deviceId parameter replaced with List devicesIDs.
can you help
Please follow these steps.
}
I hope the above code will help you to send push on multiple devices. For more detail please refer this link https://firebase.google.com/docs/cloud-messaging/android/device-group
***Note : Please must read the about creating/removing group by the above link.
You should create a Topic and let users subscribe to that topic. That way, when you send an FCM message, every user subscribed gets it, except you actually want to keep record of their Id's for special purposes.
See this link: https://firebase.google.com/docs/cloud-messaging/android/topic-messaging
Update: For v1, it seems that
registration_ids
is no longer supported. It is strongly suggested that topics be used instead. Only the parameters shown in the documentation are supported for v1.Simply use the registration_ids parameter instead of
to
in your payload. Depending also on your use case, you may use either Topic Messaging or Device Group Messaging.For more details, you can check out the Sending to Multiple Devices in FCM docs.