Posted on by
EDITOR’S NOTE: This tutorial is outdated and has been replaced by a new guide: Local/Push Notifications. Please refer to this guide for details and usage examples.

Christmas came early for many Corona SDK developers! The team was very busy last week with a bunch of check-ins that rolled out to daily builds. There are many new features for you to explore, including:

  • Build 987 — Inneractive ads SDK updated to 1.1.5
  • Build 990 — iOS 5/6 native Twitter access via native.showPopup()
  • Build 990 — new native.canShowPopup() API call to test for email, sms and twitter capability
  • Build 991 — Facebook SDK updated to 3.1.1
  • Build 992 — iAds support added
  • Build 993 — (drum roll please)… Android Push Notifications!

Several developers are already in the process of deploying these new features which are now quicker to add to Corona thanks to Project Gluon, Corona SDK’s new Plugin system.

Today, let’s talk about the much-anticipated Android Push Notifications. If you’ve tried to deploy push notifications for Apple, it seems as if you require a degree in rocket science and an MBA in accounting to deal with the complexity of all the certificates, keys, sandboxing, provisioning profiles, and 3rd-party vendor setups. Compound all of this with the “fun” of trying to set it up on a client’s iOS developer account, and you have one of the most dreaded activities known to a mobile developer. Fortunately, once you’re past the setup on Apple’s side, push notifications in Corona are relatively simple.

Android, on the other hand, isn’t quite as terrifying to set up. The big hurdle with push notifications is the fact that Android OS doesn’t manage the push notifications, but pushes that responsibility to the app itself. Corona Labs has worked very hard to make the in-app implementation for Android as easy as the Apple side.

Four Basic Steps

There are four basic steps to getting push notifications working on Android:

  1. Setup for Google
  2. Setup for 3rd-party services (optional)
  3. Respond to registration events from your app
  4. Respond to push notifications as they arrive

1. Setup for Google

aos-push-1
  • STEP 1:  Go to https://code.google.com/apis/console in the browser of your choice. If you have not done so, click on Services. Find the entry for Google Cloud Messaging for Android entry and turn it on. You will have to accept some “Terms of Services” agreement, so please do so.
aos-push-2
  • STEP 2:  Click on API Access. Then click on Create new Server key. You will be presented with a rather long screen. You don’t need to fill out anything in the IP Address area. Copy your API Key to use in your services that send push notifications, like Corona Cloud services.  NOTE: The Corona SDK sample app has the ability to send push notifications and you will need this API Key if you want to build and test the sample app.
aos-push-3
  • STEP 3:  Go to the API Console’s Overview tab and copy down the Project Number. Ignore the Project ID. Your Project Number is also the URL. You will need this number for your app to receive push notifications.
aos-push-4
  • STEP 4:  This doesn’t seem to be a “required” step, but you can visit your Google Play Developer Console, find your app (if you have it uploaded), edit its meta data, and find the Enable Google Cloud Messaging Stats entry. Add your Project ID as the Sender ID here. This allows you to see stats about your app’s GCM usage.

2. Setup for 3rd-Party services (optional)

On this topic, there isn’t a lot of advice that we can offer in this blog post because you may be using anything from Corona Cloud Services (Game Minion) to Urban Airship, or you might wish to roll out your own service since sending messages uses a standard REST API call. However, your service will likely to want your API Server Key. They will ask for something like GCM Server Key. This should be all that you need to provide.

3. Respond to registration events from your app

It was almost one year ago when Corona Labs blogged about setting up push notifications for iOS. Most of that post steps through the challenge of setting up push notifications for Apple. However, steps 4 and 5 pertain to the Corona aspects of that, and it’s a good reference for Android push notifications as well.

There are three files you have to change to receive push notifications. First is your config.lua. You need to add this block:

This needs to be in quotes even though it’s just a number. If you’re using the Ultimate config.lua from our recent blog post, then you would insert this twice, once for the “tall” Android devices and again for the others:

There’s no need to add this to the iOS device area of the configuration, but remember to put the iOS-specific push notification code in those blocks!

Next is your build.settings file. You need to add this block (or mix it in with your existing android entry):

This should be a child of the settings = {} table, and peers with iPhone and orientation or other blocks.

Now for your main.lua, there is some code that needs to be added. If you already have this code added for iOS push notifications, you should be in good shape. If this is new to you, there are three code chunks that you must add.

First, you need to set up a handler function to respond to notification events. There are two events that you need to handle in specific: Registration and the Push Event itself. Both are managed with the same handler function. To enable this function, you have to setup a Runtime listener. Note that when a push notification launches your app from suspended or stopped state, you may want to react to it by going to a specific screen.

When you get the “remoteRegistration” event you’re completely registered with either Apple or Google and you are presented your token in the event.token variable. You need this value to register your device with other services. This remoteRegistration event is a good point to register with other services or store the token for later, if you have to register after a login process with your push sending service.

Now, to make this function do something useful, you need to hook it up to Corona’s event system:

