Android GCM is a native extension that enables the use of GCM (Google Cloud Messaging) in your application. It allows a server to notify your application when important events occur at any time. Additionally when the application resumes or restarts it also gets notified, which is a GCM feature called “fetch message payload after app just launched”.


package com.afterisk.gcmdemo
	import com.afterisk.shared.ane.lib.GCMEvent;
	import com.afterisk.shared.ane.lib.GCMPushInterface;
	import com.bit101.components.PushButton;

	import flash.desktop.NativeApplication;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextField;

	[SWF(frameRate="40", backgroundColor="#000000")]

	public class GCMExample extends Sprite

		private var Picture:Class;

		//your gcm sender id (listed on your app page at google developer portal)
		public static const GCM_SENDER_ID:String = "863211319405";// "yourGCMSenderID";

		private var _gcmi:GCMPushInterface;
		private var _gcmDeviceID:String;
		private var _payload:String;

		private var _messageField:TextField;

		public function GCMExample()
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			this.stage.align = StageAlign.TOP_LEFT;


			this.addEventListener(Event.ADDED_TO_STAGE, handleAddedToStage, false, 0, true);

		private function createUI():void
			var topBmp:Bitmap = new Picture();
			var top:Sprite = new Sprite();
			top.addEventListener(MouseEvent.CLICK, visitGooglePlay, false, 0, true);

			_messageField = new TextField();
			_messageField.width = 400;
			_messageField.height = 300;
			_messageField.multiline = true;
			_messageField.wordWrap = true;
			_messageField.background = true;
			_messageField.text = "GCM Example Log:";
			_messageField.x = 40;
			_messageField.y = 250;

			var but1:PushButton = new PushButton(this, 90, _messageField.y + _messageField.height + 15, "Register", register);
			but1.scaleX = but1.scaleY = 3;
			var but2:PushButton = new PushButton(this, 90, 640, "Unregister", unregister);
			but2.scaleX = but2.scaleY = 3;


		public function handleAddedToStage(e:Event):void
			_messageField.appendText("\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);


		private function register(e:MouseEvent = null):void
			_messageField.appendText("\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);

			if(response.indexOf("registrationID:") != -1)
				_messageField.appendText("\n\nDevice was already registered.\n" + response);
				//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);
				//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

		public function unregister(e:MouseEvent = null):void
			_messageField.appendText("\n\nUnegistering device with GCM...");

		//on successful registration get GCM registration id for your device
		private function handleRegistered(e:GCMEvent):void
			_messageField.appendText("\n\nreceived device registrationID: " + e.deviceRegistrationID);
			_gcmDeviceID = e.deviceRegistrationID;

		private function handleRegistrationIDReceived():void
			//send device id to backend service that will broadcast messages

		public function checkPendingFromLaunchPayload():void
			_payload = _gcmi.checkPendingPayload();
			if(_payload != GCMPushInterface.NO_MESSAGE)
				_messageField.appendText("\n\npending payload:" + _payload);

		//messages are received when app is in background therefore add event for when app is resumed from notification
		private function handleMessage(e:GCMEvent):void
			//get payload
			_payload = e.message;
			_messageField.appendText("\n\nGCM payload received:" + _payload);
			trace("app is in the background: adding GCM app invoke listener");
			NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onInvoke, false, 0, true);

		private function onInvoke(e:InvokeEvent):void
			trace("app was invoked by gcm notification");
			_messageField.appendText("\n\napp was invoked from GCM notification");
			NativeApplication.nativeApplication.removeEventListener(InvokeEvent.INVOKE, onInvoke);

		public function handlePayload():void
			//you can parse and treat gcm payload here
			//dispatch an event or open an appropriate view

		//when device is unregistered on Google side, you might want to unregister it with your backend service
		private function handleUnregistered(e:GCMEvent):void
			_messageField.appendText("\n\ndevice was unregistered from GCM");
			//unregister with yours or public backend messaging service

		//handle variety of gcm errors
		private function handleError(e:GCMEvent):void


		private function visitGooglePlay(e:MouseEvent):void
			navigateToURL(new URLRequest("market://details?"));
Air Native Extension, Android . URL.