6 August 2012
Experience building games for iOS with Flash Builder or Flash Professional and Adobe AIR will help you make the most of this article.
Additional required other products
Milkman Games EasyPush Notifications Extension for iOS
Intermediate
Push notifications are a powerful feature for engaging users in mobile apps, enabling you to share news and updates, bring lapsed users back to your app, and even update the state of an app remotely.
Setting up push notifications can be a complicated affair, but with the EasyPush Notifications native extension for iOS, you can implement push simply and efficiently:
Before you can use push notifications on iOS, you'll need to create a new App ID specifically for your push-enabled app, create a special provisioning profile for your app, and set up a back-end certificate for the server.

Note: When you create your app in iTunes Connect before publishing to the App Store, be sure to choose this App ID so push notifications will be enabled for production.

Push notifications require a special SSL certificate that allows a back-end server to manage notifications for your app. There are separate certificates for development and production. You can use these certificates with a service provider such as Urban Airship so you won't have to run your own servers.

The Apple Push Notification service SSL Certificate Assistant will appear to help you generate a Certificate Signing Request (see Figure 4). You have already gone through this process when you began development to generate your Adobe AIR signing certificates; you'll need to repeat it now to generate the new Push SSL certificates.

You'll need to repeat this process later to generate a Production SSL certificate before publishing your app to the App Store.
When using push notifications, Apple requires you to generate a separate provisioning (.mobileprovision) file for each application you want to create. Be sure you've completed the steps above and waited a few minutes before creating your provisioning profile.

You'll need to repeat these steps to create a Distribution Provisioning Profile, using your production SSL key, when you're ready to submit the app to the App Store.
The EasyPush Notifications native extension is designed so that it can be quickly and easily integrated with Urban Airship, a popular provider of back-end services for managing, distributing, and testing push notifications, that offers both free and affordable plans.