Finally, if your app isn’t running (i.e. it’s stopped/suspended, a push notification comes in for the app, and you tap on it), the OS will launch your app and, if you don’t do anything further, it will start the app just as if the user tapped the app icon. But what if you want to jump to a specific point in the app based on the “content” of the push notification? For example, if you’re building a news-related app and the push is about a specific breaking news story, you might wish to direct the user to that story.

This is done using launchArgs. The push notification contains custom data that can be sent along with it, and your app can react to that data. When it starts, you should check the launchArgs and potentially do something beyond your standard start-up procedure. Add this line of code, generally near the top of your main.lua:


In Summary

That’s it for this tutorial. Please note that push notifications only work for iOS devices or Android devices with Google Play installed. At this time, notifications will not work with Kindle Fire or Barnes & Noble Nook devices.

Keep an eye out for more upcoming tutorials about this past week’s useful additions to Corona SDK. Until then, we at Corona Labs wish you a happy, safe holidays and we hope that you enjoy the new gifts.


Posted by . Thanks for reading...

14 Responses to “Corona Holiday Gifts: Android Push and More!”

    • Rob Miracle

      Sending push notifications is simply a REST API call. If you look at the sample app, it shows all the headers and the table of data that needs sent. It would be a matter of emulating this in PHP using their syntax and probably using “curl” to make the request.

      Reply
    • Joshua Quick

      Google’s official documentation for sending push notifications can be found here…
      http://developer.android.com/google/gcm/gcm.html#send-msg

      Also have a look at sample project “Notifications/GooglePushNotifications” that is included with the Corona SDK for an example on how to send a push notification within your app. The sample code explains what needs to be in your HTTP request packet and also shows you what kind of responses (including errors) that you may receive from Google’s servers.

      Reply
      • jesse

        Are there any special considerations for setting up GCM via Enterprise? I am able to get the device’s token when I run a simulator build, but not in an enterprise build.

        On a hunch, I dont see where to add the projectNumber in (the AndroidManifest?)

        Reply
        • Joshua Quick

          jesse,

          You need to add some Corona related things to your AndroidManifest.xml file to get push notifications working. If you download the newest Corona Enterprise daily build, notice that all of the sample projects’ AndroidManifest.xml files have been updated. You’ll need to copy over the following service and receivers from the sample project’s AndroidManifest.xml file to your file…
          - CoronaService
          - SystemStartupBroadcastReceiver
          - AlarmManagerBroadcastReceiver
          - StatusBarBroadcastReceiver
          - GoogleCloudMessagingBroadcastReceiver

          You’ll also need to add the permissions you see in sample project “Notifications/GooglePushNotifications” to your AndroidManifest.xml file as well.

          From there, just follow the instructions that you see in the GooglePushNotifications sample project. And don’t forget to add your ProjectNumber to your config.lua file.

          I hope this helps.

          Reply
          • jesse

            Thanks for the reply — when you say the newest build, do you mean 998+? If so, I see that one in the sim-sdk download page, but not the enterprise-sdk download page.

            When I grep around for ‘GoogleCloudMessagingBroadcastReceiver’, I only see it in the APK template manifest. I also dont see a sample project for ‘GooglePushNotifications’. Is that going to show up in the ‘Samples’ directory?

        • Joshua Quick

          jesse,

          I just looked into this now. You’re right. Our newest sample project changes did not make it into the Corona Enterprise daily build. I just resolved this now. Tomorrow daily build #999 will have the updated sample projects. Sorry about that.

          Reply
          • jesse

            I see #999 on the pro sdk download page, but not on the enterprise sdk page. Can you update the enterprise daily build page?

        • David

          Jesse. Build #999 is up there now. I’m replying to this comment as WP isn’t letting me comment on the comment further down (too nested!)

          Reply
  1. jesse

    Thanks for getting that update in there. Unfortunately, I’m still jammed up:

    1) I dont see a GooglePushNotifications sample project in the Corona SDK — am I supposed to use the actual Google one?

    2) If I am to use the google one, should I make my own
    ‘com.mycompany.myapp.GCMIntentService’ class to handle GCM notifications?

    3) If I am set up properly (see below for relevant AM pieces), is it possible that ‘remoteRegistration’ events dont get automatically sent in Enterprise? Is there a way to trigger this event myself?

    4) Is there ANY other code needed to modify besides the androidmanifest.xml or config.lua? As it stands now, I am able to verify that push works in sim-builds, but not enterprise-builds

    Here are all of the relevant sections that are in my android manifest:

    Reply
  2. NanoMathias

    I am trying to find it in the Enterprise version. I can get the one in the regular SDK working just fine, but for some reason I don’t receive the registration even in the Enterprise build.

    Reply
  3. Alan

    I have a question about sending push notifications from a server.
    I’ve done it before natively for iOS. I took the UDID from the user when they registered for push notifications, and stored that on our server. I then used that when sending pushes from the server.

    Presumably the event.token that we receive when
    event.type == “remoteRegistration”

    is the same thing? So I would record this and use it to send pushes between users or from ourselves to all users etc.

    Is that correct?

    Reply

Leave a Reply

  • (Will Not Be Published)