I'm getting the error System.IO.FileNotFoundException: 'Could not find file "/private_key.json"' when I try to run this function in my Xamarin project. The file is in the main project so I am not sure what I am doing wrong. I also switch the "Copy to Output Directory" to "Copy Always"
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);
}
//Solution
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)
{
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.