c# - 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);
}
解决方案
//解决方案
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)
{
}
}
推荐阅读
- css - 以角度将图像显示为网格 ngFor
- javascript - 使用contact form7插件预览上传图片
- r - 在 DataTable 中使用 DropDown 更新 DataTable
- python - Sklearn Pipeline:在 ColumnTransformer 之后获取特征名称的最终列表,然后是来自 Pipeline 的 SelectFromModel
- ruby-on-rails - Rails + Sidekiq:Redis 被重置为空状态(sidekiq 历史丢失)
- javascript - Vue 路由器仅适用于页面重新加载
- arrays - 使用 jq,如何在不更新所有其他对象的情况下将元素附加到数组?
- html - 在颤动中从标签中获取数据
- bash - BASH 脚本不解释逻辑 Not in glob 表达式
- typescript - 在打字稿中输入对象叶子的更好方法