Advertisement
Guest User

view.mxml

a guest
Nov 23rd, 2013
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MXML 5.31 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
  3.         xmlns:s="library://ns.adobe.com/flex/spark" title="Settings" creationComplete="initView()" destructionPolicy="never">
  4.     <fx:Declarations>
  5.         <!-- Placer ici les éléments non visuels (services et objets de valeur, par exemple). -->
  6.     </fx:Declarations>
  7.    
  8.     <fx:Script>
  9.         <![CDATA[
  10.             import eu.epitech.vose.MCServer;
  11.            
  12.             import com.afterisk.shared.ane.lib.GCMEvent;
  13.             import com.afterisk.shared.ane.lib.GCMPushInterface;
  14.            
  15.             private var so:SharedObject = SharedObject.getLocal("settings");
  16.             private var autoRefreshGroupDefaultSize:int;
  17.             private var server:MCServer = MCServer.getInstance();
  18.            
  19.             public static const GCM_SENDER_ID:String = "863211319405";
  20.             private var _gcmi:GCMPushInterface;
  21.             private var _gcmDeviceID:String;
  22.             private var _payload:String;
  23.            
  24.            
  25.             private function initView():void {
  26.                 _messageField.appendText("\n\nInitializing GCMPushInterface...");
  27.                 //create instance of extension interface and add appropriate listeners to it
  28.                 _gcmi = new GCMPushInterface();
  29.                 _gcmi.addEventListener(GCMEvent.REGISTERED, handleRegistered, false, 0, true);
  30.                 _gcmi.addEventListener(GCMEvent.UNREGISTERED, handleUnregistered, false, 0, true);
  31.                 _gcmi.addEventListener(GCMEvent.MESSAGE, handleMessage, false, 0, true);
  32.                 _gcmi.addEventListener(GCMEvent.ERROR, handleError, false, 0, true);
  33.                 _gcmi.addEventListener(GCMEvent.RECOVERABLE_ERROR, handleError, false, 0, true);
  34.                
  35.                 register();
  36.             }
  37.            
  38.             private function register(e:MouseEvent = null):void
  39.             {
  40.                 _messageField.appendText("\n\nRegistering device with GCM...");
  41.                 //check if device is already registered otherwise start registration process and wait for REGISTERED event
  42.                 var response:String = _gcmi.register(GCM_SENDER_ID);
  43.                 trace(response);
  44.                
  45.                 if(response.indexOf("registrationID:") != -1)
  46.                 {
  47.                     _messageField.appendText("\n\nDevice was already registered.\n" + response);
  48.                     //extract GCM registration id for your device from response, you will need that to send messages to the device
  49.                     //create your own backend service or use public services
  50.                     _gcmDeviceID = response.substr(response.indexOf(":") + 1);
  51.                     handleRegistrationIDReceived();
  52.                     //if device was already registered check if there is any pending payload from GCM
  53.                     //this can be true when android shut down your app and its restarted instead of being resumed
  54.                     checkPendingFromLaunchPayload();
  55.                 }
  56.             }
  57.            
  58.             public function unregister(e:MouseEvent = null):void
  59.             {
  60.                 _messageField.appendText("\n\nUnegistering device with GCM...");
  61.                 _gcmi.unregister();
  62.             }
  63.            
  64.             //on successful registration get GCM registration id for your device
  65.             private function handleRegistered(e:GCMEvent):void
  66.             {
  67.                 _messageField.appendText("\n\nreceived device registrationID: " + e.deviceRegistrationID);
  68.                 _gcmDeviceID = e.deviceRegistrationID;
  69.                 handleRegistrationIDReceived();
  70.             }
  71.            
  72.             private function handleRegistrationIDReceived():void
  73.             {
  74.                 //send device id to backend service that will broadcast messages
  75.             }
  76.            
  77.             public function checkPendingFromLaunchPayload():void
  78.             {
  79.                 _payload = _gcmi.checkPendingPayload();
  80.                 if(_payload != GCMPushInterface.NO_MESSAGE)
  81.                 {
  82.                     _messageField.appendText("\n\npending payload:" + _payload);
  83.                     handlePayload();
  84.                 }
  85.             }
  86.            
  87.             //messages are received when app is in background therefore add event for when app is resumed from notification
  88.             private function handleMessage(e:GCMEvent):void
  89.             {
  90.                 //get payload
  91.                 _payload = e.message;
  92.                 _messageField.appendText("\n\nGCM payload received:" + _payload);
  93.                 trace("app is in the background: adding GCM app invoke listener");
  94.                 NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onInvoke, false, 0, true);
  95.             }
  96.            
  97.             private function onInvoke(e:InvokeEvent):void
  98.             {
  99.                 trace("app was invoked by gcm notification");
  100.                 _messageField.appendText("\n\napp was invoked from GCM notification");
  101.                 NativeApplication.nativeApplication.removeEventListener(InvokeEvent.INVOKE, onInvoke);
  102.                 handlePayload();
  103.             }
  104.            
  105.             public function handlePayload():void
  106.             {
  107.                 //you can parse and treat gcm payload here
  108.                 //dispatch an event or open an appropriate view
  109.             }
  110.            
  111.             //when device is unregistered on Google side, you might want to unregister it with your backend service
  112.             private function handleUnregistered(e:GCMEvent):void
  113.             {
  114.                 _messageField.appendText("\n\ndevice was unregistered from GCM");
  115.                 //unregister with yours or public backend messaging service
  116.                 //...
  117.             }
  118.            
  119.             //handle variety of gcm errors
  120.             private function handleError(e:GCMEvent):void
  121.             {
  122.                
  123.             }
  124.            
  125.             private function visitGooglePlay(e:MouseEvent):void
  126.             {
  127.                 navigateToURL(new URLRequest("market://details?id=air.com.afterisk.sketchguessfree"));
  128.             }
  129.            
  130.             protected function notificationButton_changeHandler(event:Event):void
  131.             {
  132.                 if (notificationButton.selected == true)
  133.                     register();
  134.                 else
  135.                     unregister();
  136.             }
  137.            
  138.         ]]>
  139.     </fx:Script>
  140.         <s:HGroup width="100%" height="100%" horizontalAlign="center" requestedColumnCount="2"
  141.                   verticalAlign="middle">
  142.             <s:TextArea id="_messageField" width="100%" height="100%"/>
  143.         </s:HGroup>
  144. </s:View>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement