Saturday, May 24, 2014

How to send notifications from Raspberry Pi to your Android Phone (Free!)

Ever wanted to send notifications from your hardware project to your phone but didn't wanted to code it all up. Well I wanted to do the same so I created a simple app to do just that. 
A few simple steps and you will be pushing on custom messages into your phone. There is no registration required and the app has the least permissions to get the job done. There is no user information saved on the server, just the id you see on the screen and the Google registered id of the device so a message can be sent to it.

How to? 
1. Install RemoteAlert app from the android market. The app will show a big orange number on the main activity. 
2. All needed to do is to do a HTTP post with that number and you should get a message on the notification of the phone. Message may take up to a minute to arrive. There is nothing I can do to send it faster, it is just the way it is.
3. Refer to the samples bellow on how to send the HTTP Post to make it work.

Remember to replace the Id with the id from your own device!

Python sample code:
import json
import urllib2

class RemoteAlert:
    def send(self, device_id, message ):
        data = { 'message' : message }
        url = '' + device_id
        req = urllib2.Request( url )
        req.add_header('Content-Type', 'application/json')
        urllib2.urlopen(req, json.dumps(data))
        return 'OK'

ra = RemoteAlert()
dev_id = 'a32b831c-7623-4c43-99cf-b614ff54e902'

print ra.send(dev_id, 'Hellow Push Message World')

Push message sent from the shell using curl
curl -X POST -H "Content-Type: application/json" -d '{"message":"Hellow Push Message World"}'

If above curl gives you an error (you might be running on windows). Windows seems to not like single quotes around data. Try it like this
curl -H "Content-Type:application/json" -X POST --data "{\"message\":\"Hellow Push Message World\"}" ""

On success you should see this: (in this case, just wait for it to show on your phone)
{"error": "OK", "name": "a32b831c-7623-4c43-99cf-b614ff54e902"}

The above curl sample you can send a notification to your phone, just replace the long id with the one from your phone and write in your message.

These two samples sends exactly the same message. There is so much that can be done to improve this and get a better solution but a friend said to me once "Forget about it, ship it" and keeping the spirit better to have something than nothing. Improvements will be latter added depending in my needs and if the community request it.

Future plans:
1. Formal documentaion
2. Sample codes for different platforms and languages
3. Make it all open source for references. The android app is pretty much the same code from the Google push notification sample and the hosting is on heroku using a free account. I do not know how long this will work if I get too many users and reach the hosting limit. However I feel pretty confident this will hold it's ground and the plan is to keep it free so anyone can have push notifications from their respective projects.

Server Source Code Posted at: