mtgred / netrunner

http://www.jinteki.net
Other
885 stars 388 forks source link

Implement a Web Push Notifications for when someone joins your room. #7518

Open Larrea-Mon opened 2 weeks ago

Larrea-Mon commented 2 weeks ago

Web Push notifications are the kinds of notifications that Discord or many websites send, and show up at the bottom right of your screen on Windows. I would love to be sent a gentle reminder that someone has joined my jnet room. This would also help cut down on the amount of abandoned rooms in jnet.

NBKelly commented 2 weeks ago

That sounds like a neat idea

acollign commented 2 weeks ago

That sounds like a neat idea

Agreed!

I gave it a shot and came up with a rather simple solution. The patch below fires a notification whenever someone enters the lobby. This is triggered right before the "ting" sound is played.

diff --git a/src/cljs/nr/lobby.cljs b/src/cljs/nr/lobby.cljs
index a13ef4068..3112bd7c1 100644
--- a/src/cljs/nr/lobby.cljs
+++ b/src/cljs/nr/lobby.cljs
@@ -20,6 +20,10 @@
    [reagent.core :as r]
    [taoensso.sente :as sente]))

+(defn notify-presence []
+  (.requestPermission js/Notification)
+  (js/Notification. "Someone has entered the lobby" #js {:body "Good luck and have fun!" :icon "/img/icons/jinteki_167.png"}))
+
 (defmethod ws/event-msg-handler :lobby/list [{data :?data}]
   (swap! app-state assoc :games data))

@@ -30,6 +34,7 @@
       (ws/ws-send! [:game/resync {:gameid (:gameid data)}]))))

 (defmethod ws/event-msg-handler :lobby/notification [{data :?data}]
+  (notify-presence)
   (play-sound data))

 (defmethod ws/event-msg-handler :lobby/toast [{{:keys [message type]} :?data}]

As you can see, the notify-presence function does two things a) ask for permission to notify b) create the notification which will fire if that action was granted. Asking the permission will open the browser pop-up which gives the user the option to either allow or always block. Subsequent calls to the the permission function is a nil operation ensuring that the user is not disturbed once she or he made her or his choice. It is then possible to clear out the permission using the browser UI.

It is important to note that this notification is sent to the person who created the lobby and the person joining it like the "ting" sound is played. For this reason, both the title and the body are generic.

@NBKelly would you say that this approach is worth a PR? I will be happy to contribute one and make the necessary changes.

References:

NBKelly commented 2 weeks ago

@acollign Yeah, go for it. Never feel like you need to ask permission to contribute.

acollign commented 2 weeks ago

Yeah, go for it. Never feel like you need to ask permission to contribute.

Excellent, PR is ready for review #7525! Note that Chrome will not allow notifications in Incognito mode. I am telling that so you don't have difficulties like me to validate the feature :-).