diff --git a/README.md b/README.md index 8774b8d..3baa7b8 100644 --- a/README.md +++ b/README.md @@ -1 +1,46 @@ -# Notifications \ No newline at end of file +# Notifications +Implementation of the [Notification API](https://developer.mozilla.org/en-US/docs/Web/API/notification) in C# for [Blazor](https://github.com/aspnet/Blazor) via Interop. + +## Demo +There is a sample application in /tests/ folder +For some other references of what the API does see the example [demo](https://web-push-book.gauntface.com/demos/notification-examples/) + +## Installation + +Coming Soon (waiting on the nuspec/ci) + +## Usage + +### Add INotificationService via DI +> Scoped by default. +```csharp +public void ConfigureServices(IServiceCollection services) +{ + services.AddNotifications(); +} +``` + +### Inject into component/pages +```csharp +@using Blazor.Extensions +@inject INotificationService NotificationService +``` + +### Create a notification +```csharp +NotificationOptions options = new NotificationOptions +{ + Body = body, + Icon = icon, +}; + +await NotificationService.CreateAsync(title, options); +``` + ### Browser Support +```csharp +bool IsSupportedByBrowser = NotificationService.IsSupportedByBrowserAsync; +``` +### Request Permission +```csharp +PermissionType permission = await NotificationService.RequestPermissionAsync(); +``` diff --git a/src/Blazor.Extensions.Notifications.JS/Blazor.Extensions.Notifications.JS.csproj b/src/Blazor.Extensions.Notifications.JS/Blazor.Extensions.Notifications.JS.csproj index f4bea42..ec40a37 100644 --- a/src/Blazor.Extensions.Notifications.JS/Blazor.Extensions.Notifications.JS.csproj +++ b/src/Blazor.Extensions.Notifications.JS/Blazor.Extensions.Notifications.JS.csproj @@ -21,8 +21,7 @@ - - + diff --git a/src/Blazor.Extensions.Notifications.JS/dist/blazor.extensions.notifications.js b/src/Blazor.Extensions.Notifications.JS/dist/blazor.extensions.notifications.js index e721e29..a52c6b7 100644 --- a/src/Blazor.Extensions.Notifications.JS/dist/blazor.extensions.notifications.js +++ b/src/Blazor.Extensions.Notifications.JS/dist/blazor.extensions.notifications.js @@ -1,2 +1,2 @@ -!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),n(1)},function(e,t,n){"use strict";"undefined"==typeof window||window.BlazorExtensions||(window.BlazorExtensions={})}]); -//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vd2VicGFjay9ib290c3RyYXAiLCJ3ZWJwYWNrOi8vLy4vc3JjL0luaXRpYWxpemUudHMiLCJ3ZWJwYWNrOi8vLy4vc3JjL0dsb2JhbEV4cG9ydHMudHMiXSwibmFtZXMiOlsiaW5zdGFsbGVkTW9kdWxlcyIsIl9fd2VicGFja19yZXF1aXJlX18iLCJtb2R1bGVJZCIsImV4cG9ydHMiLCJtb2R1bGUiLCJpIiwibCIsIm1vZHVsZXMiLCJjYWxsIiwibSIsImMiLCJkIiwibmFtZSIsImdldHRlciIsIm8iLCJPYmplY3QiLCJkZWZpbmVQcm9wZXJ0eSIsImVudW1lcmFibGUiLCJnZXQiLCJyIiwiU3ltYm9sIiwidG9TdHJpbmdUYWciLCJ2YWx1ZSIsInQiLCJtb2RlIiwiX19lc01vZHVsZSIsIm5zIiwiY3JlYXRlIiw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cm9tIEpTXHJcbiAgICB3aW5kb3dbJ0JsYXpvckV4dGVuc2lvbnMnXSA9IHtcclxuICAgIH07XHJcbn1cclxuIl0sInNvdXJjZVJvb3QiOiIifQ== \ No newline at end of file +!function(e){var t={};function n(o){if(t[o])return t[o].exports;var r=t[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,n),r.l=!0,r.exports}n.m=e,n.c=t,n.d=function(e,t,o){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)n.d(o,r,function(t){return e[t]}.bind(null,r));return o},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";var o=this&&this.__assign||Object.assign||function(e){for(var t,n=1,o=arguments.length;n element, make the - // following APIs available in global scope for invocation from JS - window['BlazorExtensions'] = { - }; -} diff --git a/src/Blazor.Extensions.Notifications.JS/src/Initialize.ts b/src/Blazor.Extensions.Notifications.JS/src/Initialize.ts index 87ed2e0..826bf30 100644 --- a/src/Blazor.Extensions.Notifications.JS/src/Initialize.ts +++ b/src/Blazor.Extensions.Notifications.JS/src/Initialize.ts @@ -1,8 +1,26 @@ -import './GlobalExports'; +import { NotificationsManager } from './NotificationService'; -// import { HubConnectionManager } from './HubConnectionManager'; +namespace Notifications { + const blazorExtensions: string = 'BlazorExtensions'; + // define what this extension adds to the window object inside BlazorExtensions + const extensionObject = { + Notifications: new NotificationsManager() + }; -// "use strict"; -// HubConnectionManager.initialize(); + export function initialize(): void { + if (typeof window !== 'undefined' && !window[blazorExtensions]) { + // when the library is loaded in a browser via a