2019年更新
有一个新的.NET 管理 SDK https://firebase.google.com/docs/reference/admin/dotnet/允许您从服务器发送通知。
通过 Nuget 安装
Install-Package FirebaseAdmin
然后,您必须按照给出的说明下载服务帐户密钥来获取它here https://firebase.google.com/docs/admin/setup,然后在您的项目中引用它。我已经能够通过像这样初始化客户端来发送消息
using FirebaseAdmin;
using FirebaseAdmin.Messaging;
using Google.Apis.Auth.OAuth2;
...
public class MobileMessagingClient : IMobileMessagingClient
{
private readonly FirebaseMessaging messaging;
public MobileMessagingClient()
{
var app = FirebaseApp.Create(new AppOptions() { Credential = GoogleCredential.FromFile("serviceAccountKey.json").CreateScoped("https://www.googleapis.com/auth/firebase.messaging")});
messaging = FirebaseMessaging.GetMessaging(app);
}
//...
}
初始化应用程序后,您现在可以创建通知和数据消息并将它们发送到您想要的设备。
private Message CreateNotification(string title, string notificationBody, string token)
{
return new Message()
{
Token = token,
Notification = new Notification()
{
Body = notificationBody,
Title = title
}
};
}
public async Task SendNotification(string token, string title, string body)
{
var result = await messaging.SendAsync(CreateNotification(title, body, token));
//do something with result
}
.....然后您可以将其添加到您的服务集合中...
services.AddSingleton<IMobileMessagingClient, MobileMessagingClient >();