Push notification directly using PHP

Push notifications are a powerful way to keep yourself or others updated in real-time. With ntfy.sh, you get a simple and efficient solution for sending notifications without complex setup. In this article, we’ll go through what ntfy.sh is and how you can use it to send notifications from various platforms.

What is ntfy.sh?
ntfy.sh is an open-source service that allows you to send push notifications via a simple REST API. It works across multiple platforms and can be used for both personal and professional purposes. You can send notifications directly to your phone, browser, or other clients, and it requires no advanced setup.

Getting started:

  • Install the app
    You can find the ntfy.sh app for both iPhone and Android. Once installed, simply subscribe to your topic.

  • Subscribe to one or more topics
    A topic is the URL you end up using to send notifications. As a free user, you cannot have private topics, but for just $5 per month, you can. Alternatively, you can get creative with your topics, such as using a hash of a text string. For example, “cc00c0a9010d507141e83536af19f16d,” which is an MD5 hash for “this is an MD5 hash.” Chances are, no one else will subscribe to that 😉.

  • Start sending notifications
    Now, you can actually start sending notifications to your topic. I’ll provide some examples of how you can do this programmatically 😄.

Very simple with cURL:

 curl -d "Hello from ntfy.sh!" https://ntfy.sh/supersecrettopic

I personally use ntfy.sh to send notifications when I generate leads through my affiliate websites 🤑. I’ve packaged it into a Composer package – tykfyr/ntfy-php. Here’s how you install the package:

composer require tykfyr/ntfy-php

You’re pretty much set up to start sending notifications directly to your phone with PHP. Pretty smart, right?

The next step — the fun part — is to start sending notifications. But how do we do that? Here’s a simple code snippet that uses my Composer package:

use Tykfyr\Ntfy\Client;

$ntfy = new Client('supersecrettopic');

$ntfy->send('Backup job finished!');
$ntfy->send('Deployment failed!', [
    'priority' => 5,
    'title' => '🚨 Error in deployment',
    'tags' => ['warning', 'deploy'],
    'click' => 'https://example.com/status'
]);

You can read more on ntfy.sh, and their documentation (for the tech-savvy) can be found at docs.ntfy.sh.

Leave a Comment