1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
trace("\n\nInitializing GCMPushInterface...");
//create instance of extension interface and add appropriate listeners to it
_gcmi = new GCMPushInterface();
_gcmi.addEventListener(GCMEvent.REGISTERED, handleRegistered, false, 0, true);
_gcmi.addEventListener(GCMEvent.UNREGISTERED, handleUnregistered, false, 0, true);
_gcmi.addEventListener(GCMEvent.MESSAGE, handleMessage, false, 0, true);
_gcmi.addEventListener(GCMEvent.ERROR, handleError, false, 0, true);
_gcmi.addEventListener(GCMEvent.RECOVERABLE_ERROR, handleError, false, 0, true);
register();
private function register():void
{
trace("\n\nRegistering device with GCM...");
//check if device is already registered otherwise start registration process and wait for REGISTERED event
var response:String = _gcmi.register(GCM_SENDER_ID);
trace(response);
if(response.indexOf("registrationID:") != -1)
{
trace("\n\nDevice was already registered.\n" + response);
this.dispatchEvent(new GenericEvents(GenericEventsType.NOTIF,["\n\nDevice was already registered.\n" + response + "\n"]));
//extract GCM registration id for your device from response, you will need that to send messages to the device
//create your own backend service or use public services
_gcmDeviceID = response.substr(response.indexOf(":") + 1);
handleRegistrationIDReceived();
//if device was already registered check if there is any pending payload from GCM
//this can be true when android shut down your app and its restarted instead of being resumed
checkPendingFromLaunchPayload();
}
} |
Partager