Apple Push Notification Service native extension for Adobe AIR

Here’s another AIR native extension for iOS I’m currently working on – Apple Push Notification Service (APNs) support.
It’s almost complete – the most important things are there. You can register to APNs, get your device token and receive remote notification in ActionScript – everything that you need to build a great app.

Grab the source from github: https://github.com/pkoscierzynski/NativeAPNService

To run the example you need to go to Apple Developer Portal and create a new application id, generate push notification certificates and a new mobile provisioning profile.
I’d recomend this tutorial by Matthijs Hollemans, it’s very detailed and self explanatory (like all the tutorials on Ray’s site).
Apple Push Notification Services Tutorial: Part 1/2

I also used php sample to publish my message to APNs sandbox servers. Follow the tutorial step by step and you should get everything working.

When you’ll have your app id and be sure to put it in the apropriate xml tags of AIR application descriptior.
In AIR 3.1, application descriptor has a new section for iOS apps – Entitlements. In Entitlements section you need to provide the id of you application, otherwise the APN won’t work (this section is the content of Entitlements.plist file known from native Xcode / Objective-C).

Adobe AIR application descriptor. iPhone Entitlements section.

Apple Push Notifications in Adobe AIR iOS application from Piotr Koscierzynski on Vimeo.

81 thoughts on “Apple Push Notification Service native extension for Adobe AIR

  1. Should this work with AIR 3.0? I am getting a Exception in thread “main” java.lang.Error: Unable to find named traits: pl.randori.air.nativeextensions.ios::APNSExtension

    Using ADT to package. My reference to the ANE file in flash is set to external as it should be. Contact me at my email address, I could use your help on this and we can pay you for your time.

    Thanks!

    Mike

      • I got it working, I wasn’t specifying the extension ID in my app config file. Is there a way to turn off the dialogs that appear displaying the device id? I need to get this working today. Please contact me. Thanks!

        Mike

      • I’m interested too by this source code. I success to compile your version but I need to get the first push, when application is not launched and waked by the notification.
        In your app, the push is received by the extension, but not transmitted to the app (because it’s not initialized yet).
        I think to “save” it into extension, wait for the dispatching of application complete and then, re send an event to get this message.
        Sorry for my English maybe so bad :-s and thanks a lot for this great example.

  2. Hi, once again love Your work.
    I got just one question. Is it posible You would add a support for Windows or a mac. It can return gibberish but with the function isSupported .. please!!
    I can’t imagine anybody debbuging a whole app just on ios.. And as You know in flash builder simulator we got this lovely error: “incompatible with this environment”.
    pretty please:)

  3. Hi.

    I’m trying you ane but when i try to compile, I get an error:

    “Id: file not found: %ADT_ENV_VAR_26%”
    “Compilation failed while executing: Id64″

    I’m using windows 7

    Can you help?

    Thanks

  4. Is it possible send notification direct on device, роint-to-point without server side( php and ssl://gateway.sandbox.push.apple.com:2195)?

    • Device token can only be accessed using Objective-C code. My extension does exacly that and then returns the token back to ActionScript as a StatusEvent.

      • Do u mean that i shall make .m file implement UIApplicationDelegate and define the method:
        -(void) application :( UIApplication *) application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *) deviceToken

        ?Then change the default delegate of UIApplication?

  5. Will you be releasing the source code? Or a commercial version of the extension?

    I got everything to work ok which is great but now I want to use the extension in a proper project so I don’t want the alert which comes up and ideally the event type would be more specific – currently the type of APNSExtensionEvent does not indicate a specific type e.g. token.

    Any help?

    • As far as I know it’s not possible to codesign (+Entitlements.plist) an IPA archive on Windows. You need iOS SDK tools which is only available on MacOS X.
      I don’t have a Windows so I haven’t really tried to make it work.

  6. Also -
    Are you planning to also support local notifications, or at least setting the number for the icon badge? That would make a lot of sense and make your extension even awesomer :)

    • We are using this extension in a couple of projects for our clients and we do not plan to release the source of this extension right now.

  7. Hi Piotr Koscierzynski, thanks for all! now i’ve compiled your example app in my os workstation.. and it works!!! Thanks a lot.

    But , for testing pushing from my server, i need know the device token, how i can get it? you write about a Status Event: how can i access this property?

    Thanks and have a nice day.

  8. under the view1_creationCompleteHandler(event:FlexEvent),

    apnServiceExtension = new APNSExtension();
    apnServiceExtension.addEventListener(APNSExtensionEvent.APN_SERVICE_EVENT, onRemoteNotificationEvent);

    I did not received any return token or any message, the textarea just show nothing Anyone had this problem?

    • You didn’t receive any notifation because the device failed to register to APNs servers. That’s a very common error – check your provisioning profile, app ID, etc.

      • Hi Piotr,
        I’m trying and trying again, but I’m missing something …
        In my case, textarea shows:
        ” will send request to APNS (the first row)
        Registering device to APNS (the second row)”
        and at same time, native popup shows “AIR APN EVENT”( as title) “Registering to APNS” (popop body).. and no more (no token etc..) and that every time I click on “Register To APN Service” button..

        What am I doing wrong? I’m trying on a iPad (1)
        thank you so much

  9. Hey,

    Was wondering how you assigned the delegate class in your code, been trying to do this myself, but keep running into problems as soon as I set

    [UIApplication sharedApplication].delegate

    It seems that as soon as I set that all the other FRE functions to communicate back to flash stop working.

    Any help is appreciated, cheers!

  10. Hello,

    i successfuly compile my ipa, APNSExtension is available (i can trace it).
    But once i instantiate:
    apnServiceExtension = new APNSExtension();

    my app crash instantly….

    any idea ??

  11. Dear Piotr,

    Thanks a lot for your great tutorial. I created an application on developer.apple.com and configured APNs already. I export the ipa file from Flash Builder 4.6 using MAC. Everything seems fine except the thing that I stuck at the step “get APNS device token”. When I push the button to register APN, I just receive 2 first messages and it stops at “registering device to APNS”.

    Even when I tried with your release source code (I modifid the app id to mine already), that issue still remains. Could you please tell me which step I missed or you guess which step of mine has the problem.

    Thanks and BEST Regards,

  12. Good afternoon,

    I am having some trouble with this. It’s a weird error and I haven’t found anyone with the same issue yet.

    The thing is that when I try install my app on my iPhone3GS, it doesn’t finish and the device raises an “iTunes Sync > Error installing “”" and that’s it.

    I realized that the problem comes when I add the Entitlements section on the application descriptor. Without it, the app is installed normally but of course the registerForRemoteNotifications function won’t work and will raise and error saying that the aps-environment isn’t defined and some other stuff related to this matter.

    If anyone has a clue about this, let me know please, I’m having a hard time with this.

    Cheers!

    • Problem solved.

      The issue was that the provisioning profile I was using was created before push notifications were enabled for the AppID, so it lacked some data related to this matter.

      To solve this I just created a new provisioning profile for the app and it all worked smoothly.

  13. Tried compiling for AIR3.2 for ios with the newest ADT.
    I can confirm that ADT compiling under windows does not work. In my case it throws:
    ld: file not found: %adt_env_var_32%
    Compilation failed while executing : ld64

    My guess is that this error occurs because i am missing the iOS SDK from XCode.

    If this is not the case – i would really appreciate any help you can provide.
    I will try to compile in OSX, but that just seems like such a hassle.

  14. Hello,
    same error

    can you help me
    error:

    Error occurred while packaging the application:

    Exception in thread “main” java.lang.Error: Unable to find named traits: pl.randori.air.nativeextensions.ios::APNSExtension
    at adobe.abc.Domain.resolveTypeName(Domain.java:231)
    at adobe.abc.Domain.resolveTypeName(Domain.java:148)
    at adobe.abc.GlobalOptimizer$InputAbc.resolveTypeName(GlobalOptimizer.java:272)
    at adobe.abc.GlobalOptimizer$InputAbc.resolveSlotType(GlobalOptimizer.java:946)
    at adobe.abc.GlobalOptimizer$InputAbc.resolveType(GlobalOptimizer.java:535)
    at adobe.abc.GlobalOptimizer$InputAbc.resolveTypes(GlobalOptimizer.java:448)
    at adobe.abc.LLVMEmitter.generateBitcode(LLVMEmitter.java:327)
    at com.adobe.air.ipa.AOTCompiler.convertAbcToLlvmBitcodeImpl(AOTCompiler.java:472)
    at com.adobe.air.ipa.BitcodeGenerator.main(BitcodeGenerator.java:82)
    Compilation failed while executing : ADT

  15. Hi Piotr,

    First of all, thanks for the great work. I’m using your mail extension in an app (works great!!) and now implementing your push extension. But I get an error :

    [exec] ld: duplicate symbol _OBJC_IVAR_$_MailComposerHelper.context in /var/folders/fm/xskr6spx457bnd4hkm6ff_kc0000gn/T/03e3f7df-1982-4Compilation failed while executing : ld64
    [exec] 169-a889-dbce599e3351/libpl.randori.air.nativeextensions.ios.MailExtension.a(MailComposerHelper.o) and /var/folders/fm/xskr6spx457bnd4hkm6ff_kc0000gn/T/03e3f7df-1982-4169-a889-dbce599e3351/libpl.randori.air.nativeextensions.ios.APNSExtension.a(MailComposerHelper.o)
    [exec] Result: 12

    Usign AIR 3.3 (yesterday released) and Flex 4.6. Package with ipa-test-interpreter.

    Any idea?

  16. I am trying to use the extension with other extensions such as
    http://lancelotmobile.com/blog/admob-ane/
    it is giving errors, as it seems you are using the same function name to initialize the extension:
    Multiple ANEs with the same initializer ExtIntizilizer

    and it wont work, is there a way you can provide us with a build that uses a more custom function name so it won’t conflict in the future?
    Thanks

  17. Same here:

    2 lines:
    - Received event: Will send request to APNS
    - Received event: Registering device to APNS

    1 popup:
    AIR APN Event
    Registering to APNS

    Any fix for this? Is the binary outdated?

  18. sorry, I got some problem can you help me

    The entitlements specified in your application’s Code Signing Entitlements file do not match those specified in your provisioning profile.

    (0xE8008016).

  19. Thanks a lot for this ANE. Registration work fine but i don’t receive notification.

    Does ANE work in Production mode ? (I changed key aps-environment to production and get-task-allow to false )

    How do you know which gateway ANE is connected to ?
    gateway.push.apple.com or gateway.sandbox.push.apple.com ?

    Thanks for you reply

    • Check again and regenerate app’s provisioning profile. It’s a certificate issue, not related to the ANE itself.
      Sandbox is used only for apps that are not in AppStore, ANE does not care about which server sends the message.

  20. Hey Piotr, THANKS for the ANE for APN!!

    I am having a similar issue with several others who commented with getting the device token.

    I am able to see the two events firing back:

    Received event: Will send request to APNS
    Received event: Registering device to APNS

    Though no Device token, if I understand correctly the ANE is taking note of it, though, how should I listen to it?

    This is a very important ANE, and any help would go a looong way!

    • Check the entitlements again and regenerate app’s provisioning profile if you have to. It’s a certificate issue, not related to the ANE itself.
      You fail security validation at Apple servers and that’s why it doesn’t work.

    • Maybe it’s related to a newer iOS and AIR SDK version, maybe Adobe changed something.
      This version of ane is working for sure when app is build using AIR 3.2 and up to iOS 5.0.

      I recently got it updated (compiled with iOS SDK 5.1 and added Android support) and it’s working with AIR 3.3 beta on new iPad running iOS 5.1 and iPhone 3GS running iOS 5.1.
      I’ll try to post an update in my free time.

      Can you give more details (device, iOS version, AIR version) ?

  21. I am facing issue while installing the app to the device.
    First I forgot to add the aps-environment key to the entitlements file. I was able to install it but got error that aps-environment was not set.
    Now when I have added it, I am not able to install the app. It starts installation and stops with an error.
    Any idea what might be wrong?

  22. I can’t get it to work either. I use CS6 and am not entire what to put in the Entitlement sections given the blanked out examples. Does it need the random hex string Apple provides you with? and what’s before the .* in the key section?

    I am convinced that my provisioning file has the push notifications in it (I regenerated and downloaded it 3 times!) but still the device token is not returned.

    I also never found an answer to how to switch the pop-ups off. So totally stuck really and any more suggestions would be appreciated.

  23. HI piotr,
    Google change Cloud Messaging System !
    Normaly, old ANE must be work with the new system.
    Instead of the email adress, put your new projectId.
    Do you know if something change in the app xml descriptor ?
    Thanks.
    Thierry

  24. Hey!

    Great tutorial, works fine!
    But do I have to register my App by every launch? And is it possible to prevent the notifications on registration? I always get like “AIR APN Event – successfully registered to APN Service. Device token: …”

    Thanks!