Pages

Monday, February 4, 2013

Implementing Android Push Notifications using Urbanairship (GCM) client Lib

Hello friends,
This is about how to Integrate Push Notification service of Urbanairship client(GCM) in Android App.
I didn't find a good blog for Urbanairship integration in Android for using Google's GCM service. Processes dictated in Urbanairship docs are good but not much clear for new developers.

As we know rather than using GCM lib. directly prefer to use Urbanairship services which are pretty simple.
If you want to read about GCM here you go with Google GCM doc.

I will explain complete process from start to end the way I did and it's working.

If you have your project ready to integrate lets start.
Steps:

1. First of all register your Android project on Google API site. If you visiting site first time, get to see below window(You need to use your gmail account to login).



Clicking on create project will lead to new page, on left hand side drop down menu select "Create Project". Provide name of your project. It will bring you on dashboard where you get the details of your project.
From there save the 12 digit "Project Number" (e.g. 933568175224) you'll need it later.
Go to Services on left hand side turn on "Google Cloud Messaging for Android".

2. Now to use Urbanairship client(GCM) you have to Register on Urbanairship.
After logging in go to "Create New App" fill details properly, for Android device add package name (e.g. com.android.gcmtestproject) and check Push enable.
This process leads to a details page where you'll receive Application Key, App Secrete we are going to use it in airshipconfig.properties file, we'll get back to this later.

As result of above two steps your registered with Google API and Urbanairship.

3. Now you need Urbanairship lib for integration so Downlaod latest zip (click to download zip 1.0MB). In that you'll get Urbanairship lib and a sample application to demonstrate the process.
Just copy that urbanairship.2.0.0.jar and paste to your android projects /lib/ folder. Add this jar file to your projects build path.

GCM Early Access Urbanairship this doc page dictates you everything but I'm explaining in detail.

4. Create airshipconfig.properties file in /assets/ folder now add following lines in it.


As you noticed in above code we commented productionAppKey and productionAppSecret because we are not deploying it to market when you do so remove comment and add both field values.

5. Setting up your AndroidManifest.xml file with the necessary receivers, services and permissions to start receiving push notifications. You can also find AndroidManifest.xml in sample project from urbanairship.zip that you downloaded.


In above manifest file just replace com.urbanairship.push.sample with your projects package name (here I've com.android.gcmtestproject).

6. Enabling Push: By default, push is disabled in the client library. So, for testing purposes, enabling push service on start up.
Client library automatically starts push upon takeOff.
Add following lines to your projects application’s onCreate method.


7. Get APID: The APID is a unique identifier that ties together an application/device pair on android. You can use this ID to target pushes to specific devices using the Urban Airship API. Once an APID is created, it will persist the application until it is either re-installed, or its internal data is cleared.
Add following lines to your projects application’s onCreate method.


8. Now write IntentReceiver class which extends BroadcastReceiver. In it's onReceive() do the following

get action from intent:

When action is ACTION_PUSH_RECEIVED or ACTION_NOTIFICATION_OPENED then you can extract data from intent and pass it to another activity. When action is ACTION_REGISTRATION_FINISHED you can get APID through following code.


I described APID in step 7.

Note this APID as we'll use it to send push notifications from Urbanairship and don't forgot to add IntentReceiver to your manifest file.

So, these are the steps pretty simple. Your app is now ready to receive Push notifications from Urbanairship.

For testing that push notification on device is working or not go to https://go.urbanairship.com. Log in
then on left hand side in your App's Details window click on Push then click on "Send Broadcast" as we created IntentReceiver class which is BroadcastReceiver so we receive fired intent.
Or click on "Testing Push Notifications".

Note while sending and receiving Push notification you have to log in from your Google account on Android device to receive Pushes.

A little prob. as we know that GCM is not completely replaced for C2DM and on Urbanairship Early Access is available.
I got response mail from Urbanairship support team i.e. "GCM early access requests filled much faster than expected and new activations have been halted. So we have to wait till release."

If you're not receiving Push notifications then send an email to Urbanairship(support@urbanairship.com) with following details.

Subject should be specific.

Account Type is : Pro
Application name : GCMTestApplication
Application Key : dqzp7LmjTRG65cZBfUhMxw

Message


Then Urbanairship support team replies you with case no. and they try to clear it ASAP, and then your application can receive push notifications.

4 comments:

  1. Hmm, that is some compelling information you’ve got going! Makes me scratch my head and think. Keep up the good writing!

    I am going to try this great information with my best push notification server.

    ReplyDelete
  2. Nice post with great details and info. I really appreciate your job. Thanks for sharing. Keep it up.

    emergency notification

    ReplyDelete
  3. Mobile phones have become an integral part of our lives, and we rely on them for various tasks, including entertainment. With the rise of streaming platforms and mobile apps, we can now watch our favorite shows and movies on the go. XMLTV integration on mobile phones takes this convenience to the next level by allowing users to access TV listings and guides wherever they are. By installing a compatible XMLTV app on their mobile phones, users can stay updated with their favorite TV shows and movies, no matter where they are.

    ReplyDelete