Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send notification from C# Console application

I want to create console application which is used to send notifications to different mobile devices via Goolge Firebase Notifications,

I have seen the code from link Send push to Android by C# using FCM (Firebase Cloud Messaging)

I am getting internal server error with status code 500

try{
    string url = @"https://fcm.googleapis.com/fcm/send";
    WebRequest tRequest = WebRequest.Create(url);
    tRequest.Method = "post";
    tRequest.ContentType = "application/json";

    string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + "This is the message" + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + deviceId + "";
    var data = new
    {
        to = deviceId,
        notification = new
        {
            body = "This is the message",
            title = "This is the title"

        }
    };
    string jsonss = Newtonsoft.Json.JsonConvert.SerializeObject(data);

    Byte[] byteArray = Encoding.UTF8.GetBytes(jsonss);              
    tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
    tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
    tRequest.ContentLength = byteArray.Length;
    tRequest.ContentType = "application/json";
    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();
                    Console.Write(sResponseFromServer);
                }
            }
        }
    }
}

catch (Exception ex)
{
    Console.Write(ex.Message);
    {
        var sss = ex.Message;
        if (ex.InnerException != null)
        {
            var ss = ex.InnerException;
        }
    }

}
like image 499
Sachin Verma Avatar asked Aug 10 '16 12:08

Sachin Verma


1 Answers

Yes, Duston is right, I am using wrong value in variable 'applicationID'

Actually I am using mobilesdk_app_id value instead of api_key current key thats why it is giving 500 error

Now it is working fine :)

Thanks Json, Francis Lord for your efforts :)

like image 138
Sachin Verma Avatar answered Nov 14 '22 23:11

Sachin Verma