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â€.
Sample
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.events.Event; import flash.events.InvokeEvent; import flash.events.MouseEvent; import flash.net.URLRequest; import flash.net.navigateToURL; import flash.text.TextField; [SWF(frameRate="40", backgroundColor="#000000")] public class GCMExample extends Sprite { [Embed(source="/assets/top.png")] 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; createUI(); 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.addChild(topBmp); this.addChild(top); 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:"; this.addChild(_messageField); _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); register(); } 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); trace(response); 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); 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(); } } public function unregister(e:MouseEvent = null):void { _messageField.appendText("\n\nUnegistering device with GCM..."); _gcmi.unregister(); } //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; handleRegistrationIDReceived(); } 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); handlePayload(); } } //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); handlePayload(); } 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?id=air.com.afterisk.sketchguessfree")); } } }
Does this work with UrbanAirShip? If so, how do i implement it?
I’m not familiar with UrbanAirShip. As far as I know, this ANE receives push notifications directly from Google servers (using their API for that). If UrbanAirShip is able to do that too, I think you will find no problem. You should confirm that with the ANE author though.