Asp Net Core Web Push Notifications
Asked Answered
F

2

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.
Flay answered 17/2, 2017 at 17:55 Comment(14)
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.Preussen
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.Flay
You can use Pushpad and its REST API (see POST /projects/PROJECT_ID/notifications).Hayrack
@Hayrack usage of third-party-service is not the best choiceFlay
@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.Hayrack
@aleha did you ever get an answer for this?Lanford
@johnny5 added Current solution section. No progress.Flay
@aleha thanks for your editsLanford
which version of .Net core?Vansickle
@Vansickle v1.1, will wait for core 2.1 to switch whole projectFlay
I'm not sure if this is what you want, but we've been using 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.Fervor
@Á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 furtherFlay
Any progress? Can we use service workers with SignalR now?Thereon
@akmal Unfortunately I do not support this project anymore. It would be interesting to know the result tooFlay
J
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:[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);
}
Jehoash answered 3/12, 2017 at 10:21 Comment(4)
pushEndpoint targeted to googleapis meant that it only will work in chrome?Flay
You would need a different endpoint to target Firefox. In the project's readme there's a table of browser support.Fervor
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 developers.google.com/web/fundamentals/codelabs/… but I need clear undesrtanding of server side implementation in .NET. Thank you!Motorbike
Mayby you can help #62674008Wrap
C
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);
Culbert answered 2/10, 2021 at 14:7 Comment(4)
this library look pretty good, but I doubt about it life time support, gonna give it a try.Basile
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.Culbert
Hi @Andrei. Does this offer any advantages over something like SignalR ? I have this question here and was wondering if this library would help me #78290316Disfigurement
@ash signalR is for real-time websocket-like communication while this library is specifically for push notifications. It's good for the cases when you don't have an open connection with the server e.g. the app is closed.Culbert

© 2022 - 2024 — McMap. All rights reserved.