4 October 2011
Familiarity with Flash Builder 4.6, ActionScript 3, and AIR/Flex.
Intermediate
Note: By clicking the download link for any source examples on this page, you acknowledge that you have read and agree to the Adobe AIR SDK License Agreement. These files are considered Sample Code.
Note: Adobe recommends using the next version of Flash Builder for developing native extensions for Adobe AIR. Sign up now to get access to the prerelease of Flash Builder 4.6.
Adobe AIR has allowed application developers to extend the features of the runtime with a set of tools known as native extensions. By using native extensions, your applications can access all of the features of your target platform, even if the runtime itself doesn't have built-in support. This example expands AIR by allowing you to create notifications in AIR apps you deploy to Android and iOS. These notifications appear as any other system notification. You can customize the notification type, text, actions to be taken when the notification is dispatched.
The attached ZIP file contains:
Notification objects are created in AS3, and dispatched by a NotificationManager.
You can start dispatching notifications by coding the following:
private var notificationManager:NotificationManager
protected function notifyUser():void
{
if ( !this.notificationManager )
{
try {
this.notificationManager = new NotificationManager();
}
catch (ae:ArgumentError)
{
trace("The notification native extension has no support for this platform.");
return;
}
}
var n:Notification = new Notification();
n.tickerText = this.tickerText.text;
n.title = this.notificationTitle.text;
n.body = this.notificationBody.text;
this.notificationManager.notifyUser("MY_NOTIFICATION_TYPE",n);
}
The notifyUser() function dispatches the notification using the native extension. On Android, the notification appears in the notification drawer (accessible by sliding the drawer down from the top of the screen). iOS handles notifications slightly differently: if the application is running when the notification is dispatched, you won't receive a pop-up; if the application is running in the background (multitasking by using a background API), you'll receive the pop-up dialog box when the notification is dispatched.
Notification objects and their behavior are configurable:
Add an eventListener to the NotificationManager to receive an event when a notification has been addressed by the user. It's recommended you do this during the initialization of the NotificationManager instance. For example, code the following in a creationComplete handler of an s:View object:
protected function view1_creationCompleteHandler(event:FlexEvent):void
{
try
{
this.notificationManager = new NotificationManager();
}
catch (ae:ArgumentError)
{
trace("The notification native extension has no support for this platform.");
return;
}
this.notificationManager.addEventListener(NotificationEvent.NOTIFICATION_ACTION,onNotificationActionEvent);
}
private function onNotificationActionEvent(ne:NotificationEvent):void
{
trace("Notification action received. Type: " + ne.actionData);
}
You'll then receive a NotificationEvent.NOTIFICATION_ACTION when the notification is selected by the user (Android), or the action button is tapped (iOS). The ne.actionData will be set to the actionData value you set on the notification.
View the asdocs/index.html file contained in the sample to learn more about notifications and the NotificationManager.
If you're deploying to Android, be sure to specify this additional permission in the manifestAdditions tag of your application descriptor XML file:
<application>
<service android:name="com.adobe.ep.localnotifications.LocalNotificationIntentService"/>
</application>
This allows your application to be opened when the user taps the notification in the Android window shade (the notification area). No additional permissions are required for iOS applications.
For more information about developing native extensions for Adobe AIR, see:
For more information about using a native extension in an AIR application, see:
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.