Ludovic Frank - Freelance developer

NTFY.sh, easily send push notifications to smartphones and computers

ionicons-v5-k Ludovic Frank Aug 29, 2023
101 reads Level:

Hi there!

Here we are, it's back to school time, and as the first article of the season, we're going to talk about push notifications.

Here, we're not going to use the basic example of a notification that consists of sending a "successful backup" notification to your own phone, but we're going to talk about a notification that is sent by an application that keeps me going ... So, we're talking about a "product" in production and used every day by thousands of users, but there's a but: it's not to these users that we're going to send the notifications...

You already know the application in question, which is my restaurant reservation application running on Symfony with Symfony-UX/Turbo .

The problem is as follows: when reservations are made, it's not a problem that they arrive as they go along and are displayed in an interface designed for this purpose by the restaurant owners, but the problem is when they are cancelled, because a restaurant has a lot of staff, a lot of people and a lot of things to manage. Generally, the restaurant takes the reservations in a paper booklet next to it, so as to have everything in the same place.
So when there's a cancellation, the sooner they know about it, the better.

My problem was that I didn't have the time to code a push notification system in-house, in fact, for the web alone, if I could have, but the employees involved have different terminals: iOS, Android and Windows (so the web).

What is push notification?

Historically, push notifications didn't exist on the web, but over the last few years they have, whether with websockets or push notifications, which are used in a very commercial way, if you know what I mean, to get you to come back to Twitter for example.

Push notifications are, as the name suggests, pushed to the user's device. There are several protocols, on Android, on IOS and on the Web.e is simply that an event occurring on the server triggers the sending of a notification to the user, a small message with a "clickable link".

By its very nature, the push notification protocol is centralized: if you want to send a push notification on Chrome, you have to send an HTTP request to Chrome's notification services, the same goes for Safari... and I'm not even talking about iOS.

What is NTFY.sh?

If you've followed the previous paragraph, you'll have understood that if, for example, I want to send a "native" notification on iOS, then I have to "code" for iOS, the same for Android and the same for "Web Push", which is a standard.

NTFY consists of an Android client, an iOS client and a server, as well as a web front-end.

interface de NTFY

This interface allows you to test notification sending with a graphical interface.

As you've probably noticed, NTFY is an Open Source project, so it's possible to install it on your own infra-structure (server), which is what I've done myself, because the notifications that are sent out are nobody's business but the restaurant owners'?

Install NTFY on your server

To do this, nothing could be simpler: you have a docker image at your disposal. In addition to the docker image, you can find this little docker-compose, to understand how it works.

I won't go into too much detail here, as the use of docker is not the focus of this article?

Sending push notifications

NTFY works with "topics", think of it as a unique identifier, which defines a "subject", for example you could create a topic "cancellation-ws74g848raf" for notifications concerning cancellations, here I've added random characters to the name.In this case, I've added random characters, as the topic should be seen as a "password": anyone who knows this topic will be able to read notifications from it, so it's best to keep it secret.

Basic operation of NTFY

Ntfy works simply with HTTP, POST and PUT requests. To send a notification with CURL, you can test this:

Here, all subscribers to "YourTopic" will receive the "Hey there" notification.

NTFY with Symfony

Well, yes, of course... in my case I needed to use it with Symfony... so we'll look at the two methods, depending on your PHP version.

With PHP 8.0 and above

There's simply a library for that

Once installed, it's easy to use

Here's how you can use it in a Symfony "eventSubscriber".

Please note that my "eventSubscriber" is "synchronous", which means that if the event is triggered during an HTTP request, it will slow down loading time considerably. Think of Symfony's messenger to send your notification asynchronously.

For versions below PHP 8.0

Come on, don't pretend. We all know you've got projects still running on PHP 5?
For this kind of project, we'll simply use Symfony's HTTP client

Oh yes, right away, it's not as pretty, but it's functional and will last as long as you update your project?

As with recent versions of PHP, think of asynchronous?

Integration with Symfony's notification system

For those who want to integrate NTFY with Symfony's notification system, take a look here.

the iOS case

Ah well, as always, iOS is a bit special, and so are notifications.
To send notifications on iOS, you have to go through Apple's notification system. Fortunately, NTFY's dev has thought of this: in your server's parameters (server.yml file), remember to set: upstream-base-url: "https://ntfy.sh".

This will enable you to use the NTFY store application with your server, without having to reconfigure everything with the Apple pattern service.

Does this mean my messages go through the NTFY server?
No sensitive data goes through the NTFY server, a hash of the complete URL of your topic is created as a topic on the ntfy.sh server, then only the id of your notification goes through e "topic", which the phone will use to fetch the content of the notification from your own server.
For more details on the mechanism, click here.

Safari (iOS), NTFY and web push notifications

Yes, Safari on iOS now supports web push, but you have to put the PWA on the smartphone's home screen.Knowing Apple and their "vision" of standards, I preferred not to test too much, because as I said above, these notifications for me are in production, so I prefer to use Apple's native notification service for the moment.
When I have more experience with Safari notifications, I'll start using it in production and update this article.

iOS notification with NTFY

What about Android?

Of course, Android handles web notifications when it's open.
If you want to use the app from the store, you have two choices: either, as on iOS, use the system specific to Android, exposed by Google, or you can let the app run in the background and fetch the new notifications. The advantage of the second solution is decentralization, but obviously it comes at a cost, and in this case, the cost is battery power.

Conclusion

NTFY allows you to have your own notification server. In my case, it's been very useful and has saved me a lot of unnecessary headaches over things that aren't that important, allowing me to keep my attention on things that are more important to my customers....
Thank you for reading this article and see you next time?