Main goal is to add to site ability to send web notification to pop up a system notification to alert the user using Html5 Push API and service workers. Not using SignalR which only can run client scripts while site is opened. Also should be ability to send notification if site is closed, as mentioned here - it is possible.
Here is good article about Push API and provided good example But it uses NodeJS as server and web-push component to send requests to notification services.
Can't find any .NET examples. I think about the two workarounds.
First, is to write everything from scratch based on Push API article note about server:
When you send a push message without data, you simply send it to the endpoint URL using an HTTP POST request. However, when the push message contains data, you need to encrypt it, which is quite a complex process.
Second, is to use AspNetCore.NodeServices (article about it) to execute node.js script file
Are there more solutions? Maybe exists ready solution for this?
After subject researching
3 cases:
new Notification('Message')
It's became to complicated. What is wrong?
Possible solutions:
For 1 and 2 cases found this repository. Think it is a good frontend solution with a good fallback support. For 3 case still don't know what to do.
Current solution. Added: 2017-11-22
Used by more than 8 percent of top websites, web push notifications are a widespread and effective communication channel. These messages offer a critical mechanism for websites to reach their users with timely and personalized messages.
The node library you mention has been ported to c#: web-push-csharp.
Here's a simplified usage example taken directly from their site:
var pushEndpoint = @"https://fcm.googleapis.com/fcm/send/efz_TLX_rLU:APA91bE6U0iybLYvv0F3mf6uDLB6...."; var p256dh = @"BKK18ZjtENC4jdhAAg9OfJacySQiDVcXMamy3SKKy7FwJcI5E0DKO9v4V2Pb8NnAPN4EVdmhO............"; var auth = @"fkJatBBEl..............."; var subject = @"mailto:[email protected]"; var publicKey = @"BDjASz8kkVBQJgWcD05uX3VxIs_gSHyuS023jnBoHBgUbg8zIJvTSQytR8MP4Z3-kzcGNVnM..............."; var privateKey = @"mryM-krWj_6IsIMGsd8wNFXGBxnx..............."; var subscription = new PushSubscription(pushEndpoint, p256dh, auth); var vapidDetails = new VapidDetails(subject, publicKey, privateKey); var webPushClient = new WebPushClient(); try { webPushClient.SendNotification(subscription, "payload", vapidDetails); } catch (WebPushException exception) { Console.WriteLine("Http STATUS code" + exception.StatusCode); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With