Monday, September 26, 2011

Adobe AIR for Mobile: Adventures in Frustration

For work I am currently banging my head on an Adobe AIR project that will be deployed into iOS and Android phone/tablet apps.

There seem's to be an ever growing list of things that just "don't work right" in the language (at least compared to if I was writing things natively.).. and unfortunately the documentation/community is severely lacking.

Recently I was 100% stumped by a single build error for an entire weekend - it came out of no where, and it makes no sense at all. (I only found the solution by painstakingly merging changes from my current code into a 3 day old build, and building after each change).

The build issue:


When doing a release build of my AIR project in Flash Builder 4.5 I was receiving the strangest error ever:
Error occurred while packaging the application: Warning: 'function nextResult' contains a verify error at offset 757174. The body of this method will be replaced with 'throw VerifyError.


There was a nice long stack trace included as well, that pointed to absolutely nothing in my own code - (no way I am going to type this guy up!)



So what was the culprit? The code below (note, not even part of the referenced nextResult function!)

Bad Code (???)
[javascript]
switch(currentState)
{
case "Splash2":
{
// restart_clickHandler(null);
// break;
}
case "LangLoc":
{
restart_clickHandler(null);
break;
}
default:
{
break;
}
}
[/javascript]

After tweaking the case block below to have its own break (see below) statement, the Export Release Build worked perfectly (I think).

Good Code
[javascript]
switch(currentState)
{
case "Splash2":
{
restart_clickHandler(null);
break;
}
case "LangLoc":
{
restart_clickHandler(null);
break;
}
default:
{
break;
}
}
[/javascript]

Not cool Adobe. Not cool.

Thursday, September 01, 2011

Android: Testing C2DM service from a shell

Recently I did some debugging of an existing Google C2DM infrastructure for an Android project. I wanted to test to see if the Android application was actually capable of receiving "messages" from the Google C2DM servers.



Android Cloud to Device Messaging (C2DM) is a service that helps developers send data from servers to their applications on Android devices. The service provides a simple, lightweight mechanism that servers can use to tell mobile applications to contact the server directly, to fetch updated application or user data. The C2DM service handles all aspects of queueing of messages and delivery to the target application running on the target device.


Basically the infrastructure is: Hosted servers (on your backend) send messages to Google C2DM service, and Google pushes those messages to the appropriately registered Android handsets.

I did not have access to the backend infrastructure, but wanted to send messages to my handset anyway.

The following is a bash script which you can use to push messages to Google's C2DM service, assuming you have the AUTH key for the backend server and the Registration ID of the receiving handset.

[shell]
#/bin/bash

REG_KEY=APA91bHuJQtqbrbkA......QFzpaJPuXY

AUTH_KEY_NPIKE=DQAAAO8........AC4P0

AUTH_KEY=$AUTH_KEY_NPIKE


#echo
echo Send Game Alert to Device
echo test_c2dm_sendGameAlert.sh [waitTime] [device_reg_key] [auth_key]
echo

# reg key
if [ ! -z $2 ];
then
REG_KEY=$2
echo Using user provided device registration key of: $REG_KEY
echo
fi

# auth key
if [ ! -z $3 ];
then
AUTH_KEY=$3
echo Using user provided auth key of: $AUTH_KEY
echo
fi

if [ ! -z $1 ];
then
WAIT=$1
echo Will wait for $WAIT seconds before sending C2DM message.

for (( c=1; c<=$WAIT; c++ ))
do
DELAY=$(($WAIT - $c))
echo -ne "$DELAY "

sleep 1
done
fi





echo Sending C2DM message to Google..
echo
C2DM_RESPONSE=`curl "https://android.apis.google.com/c2dm/send" -d "registration_id=$REG_KEY" -d "collapse_key=1" -d "data.mediaActionKey=Go There" -d "data.alert=New England Patrios win the Superbowl!very long very long very long very long very long very long very long very long" -d "data.mediaId=2010090900" -H "Authorization: GoogleLogin auth=$AUTH_KEY" -s`

echo C2DM Response:
echo $C2DM_RESPONSE

echo
echo Complete.
[/shell]

You can either hardcode in the appropriate keys, or you can pass them as arguments.


./test_c2dm_sendGameAlert.s 0 myDeviceKey myServerAuthKey


The very first argument is a "delay" time - hand if you want to kick off the script, and then run into your manager's office to say: "Hey check out this notification from the Google C2DM service!"


./test_c2dm_sendGameAlert.s 30