Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp Net Core Web Push Notifications

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:

  1. HTTP + old browsers + IE all versions - Use SignalR + render Notification using html+js
  2. HTTP + Modern browsers (Chrome, Firefox, Safary, Opera?, Edge) with support of Notification API. - Use SignalR and trigger native browser notification with js using new Notification('Message')
  3. HTTPS + Chrome (Mobile) with support of Push API to trigger native notification for closed site using service-workers. Mobile version of Chrome can create notifications only by using service-worker.

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

  • No offline clients notifications support
  • No mobile support
  • For Chrome v62+ moving all project to https - link
  • Using SignalR (0.2.0) for sending push to online clients
  • Using pnotify v3+ to show native desctop or html notifications.
  • Waiting for pnotify v4.0.0 (author promises chrome mobile support. Issue)
  • Waiting for .NET Core 2.1 with SignalR 1.0 to rewrite whole project to it.
like image 385
aleha Avatar asked Feb 17 '17 17:02

aleha


People also ask

Are web push notifications effective?

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.


1 Answers

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); } 
like image 79
Ángela Avatar answered Sep 28 '22 04:09

Ángela