The EasyPush Notifications native extension requires the AIR 3.3 SDK or a later version. You can download the latest AIR SDK from http://www.adobe.com/special/products/air/sdk/. If you haven't already installed the AIR 3.3 SDK for your Flash Professional CS6 or Flash Builder IDE, follow the instructions below.
<name> tag to Flex 4.6.0 (AIR 3.3) .Enabling the AIR 3.3 SDK in Flash Builder 4.6 on OS X
sudo cp -Rp /Applications/Adobe\ Flash\ Builder\ 4.6/sdks/AIR33SDK/ /Applications/Adobe\ Flash\ Builder\ 4.6/sdks/4.6.0/
<name> tag to Flex 4.6.0 (AIR 3.3) .The most time consuming part is done. Now you're ready to write some code!
The next step is to add the com.milkmangames.extensions.EasyPush.ane file (or EasyPushAPI.swc for FlashDevelop) to your project. These files can be found in the extension folder of the EasyPush Notifications extension package.
In Flash Professional CS6:
In Flash Builder 4.6:
In FlashDevelop:
You can start using the EasyPush Notifications extension with a few simple calls. See example/EasyPush.as for a full example class.
Follow these steps to get started:
import com.milkmangames.nativeextensions.*;
import com.milkmangames.nativeextensions.events.*;
isSupported() and areNotificationsAvailable() methods. If they return false , your app won't be able to use push notifications (either the device does not support them or the user has disabled them.)
If notifications are supported and available, you can initialize Urban Airship by calling the initAirship() method. This should be the very first thing your code does after the app starts to ensure any messages are properly received.
if(EasyPush.isSupported() && EasyPush.areNotificationsAvailable())
{
// "YOUR_AIRSHIP_KEY” - put your application key from urban airship here
// "YOUR_AIRSHIP_SECRET” - put the app secret from urban airship here
// true – sets development mode to ON. Set to false only when publishing for app store.
// true – enables automatic badge number tracking on the icon (optional)
// true – will automatically show alert boxes when a push is received.
EasyPush.initAirship("YOUR_AIRSHIP_KEY”,”YOUR_AIRSHIP_SECRET”,”airship”,true,true,true);
}
else {
trace("Push is not supported or is turned off...”);
return;
}
The initAirship() method takes six parameters. The first two are your app's Application Key and Secret, which you get from the Urban Airship website as described earlier. The third string value is reserved for future API use; you can use any string here.
The next three Boolean parameters are developmentMode , autoBadge , and autoAlertBox , in that order.
Setting developmentMode to true enables Development mode for your app- so that the Urban Airship server will transfer messages between its own sandbox server, and Apple's sandbox server, instead of using production URLs You'll want to set this to false when you are ready to publish to the app store.
Setting autoBadge to true will cause the notification badge number on your app's icon to be controlled automatically by Urban Airship.
Setting autoAlertBox to true will cause an AlertBox to appear in the native UI if the app receives a push while it's running. You can set it to false if you want to handle those messages with your own UI.
That's all you need for receiving basic push notifications!
Urban Airship lets you associate an alias or group of tags with a user. This is useful for targeting push notifications to a specific user or group of users.
You can set an alias that's unique to the user to make them easily identifiable for push notifications. For instance, if you're using the GoViral extension to log the user into Facebook, you might want to set their alias to their Facebook ID, so you can send direct notifications to that particular user; for example:
// sets the user's alias to "bob@internet.com”.
// You'd want to be sure this is unique to your user though!
EasyPush.airship.updateAlias("bob@internet.com”);
You can set tags to assign users to logical groups so that you can send selective push notifications to them later.
// create a vector array of tags
var tags:Vector.<String>=new Vector.<String>();
tags.push("advanced");
tags.push("gamer");
EasyPush.airship.setAirshipTags(tags);
Quiet time is a period during which notifications will not be displayed. The following example silences push notifications for the next 15 minutes:
//Setting quiet time for next 15 minutes...
var now:Date=new Date();
var inFifteen:Date=new Date();
inFifteen.setTime(now.millisecondsUTC+(15*60*1000));
EasyPush.airship.setQuietTime(now,inFifteen);
The EasyPush Notifications native extension dispatches several events that you may want to handle.
When the user starts your app and the extension is initialized, it will attempt to register them for notifications. As a result, either PNAEvent.TOKEN_REGISTERED or PNAEvent.TOKEN_REGISTRATION_FAILED will be dispatched. If registration succeeds, but some of the types of push requested are turned off (for instance, push messages are enabled, but not sounds), the PNAEvent.TYPES_DISABLED event will fire. The following code illustrates how these events can be handled:
EasyPush.airship.addEventListener(PNAEvent.TOKEN_REGISTERED,onTokenRegistered);
EasyPush.airship.addEventListener(PNAEvent.TOKEN_REGISTRATION_FAILED,onRegFailed);
EasyPush.airship.addEventListener(PNAEvent.TYPES_DISABLED,onTokenTypesDisabled);
function onTokenRegistered(e:PNAEvent):void
{
trace("token was registered: "+e.token);
}
function onRegFailed(e:PNAEvent):void
{
trace("reg failed: "+e.errorId+"="+e.errorMsg);
}
function onTokenTypesDisabled(e:PNAEvent):void
{
trace(“some types disabled:”+e.disabledTypes);
}
There are two scenarios for receiving notifications. Either the app was open, and a push was received ( PNAEvent.FOREGROUND_NOTIFICATION ), or the app was in the background, and the user clicked the notification to open the app ( PNAEvent.RESUMED_FROM_NOTIFICATION ). In the former case, a message box with the notification will automatically be displayed to the user, if autoAlertBox was set to true in the initAirship() call. If the user taps the OK button on this alert, PNAEvent.ALERT_DISMISSED will be dispatched.
These events contain extra information about the notifications that were sent. You can use this data to perform additional actions in your app.
EasyPush.airship.addEventListener(PNAEvent.ALERT_DISMISSED,onAlertDismissed);
EasyPush.airship.addEventListener(PNAEvent.FOREGROUND_NOTIFICATION,onNotification);
EasyPush.airship.addEventListener(PNAEvent.RESUMED_FROM_NOTIFICATION,onNotification);
function onNotification(e:PNAEvent):void
{
trace(“new notification, type+"="+e.rawPayload+","+e.badgeValue+","+e.title);
}
function onAlertDismissed(e:PNAEvent):void
{
trace(“Alert dismissed, payload="+e.rawPayload+","+e.badgeValue+","+e.title);
}
In your application descriptor file, you need to specify the version of the AIR SDK you are using (3.3 or later) as well as a link to the extension. For a working example, see example/app.xml.
<application xmlns="http://ns.adobe.com/air/application/3.3">
<extensions>
<extensionID>com.milkmangames.extensions.EasyPush</extensionID>
</extensions>
<id> property exactly matches the App ID you created in the iOS Provisioning Portal.entitlements element to the application XML file and include in it the App ID you created in the iOS Provisioning Portal. The App ID consists of a string of random letters and numbers followed by your App Bundle ID. For instance, it might be something like "Q942RZTE24.com.yourcompany.yourgame". Copy this exact string twice into the <iphone> block of your application descriptor, like so:<iPhone>
<InfoAdditions>
<![CDATA[
<key>UIDeviceFamily</key>
<array>
<string>1</string>
<string>2</string>
</array> ]]> </InfoAdditions>
<Entitlements>
<![CDATA[
<key>application-identifier</key> <string>Q942RZTE24.com.yourcompany.yourgame</string>
<key>aps-environment</key>
<string>development</string>
<key>get-task-allow</key>
<true/>
<key>keychain-access-groups</key>
<array>
<string>Q994RZTE24.com.milkmangames.pushexample</string>
</array>
]]>
</Entitlements>
</iPhone>
development to production , and remove the entire <key>get-task-allow</key><true/> line.If you're using Flash Builder 4.6 or later, or Flash Professional CS6 or later, and have added the EasyPush Notifications extension library as described above, then you can compile as you usually do directly from the IDE. If not and you are building your app with the extension from the command line, then you'll need to specify the directory containing the com.milkmangames.nativeextensions.EasyPush.ane file.
Here is an example build command line:
c:\dev\air_sdk_33\bin\adt -package -target ipa-test-interpreter -storetype pkcs12 -keystore YOURKEYSTOREFILE.P12 -storepass YOURPASSWORD -provisioning-profile YOUR_MOBILEPROVISION_FILE.mobileprovision myapp.ipa myapp.xml myapp.swf -extdir .
Take note of the dot after -extdir . It is not a typo; it tells the adt packager to look for the extension in the current directory.
Once you've built your app and successfully installed it on your device, it's easy to send a test push notification with Urban Airship:

aTitle property to the JSON Payload (see Figure 9).
That's it! The notification should trigger on your device. To send a notification to multiple devices, use the Send Broadcast option instead.
Now that you have push notifications up and running in your app, you may want to explore these other native extension tutorials:
For additional extensions, see More Native Extensions from Milkman Games.