Softeq.NetKit.Services.PushNotifications component simplifies the process of bringing support for Push notificaitons to your project. It's build around Azure Notification Hub and supports the following push notification platforms:
- Android
- iOS
- Check-out master branch from repository;
- Add a reference to Softeq.NetKit.Services.PushNotifications into target project.
Add Notification Hub settings to your appsettings.json file:
"Notifications": {
"Push": {
"NotificationHub": {
"ConnectionString": "CONN_STR",
"HubName": "HUB_NAME"
}
}
}Softeq.NetKit.Services.PushNotifications comes with Autofac Module to register its dependencies.
If Autofac is used on your project, add PushNotificationsModule to the container:
builder.RegisterModule(newSofteq.NetKit.Services.PushNotifications.PushNotificationsModule());If different IoC container is used or manual registration is required, the following registrations need to be performed:
- Set up and register NotificationHub configuration
AzureNotificationHubConfigurationin your DI container
builder.Register(context =>{varconfig=context.Resolve<IConfiguration>();returnnewAzureNotificationHubConfiguration(config["Notifications:Push:NotificationHub:ConnectionString"],config["Notifications:Push:NotificationHub:HubName"]);}).SingleInstance();- Register
IPushNotificationSubscriberimplementation
builder.RegisterType<AzureNotificationHubSender>().As<IPushNotificationSubscriber>();- Register
IPushNotificationSenderimplementation
builder.RegisterType<AzureNotificationHubSubscriber>().As<IPushNotificationSender>();Softeq.NetKit.Services.PushNotifications comes with PushNotificationMessage that represents a simple push message with the following structure;
publicclassPushNotificationMessage{[JsonIgnore]publicintNotificationType{get;set;}[JsonIgnore]publicstringTitle{get;set;}=string.Empty;[JsonIgnore]publicstringBody{get;set;}=string.Empty;[JsonIgnore]publicintBadge{get;set;}=0;[JsonIgnore]publicstringSound{get;set;}="default";publicvirtualstringGetData(){returnJsonConvert.SerializeObject(this);}}If an additional properties are required to be sent with push, create custom push message class and inherit from PushNotificationMessage
publicclassArticleLikedPush:PushNotificationMessage{publicArticleLikedPush(){Title="Article liked.";Body="Someone liked your article. Check it out!";NotificationType=(int)PushNotificationType.ArticleLiked;}[JsonProperty("articleId")]publicGuidArticleId{get;set;}[JsonProperty("newsHeader")]publicstringNewsHeader{get;set;}}Project supports client-side message localization by providing platform-specific localization keys:
- For iOS -
loc-key\loc-argsandtitle-loc-key\title-loc-args - For Android -
body_loc_key\body_loc_argsandtitle_loc_key\title_loc_args
To enable client-size localization for a particular message, BodyLocalizationKey and TitleLocalizationKey properties need to be initialized with valid resource names, available in client:
publicclassArticleLikedPush:PushNotificationMessage{privatestaticstring_bodyLocalizationKey="article_Liked_body";privatestaticstring_titleLocalizationKey="article_Liked_title";publicArticleLikedPush(){BodyLocalizationKey=_bodyLocalizationKey;TitleLocalizationKey=_titleLocalizationKey;NotificationType=(int)PushNotificationType.ArticleLiked;}[JsonProperty("articleId")]publicGuidArticleId{get;set;}[JsonProperty("newsHeader")]publicstringNewsHeader{get;set;}[JsonProperty("userIdWhoLikedArticle")]publicstringUserIdWhoLikedArticle{get;set;}[JsonIgnore][LocalizationParameter(LocalizationTarget.Title,1)][LocalizationParameter(LocalizationTarget.Body,1)]publicstringUserNameWhoLikedArticle{get;set;}}Inject IPushNotificationSubscriber into your service to subscribe or unsubscribe user device from push notifications.
Inject IPushNotificationSender into your service to send push notifications to registered devices.
This project is maintained by Softeq Development Corp.
We specialize in .NET core applications.
We welcome any contributions.
The Query Utils project is available for free use, as described by the LICENSE (MIT).