Reputation: 5
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.
Thank you in advance.
Upvotes: 0
Views: 1057
Reputation: 12717
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()
Upvotes: 1