How to Send Push Notifications to Your Phone From Any Script

Getting notified when tasks complete

Jamie Bullock
Jun 19 · 4 min read
Image for post
Image for post
Photo by Jamie Street on Unsplash

As programmers, we often need to set off long-running tasks and scripts. This may include:

  • Transferring or copying large files
  • Processing big data sets
  • Lengthy compiler processes

With any kind of time-consuming script, it’s convenient if not essential to know when the script finishes. We also want to know whether the process succeeded or failed.

While some software includes these kinds of notifications built-in, most command-line tools don’t. Also, we ideally want to obtain notifications wherever we are, not only on the device where the script is running.

Using IFTTT

Image for post
Image for post
Image courtesy of the author

Sending push notifications to any mobile device can easily be achieved with the web service IFTTT. This can be used to hook up a web request sent to a given URL to a companion app installed on a mobile device.

Here are the steps to achieve that:

  1. Visit http://ifttt.com and sign up for an account.
  2. Click on your account icon (top right), and then click on Create.
  3. Click on the If+ icon and type “Webhooks” in the search box.
  4. Select Webhooks → Receive a Web Request and type “notify” for your event name.
  5. Next, click on the +That icon and type “Notifications” in the search box.
  6. Select “Send a notification from the IFTTT app.”
  7. Type the following text into the Message box:
Notification: {{Value1}}

8. Click ‘Create action’ and then give your action a name, e.g. “Push Notification.” Then click on Finish.

Congratulations! You’ve now created an IFTTT action.

Testing the Notification

To test the notification, you need to download the IFTTT app on your mobile device and sign in with your account. It should prompt whether you wish to receive notifications — select ‘yes.’

Assuming the app is installed correctly on your device, we can now return to the web browser to find the URL for our applet.

To do this, visit https://ifttt.com, and then:

  1. Click on your account icon (top right), and then click on My Services.
  2. Select Webhooks from the list, and then click on Documentation.

A screen should be displayed showing your API URL as well as some fields to pass values via a POST request.

  1. Type “notify” (the name of our event) in the event box.
  2. Type “test” in the Value 1 box.

The resulting command at the bottom should look something like this:

curl -X POST -H "Content-Type: application/json" -d '{"value1":"test"}' https://maker.ifttt.com/trigger/notify/with/key/123jhkjh3k4h24j343

If you click Test It, you should shortly receive a notification on your mobile.

If you don’t receive the notification within 60 seconds, try restarting your device to force the app to prompt for notification authorisation.

If things still don’t work, I recommend posting on the IFTTT Reddit.

Sending Notifications From a Script

OK, so we have IFTTT working. Now for the fun part! We’re going to write a simple bash script that lets us send notifications to our mobile from the command line. This part will work on any Unix-like system such as Linux, macOS, or Cygwin on Windows.

Open a terminal, and then create a new text file called notify. Paste in the following, replacing <your key> with the key from the Webhook documentation:

#!/bin/basharg=$(echo $1 | sed 's/ /%20/g')
curl -s -o /dev/null "https://maker.ifttt.com/trigger/notify/with/key/<your key>?value1=$arg"

Now save the script and make it executable. This can usually be done with:

$ chmod +x notify

Now we can test our script from the command line:

$ ./notify "important stuff"

All being well, you should shortly receive the text “Notification: important stuff” on your mobile device.

Putting It Into Practice

Now that we have a script that can easily post notifications, we first need to put it somewhere the system can easily run it from. I normally use /usr/local/bin for this kind of thing, so:

$ sudo mv notify /usr/local/bin

Assuming /usr/local/bin is in your PATH, then you should now be able to simply type notify "blah" from any directory and it will work.

What can we do with this? Any Unix command can be executed when another has completed using the && shell operator. This is explained in “6 Bash Shell Command Line Chaining Operators in Linux.”

So, for example, we can write:

cp my_big_file /some/folder && notify "File copied successfully"

Or:

make && notify "Code compiled"

However, we often don’t just want to know if something succeeded but also if it failed. Here we can use the || operator, for example:

make && notify "Compile successful" || notify "Compile failed"

Using From Code

In addition to command-line notifications from the shell, we may also want to send notifications from code. Most languages have support for sending requests to web services, either within the language standard library itself or through third-party frameworks.

For example, with Python, we can use the urllib.request module as follows:

from urllib import requestkey = "<your key>"
message = "something"
request.urlopen("https://maker.ifttt.com/trigger/notify/with/key/%s?value1=%s" % (key,message))

In the end, we are just sending a GET request to a URL.

If you have any ideas for useful applications of scripted notifications, I’d love to hear them in the comments.

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store