Sending bulk push notifications using FCM in C#? - c#

I am writing a c# service that will allow me to send push notifications using FCM. Now, everything works fine but then I want to send bulk push notification using FCM.
I want to be able to send the same message to multiple devices at once. The current takes one registration token and then goes through the whole process and then goes to the next one. But this will be useless if I have to send push notification to many numbers at once. What should be my best option?
c# code
public String SendPushNotification(string regtoken)
{
try
{
string applicationID = "#############3";
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;
}

Update: For v1, it seems that registration_ids is no longer supported and that making use of topics is the best approach.
You can either use the registration_ids parameter where you'll be able to specify up to 1000 registration tokens:
This parameter specifies the recipient of a multicast message, a message sent to more than one registration token.
The value should be an array of registration tokens to which to send the multicast message. The array must contain at least 1 and at most 1000 registration tokens. To send a message to a single device, use the to parameter.
Just replace
to = deviceId
to something like
registration_ids = deviceIds
where deviceIds is an array of tokens.
Or you could make use of Topic Messaging, where so long as the user is subscribed to your given topic, they would receive messages sent to that topic.

Related

Receiving push notifications twice

We have been using Firebase to send push notifications for a couple of months in our app, and I have used it in other of my apps . Everything had gone well, however a week ago we started receiving duplicate notifications (not always). The strange thing is that we did not change any code, it just started to happen in both Android and iOS. It only happens sometimes not always, and only in some users/devices.
I want to know if someone else is going through this with Firebase. I think it is a Firebase bug but I would like to see if someone can confirm something about it.
The ios app is made in swift, and the android app in Java.
BTW. We are sending it based on this with FCM in our back-end. And we have no duplicated topics. I have checked and devices are only subscribed once.
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
"to": "/topics/foo-bar",
"data": {
"message": "This is a Firebase Cloud Messaging Topic Message!",
}
}
Here my c# code in our back-end to send notifications
public void sendPushNotification(string _title, string _message, string _topic, object customData = null)
{
var requestUri = "https://fcm.googleapis.com/fcm/send";
WebRequest webRequest = WebRequest.Create(requestUri);
webRequest.Method = "POST";
webRequest.Headers.Add(string.Format("Authorization: key={0}", FCM_SERVER_API_KEY));
webRequest.Headers.Add(string.Format("Sender: id={0}", FCM_SENDER_ID));
webRequest.ContentType = "application/json";
var data = new
{
to = "/topics/" + _topic, // this is for topic
notification = new
{
title = _title,
body = _message,
sound = "default"
},
data = customData, //custom data (got it from firebase documentation)
content_available = true
};
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
Byte[] byteArray = Encoding.UTF8.GetBytes(json);
webRequest.ContentLength = byteArray.Length;
using (Stream dataStream = webRequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
using (WebResponse webResponse = webRequest.GetResponse())
{
using (Stream dataStreamResponse = webResponse.GetResponseStream())
{
using (StreamReader tReader = new StreamReader(dataStreamResponse))
{
String sResponseFromServer = tReader.ReadToEnd();
}
}
}
}
}

There is way to send all full options appear in firebase cloud messaging create messaging page

