42

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.
aleha_84
  • 8,309
  • 2
  • 38
  • 46
  • 4
    What is your reason for not using SignalR - it will be able to notify your client from the backend, where you then react in the callback with the HTML5 Push API. – Andre Andersen Feb 17 '17 at 18:44
  • Customer wants to see browser notifications on mobile devices and on the desktop from browser, which generated by calling ServiceWorkerRegistration.showNotification from service worker script. So SignallR can call it then site is currently open, but if it closed then Notification API is needed. – aleha_84 Feb 17 '17 at 19:24
  • You can use [Pushpad](https://pushpad.xyz/docs/pushpad_pro_getting_started) and its [REST API](https://pushpad.xyz/docs/rest_api) (see `POST /projects/PROJECT_ID/notifications`). – collimarco Feb 18 '17 at 10:12
  • 1
    @collimarco usage of third-party-service is not the best choice – aleha_84 Feb 18 '17 at 10:30
  • 1
    @aleha sure, the choice depends on your needs. Just note that it relies on the *Push API*, it's not built with strange proprietary protocols. – collimarco Feb 18 '17 at 14:22
  • @aleha did you ever get an answer for this? – johnny 5 Nov 21 '17 at 17:03
  • @johnny5 added Current solution section. No progress. – aleha_84 Nov 22 '17 at 11:06
  • @aleha thanks for your edits – johnny 5 Nov 22 '17 at 13:59
  • which version of .Net core? – aaronR Dec 01 '17 at 22:30
  • @aaronR v1.1, will wait for core 2.1 to switch whole project – aleha_84 Dec 02 '17 at 09:00
  • 1
    I'm not sure if this is what you want, but we've been using [web-push-csharp](https://github.com/web-push-libs/web-push-csharp) for a couple of months to send web pushes with payload. It's a port of the node library you mentioned. It only works for modern Chrome, Firefox and Opera, though. – Ángela Dec 03 '17 at 08:35
  • @Ángela if i understand it right **webPush** is a great solution to notify **offline** users. I will implement it somehow in future. Now i need to notify all **online** users (including old browsers and IOS). Please if you can, create an answer with your example of usage and good description. It can help someone in further – aleha_84 Dec 03 '17 at 10:02
  • Any progress? Can we use service workers with SignalR now? – Akmal Salikhov Jan 22 '19 at 13:28
  • @akmal Unfortunately I do not support this project anymore. It would be interesting to know the result too – aleha_84 Jan 22 '19 at 14:59

2 Answers2

15

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:example@example.com";
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);
}
Ángela
  • 1,405
  • 18
  • 24
  • pushEndpoint targeted to googleapis meant that it only will work in chrome? – aleha_84 Dec 03 '17 at 13:55
  • You would need a different endpoint to target Firefox. In the project's readme there's a table of browser support. – Ángela Dec 03 '17 at 14:03
  • Would you mind to provide any sample code to subscribe to chrome push notifications via asp net mvc / .net core, please? I have started with this labwork https://developers.google.com/web/fundamentals/codelabs/push-notifications/ but I need clear undesrtanding of server side implementation in .NET. Thank you! – NoWar Aug 08 '18 at 03:31
  • Mayby you can help https://stackoverflow.com/questions/62673508/how-web-push-protocol-works – zolty13 Jul 01 '20 at 11:06
3

CorePush library for Web, Android and iOS

I would just use CorePush library for this. It has a very simple interface, it's lightweight and supports everything e.g. Web, iOS and Android. This is how you send Web Push via Firebase:

var fcm = new FcmSender(settings, httpClient);
await fcm.SendAsync(deviceToken, notification);
Andrei
  • 42,814
  • 35
  • 154
  • 218
  • this library look pretty good, but I doubt about it life time support, gonna give it a try. – Bùi Quang Tuấn Sep 19 '22 at 06:44
  • 3
    Hi @BùiQuangTuấn! Thanks for giving it a try! The library is developed by me and is widely used by my company. I'm very certain that it will be supported for a long long time. – Andrei Sep 19 '22 at 09:32