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.
Infos
- ntfy: https://ntfy.sh
- Caddy: https://caddyserver.com
- F-Droid: https://f-droid.org
- curl: https://curl.se
- Markdown: https://www.markdownguide.org
- Android broadcast intent: https://developer.android.com/develop/background-work/background-tasks/broadcasts
- ntfy releases: https://github.com/binwiederhier/ntfy/releases
- ntfy documentation: https://docs.ntfy.sh
- Requests: https://requests.readthedocs.io
« Previous 1 2 3 4
Buy this article as PDF
(incl. VAT)