Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8"?>
- <s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
- xmlns:s="library://ns.adobe.com/flex/spark" title="Settings" creationComplete="initView()" destructionPolicy="never">
- <fx:Declarations>
- <!-- Placer ici les éléments non visuels (services et objets de valeur, par exemple). -->
- </fx:Declarations>
- <fx:Script>
- <![CDATA[
- import eu.epitech.vose.MCServer;
- import com.afterisk.shared.ane.lib.GCMEvent;
- import com.afterisk.shared.ane.lib.GCMPushInterface;
- private var so:SharedObject = SharedObject.getLocal("settings");
- private var autoRefreshGroupDefaultSize:int;
- private var server:MCServer = MCServer.getInstance();
- public static const GCM_SENDER_ID:String = "863211319405";
- private var _gcmi:GCMPushInterface;
- private var _gcmDeviceID:String;
- private var _payload:String;
- private function initView():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"));
- }
- protected function notificationButton_changeHandler(event:Event):void
- {
- if (notificationButton.selected == true)
- register();
- else
- unregister();
- }
- ]]>
- </fx:Script>
- <s:HGroup width="100%" height="100%" horizontalAlign="center" requestedColumnCount="2"
- verticalAlign="middle">
- <s:TextArea id="_messageField" width="100%" height="100%"/>
- </s:HGroup>
- </s:View>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement