Push notifications for Firebase And APNS. Extension of Microsoft.Extensions.DependencyInjection
DLB.PushNotificationsis an extensible library to use push notifications in easy way. To use this package is necessary installed DLB.PushNotificaions and a concrete type. Firebase, APNS or both.
Install base the dependencies:
Install-Package DLB.PushNotifications
Then in Startup.cs file (or anything) add next line:
using PushNotifications.Extensions;
Finally, in ServiceCollection class, we have available an extension method:
services.AddPushNotifications(p =>
{
});
To send a Push Notifications only need inject 'IPushNotificationsFacade' interface and use 'SendPushNotificationAsync' method since. In this example I used a DomainEvent with DomainEventHandler, however, it can be used in any situation
public DomainEventHandler(ILogger<UserNotificationInsertedDomainEventHandler> logger,
IPushNotificationsFacade pushNotifications)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_pushNotifications = pushNotifications ??
throw new ArgumentNullException(nameof(pushNotifications));
}
public async Task Handle(DomainEvent notification)
{
var msg = new Message(Platform.Android, notification.PushToken.DeviceToken);
var payload = new Notification(description.Title, description.Description);
msg.Notification = payload;
await _pushNotifications.SendPushNotificationAsync(msg);
}
DLB.PushNotifications.Firebase is an extensible library to use firebase push notifications in easy way. To use this packge is neccesary install DLB.PushNotificaions and DLB.PushNotifications.Firebase
Install base the dependencies:
Install-Package DLB.PushNotifications
Install-Package DLB.PushNotifications.Firebase
Then in Startup.cs file (or anything) add next lines to add firebase integration:
using PushNotifications.Extensions;
using PushNotifications.Firebase.Extensions
Finally in ServiceCollection class, we have avaible a extension method:
services.AddPushNotifications(p =>
{
p.UseFirebase(conf =>
{
conf.FirebaseEndPoint = <FIREBASE_ENDPOINT>;
conf.SenderId = <SENDER_ID>;
});
});
Install-Package DLB.PushNotifications.APNS
You can use DLB.PushNotifications.APNS with two ways:
- Token (p8 certifcate)
- Socket (p12/pfx certificate)
In Startup.cs add the following lines:
services.AddPushNotifications(p =>
{
p.UseAPNS(conf =>
{
conf.DeliveryType = PushNotifications.APNS.Options.DeliveryType.Token;
conf.TokenOptions = new PushNotifications.APNS.Options.TokenOptions()
{
APNSEndPoint = settings.APNSEndPoint,
BundleId = settings.APNSBundleId,
KeyId = settings.APNSKeyId,
TeamId = settings.APNSTeamId,
P8CertificatePath = settings.APNSCertificate,
};
});
});
services.AddPushNotifications(p =>
{
p.UseAPNS(conf =>
{
conf.DeliveryType = PushNotifications.APNS.Options.DeliveryType.Socket;
conf.TokenOptions = new PushNotifications.APNS.Options.SocketOptions()
{
// Set options
};
});
});