Android Push Notifications - AGK Help

Android Push Notifications

Before you begin

You will need a server capable of running PHP scripts for this example to work

Note that these notifications will not work on Amazon or Ouya devices

Push Notifications on Android are handled by the Google Cloud Messaging API, follow the instructions on this page https://developer.android.com/google/gcm/gs.html until you have your API key, ignore the rest. Also make a note of your project number (not your project ID) mentioned on this page. The project number should be all digits like so "295375129356"

In your Google Play developer console you will find a Services and APIs section for your app that allows you to link your Google Cloud Messaging ID to it. When you submit your APK to Google Play you should fill this out with the project number gathered from the above page.

Implementation

Two android projects (interpreter_android, and template_android) are setup to use Push Notifications, the instructions are the same for both of these projects.

In your tier 1 code first call SetPushNotificationKeys(key1,key2) with your project number as the first key and an empty string for the second key, then call PushNotificationSetup(). If this returns 0 then this platform does not support push notifications. Otherwise wait for GetPushNotificationToken() to return something other than an empty string, if it returns "Error" then something went wrong. Once you have the token you need to send it to your server, you might also want to send some identifying information like a userID so you know who this token belongs to. The code we use looks something like this

g_Net_UserID = 01234 // some user ID that your server can use to recognise whos key this is
SetPushNotificationKeys( "0123456789012", "" )
result = PushNotificationSetup()
if ( result = 1 )
    token = GetPushNotificationToken()
    while ( token = "" )
        token = GetPushNotificationToken()
    endwhile
    params$ = "token="+token+"&platform="+getdevicebasename() + "&id=" + str(g_Net_UserID)
    SendHTTPRequestASync( conn, "sendToken.php", params$ ) // we do not need the server to return anything so we can ignore getting the response
endif

In this example we also send the output of GetDeviceBaseName so we know which platform this token belongs to, iOS and Android use different methods. The server will need to remember the token and who it belongs to so you can send them push notifications later, be aware that the tokens can be 183 characters or more in the case of Android. The device takes no further action after this, it simply sends off its token and the server decides when to send a notification.

When you want to send a notification to an Android device use the following PHP script to send a message to a particular device token, replacing the $apiKey field with your API key generated earlier. For example when two users are playing a turn based game and one device notifies your server it has finished its turn, the server can use the user ID to find the token that belongs to the opponent and send them a notification.

function SendPushNotificationAndroid( $deviceToken, $message, $title )
{
    // Replace with real SERVER API key from Google APIs
    $apiKey = "MyAPIKey";
 
    // Set POST variables
    $url = 'https://android.googleapis.com/gcm/send';
 
    $fields = array(
        'registration_ids'  => array( $deviceToken ),
        'data'              => array( "message" => $message, "title" => $title ),
                    );
 
    $headers = array( 
       'Authorization: key=' . $apiKey,
       'Content-Type: application/json'
                    );
 
    // Open connection
    $ch = curl_init();
 
    // Set the url, number of POST vars, POST data
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
 
    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
 
    // Execute post
    $result = curl_exec($ch);
 
    // Close connection
    curl_close($ch);
 
    echo $result;
}