【问题标题】:System.IO.FileNotFoundException: 'Could not find file Xamarin FormsSystem.IO.FileNotFoundException: '找不到文件 Xamarin Forms
【发布时间】:2021-09-23 04:05:41
【问题描述】:

当我尝试在我的 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# firebase visual-studio xamarin.forms


【解决方案1】:

//解决办法

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)
        {

        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多