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


No comments:

Post a Comment