0

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.

Community
  • 1
  • 1
Charles
  • 5
  • 5

1 Answers1

1

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

Diego Giorgini
  • 12,489
  • 1
  • 47
  • 50