I have the project in asp.net MVC 5 I need to add all option to my client-side app
which is send a push notification to android and ios app
for this scenario, I had created a page like a firebase cloud messaging =>
create message
c# code
private static string SendPushNotification()
{
string response;
try
{
string serverKey = "##########";
string senderId = "#############";
string deviceId = "//topics/all";
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;
}
So My query is that
1: I could send all these options to my HTTP request or not
2: There is open to send later open I need to also configure this
option
3: And Target User option?
Could I do this using HTTP request by providing parameters?
Not all.For most of the text fields, you can (see the docs for the HTTP ref):
Message Text = body
Message label: Nope. See the help text (?), it's just a label that the Firebase Console uses.
Delivery Date: See #2.
User Segment: See #3.
Message title = title
Android Notification Channel Name = android_channel_id
Scheduled notifications are currently not available for the REST API.
User Segments currently not available yet.

Push notifications from server

In order to send a notification from an Console App to an android device I am using the following code:
class Program
{
static void Main(string[] args)
{
string applicationId = "applicationId";
string senderId = "senderId";
string deviceId = "deviceId";
string message = "TestMessage";
var x = new AndroidFCMPushNotificationStatus();
var response = x.SendNotification(applicationId, senderId, deviceId, message);
Console.WriteLine(response);
Console.ReadLine();
}
}
and
public class AndroidFCMPushNotificationStatus
{
public string SendNotification(string applicationID, string senderId, string deviceId, string message)
{
try
{
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 = message,
},
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();
return (sResponseFromServer);
}
}
}
}
catch (Exception ex)
{
return (ex.Message);
}
}
}
from Push Notification C#.
I created a firebase console project and from the project settings, from the tab Cloud Messaging I got the Server Key and Sender Id (in code they are the applicationId and senderId). I also got my device Id from an app I downloaded to my mobile, where it gives me a string under the label Android Device Id.
However I get the message error:InvalidRegistration.
I am pretty new to this and I think I am missing something.
Is the above code supposed to work and whenever I run the program I should get a notification on my phone?
Should I do anything to allow this notifications to my mobile? I haven't configured anything on my device.
The idea is to pack this code in a service and whenever something is happening on the server I want to use this code in order to inform
my device. Is this the correct way?
Thank you in advance.
error:InvalidRegistration means that the deviceId (also called Registration token) is wrong.
Check the format of the registration token you pass to the server.
Make sure it matches the registration token the client app receives
from registering with Firebase Notifications. Do not truncate or add
additional characters.
https://firebase.google.com/docs/cloud-messaging/http-server-ref#error-codes
check that you are reading the correct token:
FirebaseInstanceId.getInstance().getToken()
https://firebase.google.com/docs/cloud-messaging/android/first-message#retrieve-the-current-registration-token

How do I compose the Firebase request so that I can broadcast 100 notification to Mobile Devices in single request?

I want to send a notification to multiple device in single FCM request. My notification text is same for all devices.
I would like to send notification to mobile devices in a batch of 100 per request. I am using c# asmx service.
Below is my code.
string regid="c_Z5yRoj4TY:APA91bGry2g_CIA1xaRy_LscxOvFX6YHqasKA96TjpG6yi1yytNyM5rtGL6DgxjGMSE5c74d7VdSL6W8zxO1ixVMlpVMwdgcrsGUWV0VfdbddC2XD","c_Z5yRoj4TY:APA91bGry2g_CIA1xaRy_LscxOvFX6YHqasKA96TjpG6yi1yytNyM5rtGL6DgxjGMSE5c74d7";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://fcm.googleapis.com/fcm/send");
httpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
httpWebRequest.Method = "POST";
String collaps_key = "Score_update";
string json = "collapse_key=abcd" + "&data.header=cricket&registration_id=" + regId + "&data.notificationId=" + notificationId + "&data.message=" + msg;
httpWebRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
httpWebRequest.Headers.Add(string.Format("Sender: key={0}", SENDER_ID));
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
//Console.WriteLine(json);
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
using (HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.WriteLine(result);
retmsgid = result.ToString();
if (retmsgid.Trim() != "")
{
ResponceString = result.ToString();
string[] msgsplits = retmsgid.Split(',');
string[] msg1 = msgsplits[0].ToString().Split(':');
ReturnMessageId = msg1[1].ToString();
}
else
{
ReturnMessageId = "0";
}
}
httpResponse.Close();
httpResponse.Dispose();
httpWebRequest = null;
}
}
In order to send to specific multiple registration devices, you'll have to make use of the registration_ids parameter:
This parameter specifies the recipient of a multicast message, a message sent to more than one registration token.
The value should be an array of registration tokens to which to send the multicast message. The array must contain at least 1 and at most 1000 registration tokens. To send a message to a single device, use the to parameter.
In your code, you were using registration_id which I think is invalid. It should be registration_ids:
string json = "collapse_key=abcd" + "&data.header=cricket&registration_ids=" + regIds + "&data.notificationId=" + notificationId + "&data.message=" + msg;
-- regIds here is an Array of Strings that contain your registration tokens.
Some previous posts that may also help (see the OPs code snippets):
FCM (Firebase Cloud Messaging) Send to multiple devices
How to send multiple Firebase Cloud Message with C#?

Sending FCM Notifications to multiple devices (but not all devices) in C#.net

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

Categories