Simple Mobile Push Notifications

Posted: September 17, 2024 at 12:00 PM

TL;DR: Check out WPN for code that implements push notifications to your browser (mobile or desktop).

Background

I run my own server/services, so it’s helpful to be able to receive notifications, such as when a hard drive starts throwing errors, or when I get an SMS to my home phone, or when snapaid fails to process a FreeBSD announcement. I had used a Java Signal client for a while, but that required both a phone number, but also a complex Java runtime to keep running, and eventually Signal upgraded the server protocol such that the client I had stopped working, and getting a new client working was too much work.

I was originally thinking about writing a custom Android App that just did this, but thinking about things a bit more, I realized that Web browsers have both a Push API and a Notifications API that I could use. Using an official notification API would keep the battery usage in check unlike using a custom protocol (or I’d have to use Google’s GCM directly, and yes, I do realize that web browsers are likely using it under the hood).

The blog post Push notifications overview has a good overview on how they work.

Overview

With a little bit of research, I found some example code that would register the push api, post the necessary info, and then display the notification received.

There are better guides on this, but the short is that it requests notifications, and once the user grants permission, registers a service worker, and once that service worker is registered, subscribe to the Push API, and POSTs the necessary info to the server. With the information POSTd, there’s a simple client that can be used to push notifications.

With a little bit of work, I got it working and modified to be even more simple by not requiring a Python webserver, but instead use a CGI so that it integrates with your existing web server.

The installation and setup instructions are located in the WPN repository.

| Home |