Gérer les Notifications Push sur IOs dans AIR

1/ Utilisez tout d’abord le SDK 4.6 avec AIR 3.4 minimum

2/Créer une classe qui gérera la notification en se connectant sur Urban Airship

package notifications
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.RemoteNotificationEvent;
	import flash.events.StatusEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestDefaults;
	import flash.net.URLRequestMethod;
	import flash.notifications.NotificationStyle;
	import flash.notifications.RemoteNotifier;
	import flash.notifications.RemoteNotifierSubscribeOptions;

	import ressources.tacSkinnableContainerDefault;

	import spark.components.Group;

	import taccomponents.tacEventsTools;

	public class NotifierAPN extends Sprite
	{
		private var _subscribeOptions:RemoteNotifierSubscribeOptions = new RemoteNotifierSubscribeOptions();
		private var _preferredStyles:Vector. = new Vector.();
		private var _remoteNot:RemoteNotifier = new RemoteNotifier();
		private var _urlString:String;
		private var _urlreq:URLRequest;
		private var _urlLoad:URLLoader = new URLLoader();

		/**
		 *
		 *
		 */
		public function NotifierAPN()
		{
			init();
		}

		/**
		 *
		 *
		 */
		private function init():void
		{
			trace("SupportedNotification Styles: " + RemoteNotifier.supportedNotificationStyles.toString() + "\n");

			trace("Before Preferred notificationStyles: " + _subscribeOptions.notificationStyles.toString() + "\n");

			_preferredStyles.push(NotificationStyle.ALERT ,NotificationStyle.BADGE,NotificationStyle.SOUND );

			_subscribeOptions.notificationStyles= _preferredStyles;

			trace("After Preferred notificationStyles:" + _subscribeOptions.notificationStyles.toString() + "\n");

			_remoteNot.addEventListener(RemoteNotificationEvent.TOKEN,tokenHandler);
			_remoteNot.addEventListener(RemoteNotificationEvent.NOTIFICATION,notificationHandler);
			_remoteNot.addEventListener(StatusEvent.STATUS,statusHandler);

			this.addEventListener(Event.ACTIVATE,activateHandler);
		}

		/**
		 *
		 * @param e
		 *
		 */
		public function tokenHandler(e:RemoteNotificationEvent):void
		{
			trace("\nRemoteNotificationEvent type: "+e.type +"\nBubbles: "+ e.bubbles + "\ncancelable " +e.cancelable +"\ntokenID:\n"+ e.tokenId +"\n");

			_urlString = new String("https://go.urbanairship.com/api/device_tokens/" + e.tokenId);
			_urlreq = new URLRequest(_urlString);

			_urlreq.authenticate = true;
			_urlreq.method = URLRequestMethod.PUT;

			URLRequestDefaults.setLoginCredentialsForHost("go.urbanairship.com","Application id","Application Secret");

			_urlLoad.load(_urlreq);
			_urlLoad.addEventListener(IOErrorEvent.IO_ERROR,iohandler);
			_urlLoad.addEventListener(Event.COMPLETE,compHandler);
			_urlLoad.addEventListener(HTTPStatusEvent.HTTP_STATUS,httpHandler);
		}

		/**
		 *
		 * @param e
		 *
		 */
		public function notificationHandler(e:RemoteNotificationEvent):void
		{
			trace("\nRemoteNotificationEvent type: " + e.type +"\nbubbles: "+ e.bubbles + "\ncancelable " +e.cancelable);

			for (var x:String in e.data)
			{
				trace("\n"+ x + ":  " + e.data[x]);
			}
		}

		/**
		 *
		 * @param e
		 *
		 */
		public function activateHandler(e:Event):void
		{
			if(RemoteNotifier.supportedNotificationStyles.toString() != " ")
			{
				_remoteNot.subscribe(_subscribeOptions);
			}
			else
			{
				trace("\n Remote Notifications not supported on this Platform !");
			}
		}

		/**
		 *
		 * @param e
		 *
		 */
		private function compHandler(e:Event):void
		{
			trace("\n In Complete handler,"+"status: " +e.type + "\n");
		}

		/**
		 *
		 * @param e
		 *
		 */
		private function httpHandler(e:HTTPStatusEvent):void
		{
			trace("\n in httpstatus handler,"+ "Status: " + e.status);
		}

		/**
		 *
		 * @param e
		 *
		 */
		private function iohandler(e:IOErrorEvent):void
		{
			trace("\n In IOError handler" + e.errorID +" " +e.type);
		}

		/**
		 *
		 * @param e
		 *
		 */
		public function statusHandler(e:StatusEvent):void
		{
			trace("\n statusHandler");

			trace("event Level" + e.level +"\nevent code " + e.code + "\ne.currentTarget: " + e.currentTarget.toString());
		}
	}
}

3/ Enfin ajouter dans le fichier Manifest :

<![CDATA[

<key>aps-environment</key>

<string>development</string>

]]>

Comments are closed.