Android and Linux

Monday, July 11, 2011

Pulling data from Notify My Android notifications

Notify My Android (NMA) doesn't have Tasker integration, but we can bridge the gap... sorta.

Data for the app is stored in a database so we can pull out the ever handy sqlite3 and grab it.

sqlite3 /data/data/ "SELECT * from notifications;"

The data is stored in pipe delimited fields with the newest record on top. Here is what it looks like when I send the output of the date command as both the event and description field.

44|141395|Home|Mon Jul 11 20:18:43 EDT 2011|Mon Jul 11 20:18:43 EDT 2011|1310430099|0|1

Assuming we want the 5th field, just tell Tasker to read the line, split the variable then flash %VAR5.

But here's the catch, Tasker can perform an action when the notification comes in but the data doesn't go into the file until a few seconds after the app is opened, after it syncs with it's home server.

The most elegant solution doesn't work. That would be a Tasker context watching for the file to be modified, then running the command to get the new notification. Unfortunately, watching for file modification doesn't seem to work in /data/data.

You could write a script to watch the file for new contents then put something in a file on the sdcard that Tasker could see, but it would be necessary to keep the script looping and if you lost the network connection or something, it could loop indefinitely. You could kill it once the app exits, but this is getting too complicated.

The easy answer is to just add a wait. You'll need a profile watching for the app to open, but disable that profile. Then have a profile watching for the notification. Once the notification comes in, have it enable the "App Open" profile. The App Open profile can carry out a task to

1 wait 5 seconds (adjust if your network connection takes longer)
2 execute: @! sqlite3 /data/data/ "SELECT * from notifications;" > /mnt/sdcard/Tasker/notified
3 read line 1 of Tasker/notified to var %NOTIFIED
4 Variable Split %NOTIFIED splitter: |
5 flash %NOTIFIED5
6 Profile Status set Notified off

Remember, the NMA app must be opened for Tasker to grab the notification data, but with these profiles, when there isn't a notification, it can be opened without triggering the task.

%NOTIFIED5 will contain the main body of the notification. You can select from 3, 4 or 5. They are the application, event and description. All three are necessary to send a notification through NMA and can contain 256, 1000 and 10000 characters respectively. Only the application and as much of the event as possible show in the Android Notification area. The entire event and the description are shown in the app.

Pent has put the ability to read any file system-wide on Tasker's todo list. Until then, I hope someone gets some use out of this.