Sending push notifications with ntfy

Using Other Clients

Curl isn't the only usable tool to send notifications to your ntfy server. The ntfy binary operates both as a server and a client. Earlier, I used the ntfy client to create users and give them access to specific topics. I executed the ntfy binary in the Docker container, which was the same binary as the one running as a server. You can also install the binary as a DEB or RPM package from the ntfy releases [7] on GitHub. After this, define your custom host and the default username and password in ~/.config/ntfy/client.yml:

default-host: https://ntfy.example.com
default-user: admin
default-password: password

You can then publish a message to a topic with

$ ntfy publish mytopic "This is my message"

And subscribe to a topic with

$ ntfy subscribe mytopic

The command will stay listening indefinitely, printing a JSON representation of every message published to the topic. Additionally, a command can be executed for every incoming message, for instance:

$ ntfy subscribe backup 'notify-send "$message"'

The ntfy documentation [8] provides numerous examples. It also shows how to publish or subscribe to topics using several programming languages. For instance, the Python code in Listing 7 sends a notification regarding a backup error through the Requests [9] library.

Listing 7

Sending a ntfy Message from Python

01 import requests
02
03 requests.post("http://ntfy.example.com/backup",
04     data="Backup unsuccessful",
05     headers={
06         "Authorization": "Basic YmFja3VwOnBhc3N3b3Jk",
07         "Title": "Backup failure",
08         "Priority": "urgent",
09         "Tags": "warning,skull"
10     })

The Authorization header uses Base64 to encode the colon-separated username:password pair. You can generate this encoded string with

echo "Basic $(echo -n 'backup:password' | base64)"

Conclusion

Ntfy is a powerful tool for sending notifications from various sources while staying in complete control. I didn't cover all of ntfy's functionality in this article. For instance, ntfy can forward messages via email using a configured SMTP server. Ntfy can also run a lightweight SMTP server. With this setup, users can send emails to an email address specific to a topic to publish messages to that topic. This is a useful feature for services that support notifications via email but not via HTTP.

You can also use ntfy to send topics via an HTTP GET request if a program doesn't support HTTP POST. Other options include publishing messages as JSON if a program doesn't allow you to add custom headers to HTTP requests, scheduling the delivery of messages, and using message templates. All these features are extensively documented on ntfy's website, including many examples for integrations with other software.

The Author

Koen Vervloesem has been writing about Linux and open source, computer security, privacy, programming, artificial intelligence, and the Internet of Things for more than 25 years. He holds master's degrees in computer science engineering and philosophy and is teaching Linux, Python, and IoT classes. You can find more on his website at http://koen.vervloesem.eu.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News