Ntfy: Your Ultimate Push Notification Powerhouse!

A utility for sending push notifications.

Kamalesh D
6 min readSep 9, 2023

What’s the Buzz About Ntfy?

Send push notifications from your app or script

Easily dispatch push notifications from your application or script by sending messages through either PUT or POST methods. Topics are generated dynamically when you subscribe or send messages to them.

Receive notifications on your phone

Subscribe to a topic and receive notifications, with different priorities, attachments, action buttons, tags & emojis, and even for automation.

Let’s get started by setting up a server.

You can utilize Digital Ocean for this purpose, and make sure to have Docker installed on your local PC.

Installation

  • Initially, you’ll need to install it on your local PC, and subsequently, set it up on an online server such as DigitalOcean.

Local PC Setup

  • I’m going to perform the workflow on macOS.
  • Before we start, let’s create a directory called “ntfy” in /etc/ntfy. Inside this folder, create a server.yml file and paste the code into it. In line 14, change the base-url to match your PC’s IP.
  • Next, run the following Docker-compose.ymlfile. This will generate an image and volume for cache, as well as a ntfy directory for the “server.yml” file.
version: "3.8"

services:
ntfy:
image: binwiederhier/ntfy
container_name: ntfy
command:
- serve
# optional: replace with your own user/group or uid/gid
volumes:
- /var/cache/ntfy:/var/cache/ntfy
- /etc/ntfy:/etc/ntfy
ports:
- 80:80
Local PC
  • So now when you hit the base-url you will see a page like this:
This is in local PC with Local IP

Note

  • If you done have docker install it from here

Ntfy in DigitalOcean Setup

  • Initially, set up an Ubuntu droplet and then install the Docker application on it.
sudo apt install docker.io -y
  • Run the below Docker script to setup the Ntfy application.
docker run -p 80:80 -it binwiederhier/ntfy serve
  • Before we start, let’s create a directory called “ntfy” in /etc/ntfy. Inside this folder, create a server.yml file and paste the code into it. In line 14, change the base-url to match your server-IP(you can use domain name for it )
  • Then Now for persistent cache we add below command
docker run \
-v /var/cache/ntfy:/var/cache/ntfy \
-p 80:80 \
-itd \
binwiederhier/ntfy \
serve \
--cache-file /var/cache/ntfy/cache.db
  • Ntfy will be initialized and operate using the base URL IP.
  • Thus now Ntfy will be functional on both your local PC and the Ubuntu server.
Ubuntu Server

Install Ntfy in Mobile App

  • After installing Ntfy go to setting and add server

First

  • Add ubuntu server ip or domain name and topic name

Similarly

  • Create add another server with local PC ip and topic name
Two topics and two Server(local and ubuntu)

Now lets test

  • In local PC type below code in cli there are various ways to send message.
curl -d "Setup successful" 192.168.20.10/macbook
CLI Local PC
Mobile Notification
  • Similarly go type for ubuntu server IP .
curl -d "Setup successful" <Ubuntu_server>/nfty

Note:

Given that the Ubuntu server’s IP address is public, you can use the curl command in your local terminal. Simply include the IP address of the Ubuntu server, and you will receive notifications on your mobile device under the specified topic. This functionality is feasible because the IP address is public.

However, for the Ntfy setup on your local PC, it relies on your Wi-Fi IP, making it inaccessible for other public users to send messages. To overcome this limitation, you can implement Cloudflare tunnels, and I will provide instructions on how to set this up in the final part of the blog.

curl -d "Setup successful" 192.168.20.10/macbook
  • Attempting the above code on an Ubuntu server won’t yield the desired results; instead, it will display a “refused to connect” error. This is because the server is not on the same network. To resolve this issue, we utilize Cloudflare tunnels.

Now, let’s explore the practical applications of Ntfy for developers.

  1. Suppose you are executing a pipeline script using tools like Ansible, Chef, Puppet, or Jenkins. In such scenarios, you can seamlessly integrate Ntfy to receive notifications upon the successful completion or failure of the code pipeline.

2. I will show you small demo using terminal

  • Type the below code for testing
sudo apt update && apt ugrade && curl -d "update is done" 192.168.31.2/ntfy
Notification
  • You can try using Ntfy for a practical demonstration of sending notifications based on the success or failure of a ping operation.
#!/bin/bash

# Define the target host to ping
target_host="example.com"

# Ping the target host
if ping -c 1 "$target_host" &> /dev/null; then
# Ping successful, send a success notification
curl -d "success" https://ntfy.sh/mytopic
else
# Ping failed, send a failure notification
curl -d "failure" https://ntfy.sh/mytopic
fi

3. To regularly check the battery percentage of your laptop and receive notifications, you can use a cron job along with a script. First, create a script to check the battery status and send a notification when it falls below a certain percentage. Here’s an example script:

#!/bin/bash

# Battery threshold (change this to your desired percentage)
threshold=20

# Get the current battery percentage
battery_percentage=$(acpi -b | awk -F ', ' '{print $2}' | sed 's/%//')

# Check if the battery percentage is below the threshold
if [ "$battery_percentage" -lt "$threshold" ]; then
# Send a notification using Ntfy
ntfy -b pushover send "Low battery warning: $battery_percentage%"
fi

Cloudflare Tunnels

Now, let’s explore the advanced capabilities of Ntfy, including setting up subscriptions for public users using your local Wi-Fi IP address, and we’ll do this by leveraging Cloudflare tunnels for enhanced functionality.

  • To utilize this feature, please log in to your Cloudflare account, ensuring you have a registered domain.
  • Once your domain is added, navigate to the Access section, where you’ll find a page resembling the one below.
  1. Now click Create tunnel and in that choose docker environment.
Docker environment

2. You’ll be provided with a Docker run script; execute this script on your local PC.

docker run cloudflare/cloudflared:latest tunnel --no-autoupdate run --token <Sensitive-token>

3. Next, complete the following details: Set the TYPE to httpand specify the URL as the IP address of your local PC.

4. Save the tunnel .

That’s it! The tunnels have now been established, linking your local IP with a domain name. This enables you to access the domain from anywhere and facilitates both sending and receiving notifications seamlessly.

Reference:

--

--

Kamalesh D

DevOps and Cloud Enthusiast | Cyber Security | Ex-intern@FordMotors | https://dev.to/kamaleshseervi