Push notifications from server - c#

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

Related

WebRequest Not working on IIS but works in Visual Studio debugging or postman

.net core 3.1 application running under IIS 8.5 win 2012 R2
I try to push notification using WebRequest through this website but I get timeout. it's flustrating because when i write the same WebRequest in Postman or even in debugging mode in Visual Studio it's working!
Problem is when the Application is trying to post webrequest to fcm.googleapis.com
User fills up form,
User POSTs to web application,
Application creates another WebRequest with data from user post to fcm.googleapis.com and submits POST form in order to push notification to android devices
Web Form for users
[HttpPost]
public async Task<IActionResult> Create(CreateViewModel vm)
{
var request = new CreateRequest()
{
Author = String.IsNullOrEmpty(User.Identity.Name) ? "Guest" : User.Identity.Name,
Description = vm.Description,
};
var response = await _notificationService.Create(request);
vm.IsValid = response.IsSuccessfull;
vm.ShowBaseMessage = true;
vm.BaseMessage = response.Message;
return View("_CreateModalPartial", vm);
}
This data is passed to service which is responsible for submiting notifications
WebRequest for notification
try
{
var tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.Headers.Add(string.Format("Authorization: key={0}", "apiKey"));
tRequest.Headers.Add(string.Format("Sender: id={0}", "senderId"));
tRequest.ContentType = "application/json";
var payload = new
{
to = clientToken,
data = new
{
author = author,
description = description
}
};
string postbody = JsonConvert.SerializeObject(payload).ToString();
Byte[] byteArray = Encoding.UTF8.GetBytes(postbody);
tRequest.ContentLength = byteArray.Length;
using (Stream dataStream = await tRequest.GetRequestStreamAsync())
{
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();
//result.Response = sResponseFromServer;
}
}
}
}
}
catch(Exception e)
{
_logger.LogWarning(e.Message);
}
ALL TESTED DIRECTLY IN SERVER.. not external PC,
I have even installed Visual Studio in win 2012 R2 to debug in there but the problem doesnt show up while dubbing.. only after publishing it to IIS 8.5

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.

Sending bulk push notifications using FCM in 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.

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