How to Send a Push Notification from Terminal

Let your Mac notify your iPhone or Apple Watch when it’s ready for you

by Colter Reed
2:07 read (647 words)
Get more done by telling your computer to alert when it has finished its task, instead of waiting for it.
by Colter Reed
2:07 read (647 words)
Get more done by telling your computer to alert when it has finished its task, instead of waiting for it.

I spend a lot of time working in Terminal. The interface isn’t flashy, but its beautiful in its simplicity. You can do just about anything in it, if you know the magical incantations, and command-line scripting is an art of automation that dates back almost to the beginning of computing.

Most of what you do in Terminal is pretty interactive. Run a command, get a response. Some commands take longer. Video transcoding, downloads, building an app, and even image transcoding can all be tasks that you have to wait for. (I’m looking at you, guetzli. You have some impressive results, but I can’t wait for HEIF images to be more widely supported.)

It’s hard to stay focused when we’re just waiting. If we let our attention wander, the pipeline is going to stall. Even if we’re doing something else while we wait, it’s easy for the computer to finish its task long before we notice.

One simple solution is to have the computer make noise when it’s finished. This can be as simple as having it beep after the command has finished.

% long_command && print \\a
% long_command && osascript -e beep

You can also use the built-in afplay command to play any sound on your computer.

% long_command && afplay /System/Library/Sounds/Submarine.aiff
% long_command && afplay ~/Library/Sounds/TaDa.m4a

Are you waiting for multiple commands to finish? Instead of overloading the same sound or remembering which sound maps to which task (which is possible), why not let the computer tell you which task finished?

% long_command && say "Image compression complete."

Of course, these all assume that you don’t have sound muted (and that the command finishes successfully).

What if you’ve wandered off and are no longer within earshot of your computer?

What if you could have your computer tap you on the wrist, wherever you were?

Enter PushCut.

PushCut is an app for iOS that lets you perform actions in response to events. For now, let’s just look at how you can use PushCut to set up your own push notification that something has happened.

PushCut is a free download and you can get started without a subscription. (I signed up for a subscription right away because I want to switch HomeKit scenes straight from the actions. It’s nice.)

First, we need to create the action to invoke when the job completes. Tap the + button to add an action, then fill in the Name and Notification details. That’s all we need for now. “Name” is how you identify the task in the app. The “Notification” details are what appears in the push notification. (Tap “Test Notification” if you want to see what it will look like.) Tap “Add” to create the notification.

The new notification template. This is just to let you know something has finished, so you don’t need to add any actions.

Once the notification is complete, the Webhook URL will turn blue. Tap on it. An action sheet will appear. If your Mac and iPhone are using the same iCloud account, all you need to do is copy the curl command; Handoff will send the command over to your Mac’s pasteboard. (It’s like magic.)

Tap the URL to copy it.

Open up Terminal, hit ⌘V, and your curl command will appear!

(If it doesn’t, it’s curl --request POST [webhook-url]; you’ll need to use the Share action to get the URL over to your Mac.)

Now the magic starts to come together. Run the command.

A push notification should appear on your phone.

It’s ready when you are.

Personally, I think that’s pretty cool right there. You can run a command on your Mac and get a notification on your phone. That’s a worthy tool for your toolbox.

Let’s wrap that CURL command so it’s easier to reuse, shall we?

Add the following line to the end of your .zshrc file (or .bash_profile, if you’re sticking to the Old Ways):

alias notify_phone="curl --request POST [webhook-url]"

Now run source ~/.zshrc (or open a new Terminal window) and run notify_phone.

Did you get the push notification on your phone?

Good. 👍

Now we can easily trigger a push notification when our long-running command is finished.

% long_command && notify_phone

By default, this will always send the same message. If you spring for a Pro subscription, you can reuse the same notification to send any message. You can specify a custom message title by adding a JSON body to the POST request you send.

curl --request POST --header "Content-Type: application/json" --data '{"title":"The Wait is Over","text":"Image compression complete: 634K reduced to 388K (39%).","sound":"jobDone"}' [webhook-url]

Want a quick pomodoro timer?

sleep 1200 && curl --request POST --header "Content-Type: application/json" --data '{"title":"Time for a break!","text":"Your pomodoro is complete.","sound":"jobDone"}' [webhook-url]

(There is more you can do.)

You don’t even need to run this from your Mac. You can send yourself a notification from a PC, a server halfway around the world, or even services like Zapier and IFTTT.

One word of caution: anyone can invoke this to send a notification to your phone. If it gets into the wild, someone could use it to annoy you, harrass you, or run your phone’s battery into the ground. Be careful where you put it.

There you have it. With a simple && notify_phone you can be notified on your phone when a long-running command is finished on your Mac. If you have an Apple Watch, you will automatically receive the notification on your wrist.

Gone are the days of having to sit next to your computer while waiting for a job to finish. You can step away from your desk, work on your golf swing, or take a nap read a book. Instead of having to spend your attention on watching for a job to finish, you can focus on something more interesting. Let your computer tell you when it’s done.

Question: What workflows does this enable for you? Share your thoughts in the comments, on Twitter, LinkedIn, or Facebook.