Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error 401 'INVALID_KEY_TYPE' con FireBase y c#

Tags:

c#

I am implementing c # with FireBase, it sends me error 401 'INVALID_KEY_TYPE'

enter code here

private static Uri FireBasePushNotificationsURL = new Uri("https://fcm.googleapis.com/fcm/send"); private static string ServerKey = "AIzaSyA9fL8lPyxcrngIDDsDeUbq9sPTkavXXXX";

    public static async Task<bool> SendPushNotification(string deviceTokens, string title, string body, object data)
    {
        bool sent = false;

        if (deviceTokens.Count() > 0)
        {
            //Object creation

            var messageInformation = new 
            {
                to = "fZ0EyxU-tsk:APA91bE3-qo4DwL9phteDJC8pG6iLdr-YSSl-N_2SJne3U6eyUhmEuZNQhJi0YM-XXXXXX",
                priority = "high",
                content_available = true,
                notification = new
                {
                    body = "Test",
                    title = "Test miguel",
                    badge = 1
                },
            };

            //Object to JSON STRUCTURE => using Newtonsoft.Json;
            string jsonMessage = JsonConvert.SerializeObject(messageInformation);

            //Create request to Firebase API
            var request = new HttpRequestMessage(HttpMethod.Post, FireBasePushNotificationsURL);

            request.Headers.TryAddWithoutValidation("Authorization", "key=" + ServerKey);
            request.Content = new StringContent(jsonMessage, Encoding.UTF8, "application/json");

            HttpResponseMessage result;
            using (var client = new HttpClient())
            {
                result = await client.SendAsync(request);
                sent = sent && result.IsSuccessStatusCode;
            }
        }

        return sent;

    }
like image 319
Miguel Borquez Avatar asked Feb 27 '20 19:02

Miguel Borquez


People also ask

What is Server Key in firebase?

What is a Firebase Server Key? A Firebase Server Key and Firebase Sender ID are required in order to send push notifications to Android mobile app devices. The goal of this section is to provision your Firebase Server Key and Firebase Sender ID for use in OneSignal.


Video Answer


1 Answers

I have the same problem. The problem is that you take the wrong server key. The right firebase server key is Project > Settings > Cloud Messaging > Server Key

like image 198
Antoine Delaroue Avatar answered Sep 27 '22 22:09

Antoine Delaroue