Fun with iPhone push
Wednesday, June 24, 2009 at 7:57AM
ViM



Yesterday the AIM messenger on the iPhone was launched with push service. When you install and run the app, it will ask you if you want push enabled. Push will keep a persistent connection with the APNS (Apple Push Notification Servers) of Apple. Any app that uses push, will use these servers to deliver messages to your phone.

The fun thing is that you don't have to run the app itself anymore to receive messages. You can 'turn off' the iPhone and still get a notification for the app on which you have enabled push on. So I was wondering what you could do with push and came up with an cheap web2iphone script that i've put on my personal blog. Members can post a message in a box and it will 'instantly' (within a couple of seconds) be delivered to my iPhone. You will need a iPhone with a dataplan to get this working. Switching off the iPhone will set it to use the gprs or 3G connection of the phone. I tested it overnight and the push app seemed to use about 15kb per 12 hours. 

 

Here's the setup: I've got AIM running on my home computer and a process that is 'tail-ing' a file remotely via ssh:

bash-3.2$ ssh vim@remote "tail -f /var/www/html/datafile.txt" >> messages.log

On my webserver if made a simple php script that writes the text that is submitted in the form to a file called datafile.txt. The script isn't that special, but to be complete, i'll include it here:

<?php
// Get the form variables using POST
$post=stripslashes($_POST['Message']);
$time = time();
$date = date('l jS \of F Y h:i:s A', $time) ."\n";

// write to the file
$myfile="datafile.txt";
$fh = fopen($myfile, 'a') or die("can't open file");
fwrite($fh, "MESSAGE $post $date
");
fclose($fh);

echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
<html>
<head>
<title>Your Page Title</title>
<meta http-equiv=\"REFRESH\" content=\"3;url=http://originalurl\"></HEAD>
<body>
Text Send
</body>
</html>";
?>

Next we need a script to send the new messages to the running AIM app. For that i wrote a bash script that communicates with iChat using applescript (osascript). iChat support AIM accounts (oscar protocol). The bash script will tail the 'messages.log' file and will send a message when a new line is written to the messages.log. 

#!/bin/bash
# tailing messages.log until a string is found
#

tail -0f messages.log | (
while true; do
  read var; printf "[%s]\n" $var
  if [[ $var = *MESSAGE* ]]; then
    echo "Found MESSAGE line";
   HOUR=`TZ=GMT+12 date +%H`
   #We don't want our nights to be disturbed
   if [[ $HOUR =~ (23|24|00|01|02|03|04|05|06|07) ]]
        then
        echo "HOUR = between 23 and 07"
    else
   #limit the number of characters to 180 and strip the MESSAGE from the beginning
   STRIPPED=`echo $var| sed -e 's/^MESSAGE//' |  sed -e 's/^\(.\{180\}\).*/\1/'`

   #communicate with iChat
   echo "tell application \"iChat\" to send \"$STRIPPED\" to account \"AIM:youraimname\"" | /usr/bin/osascript
  fi
fi;
done
)

That's it. No need for SMS messages anymore :) Here's a ugly simplified picture to explain it:

 

Article originally appeared on dutchtechies (http://dutchtechies.com/).
See website for complete article licensing information.