首页 > 解决方案 > System.IO.FileNotFoundException: '找不到文件 Xamarin Forms

问题描述

当我尝试在我的 Xamarin 项目中运行此函数时,我收到错误 System.IO.FileNotFoundException: 'Could not find file "/private_key.json"'。该文件在主项目中,所以我不确定我做错了什么。我还将“复制到输出目录”切换为“始终复制”

 public async void test()
    {
        FirebaseApp.Create(new AppOptions()
        { Credential = GoogleCredential.FromFile("private_key.json") });

        // This registration token comes from the client FCM SDKs.
        var registrationToken = App.notiToken;

        // See documentation on defining a message payload.
        var message = new Message()
        {
            Token = registrationToken,
            Notification = new Notification()
            {
                Title = "Test from code",
                Body = "Here is your test!"
            }
        };

        // Send a message to the device corresponding to the provided
        // registration token.
        string response = await FirebaseMessaging.DefaultInstance.SendAsync(message).ConfigureAwait(false);
        // Response is a message ID string.
        Console.WriteLine("Successfully sent message: " + response);
    }

标签: c#firebasevisual-studioxamarin.forms

解决方案


//解决方案

public static void NotificacionEspecificaAndroid(string titulo, string body, string token)
    {
        try
        {
            var parameters = new JsonCredentialParameters
            {
                
                Type = JsonCredentialParameters.ServiceAccountCredentialType,
                ProjectId = "",
                PrivateKeyId = "",
                PrivateKey = "",
                ClientEmail = "",
                ClientId = "",     
                ClientSecret = ""
            };
            string json = JsonConvert.SerializeObject(parameters);
            var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
            //GoogleCredential.FromStream(stream);               
            FirebaseApp.Create(new AppOptions()
            {
                //Ruta para solicitar los permisos de las credenciale que se requieren para acceder a la consola de Firebase.                 
                Credential = GoogleCredential.FromStream(stream)                    
            });

            //var a = FirebaseApp.GetInstance("FleetGuardTest");
            // This registration token comes from the client FCM SDKs.
            //En caso de que sea a un dispositivo en especifico se agrega el token en esta zona.              

            // See documentation on defining a message payload.
            var message = new Message()
            {
                Data = new Dictionary<string, string>()
                {
                    { "Sound", "Default" },
                },
                //Token = registrationToken,
                Topic = "all",
                Notification = new Notification()
                {
                    Title = titulo,
                    Body = body,
                    ImageUrl = ""                        
                }                    
            };

            // Send a message to the device corresponding to the provided
            // registration token.
            string response = FirebaseMessaging.DefaultInstance.SendAsync(message).Result;
            // Response is a message ID string.
            Console.WriteLine("Successfully sent message: " + response);
            FirebaseApp.DefaultInstance.Delete();//Importan
        }
        catch (Exception e)
        {

        }
    }

推荐阅读