[back]

Here you will find an easy-to-use solution for sending notifications to your Android phone from your Java processes or Bash scripts. This is ideal for large projects that might run on remote servers where you cannot have constant updates. You can setup your code to send a notification upon termination, or as part of error control (for instance in a catch() statement).

I have chosen to use Notify My Android for this project, but in theory any push notification service will be suitable.

Step 1: Registration and API key

In order to successfully forward the notifications to your phone, you need to register for an account and create an API key that will be used by the app. Once you have the 48-bit key, store it in a file (e.g api-key.txt).


Step 2: Android app

Download/install the Notify My Android app from Google Play store and login using your credentials.


Step 3a: Java code

Copy the code below, or download NotificationSender.java and place it in your classpath.

Make sure to change the name of the application and event in lines 22-23.

To send a notification simply instantiate the Java class with the path to api-key file, and call the notify() method:


NotificationSender notifier = new NotificationSender("api-key.txt");
notifier.notify("Finished running");

NotificationSender.java


				

Step 3b: Bash script

Download notify.sh and place it in your working directory (or alternatively in a directory in your PATH). Place the api-key file in a folder called ".notify" in your home directory, or modify the script to point to a different location.

To send a notification after script execution wrap your script with notify.sh just as you would do with e.g. time:


./notify.sh echo "Hello World!"