LiveCode LessonsHow To - LiveCode Mobile Tasks Android TasksHow do I use Push Notifications with Android?

How do I use Push Notifications with Android?

This lesson describes how to set up Push Notifications for Android Devices.

Screen captures and a sample stack are provided.

Attached Files

Introduction

Push Notifications allow you to send notifications from a server directly to an Android device. This reduces network bandwidth for applications that may normally poll a server at intervals, in order to receive an update on information the user may be waiting for.

Although Push Notifications are convenient and reduce network traffic, they are not supported by every service, both on the server side and the application side.

In order to set up Push Notifications, it is necessary that the devices register with the server. The server needs information on what device the notification is to be sent to. The device also needs to be able to receive the notification and act upon it.

This lesson is structured into the following three logical sections:

1. Signing up for Android Push Notifications

2. Writing a LiveCode Application with Push Notification Support

3. Sending a Push Notification

Signing up for Android Push Notifications (Cloud to Device Messaging)

Signing up for Android Push Notifications (Cloud to Device Messaging)

Create a new project with the google api's console at the following website https://code.google.com/apis/console/ (N.B. you will need to sign in with a Google Account ID to create a project)

Service Page for your Project

This is the default 'API Project' screen that is shown when creating a new project. You can activate many available Google services from here. You are also able to create new projects from the drop down menu, circled in the above image. We will proceed with this default template for simplicity.

 

Activating GCM (Google Cloud Messaging) for your Project

Activating GCM (Google Cloud Messaging) for your Project

In order to activate GCM you need to switch the tab, circled above, to the 'on' position. The first time this is activated in your project you are asked to accept the terms and conditions of the GCM

Creating the Server Key for Access

The next steps allow you to create a server key that is used for authorization.

1. Click 'API Access' from the left menu

2. Click 'Create new server key'

3. A new window appears. Leave the 'accept request from these server ip addresses' field empy and click create.

You now have a new 'Key for server apps (with IP locking)' in your API access area. Take a note of the API Key created as it is needed later.

Writing a LiveCode Application with Push Notification Support

Writing a LiveCode Application with Push Notification Support

When you have registered with GCM and activate the service you can create a LiveCode stack that tries to register with the Push Notification Server. Add the following code to the LiveCode stack script:

on pushNotificationRegistered tMessage

   answer "Registered:" && quote & tMessage & quote with "Okay"

end pushNotificationRegistered

Once you start the application, you should receive a message that is similar to the one shown in this step. The token displayed is specific to your device and you need to record it, so that you can send to the device from an application or a server. In this test scenario you may want to e-mail the token to yourself, possibly by updating the code as follows:

on pushNotificationRegistered tMessage

   answer "Registered:" && quote & tMessage & quote with "Okay"

   // Update the e-mail address as required

   revMail "me@my-email-address.com",,"token",tMessage

end pushNotificationRegistered

(you need to set the following  step in your 'Standalone Application Setting' for the device to register properly'

Enabling the LiveCode Stack for Push Notification Support

Enabling the LiveCode Stack for Push Notification Support

You need to put your 'Project Number' in the Push Sender ID. To obtain your project number click 'Overview' from the API Project  console that was created earlier.

Sending a Push Notification (LiveCode)

Sending a Push Notification (LiveCode)

Using the sample stack provided with this lesson, you can send a Push Notification to the device.

The API Key is the key created in the step 'Creating Server Key for Access'.

The first time you use the sample stack, make sure the tick boxes are selected. This allows you to enter the password for the google e-mail address and the token for the device (token is the number generated in the step 'Writing a LiveCode Application with Push Notification Support ').

Enter the "Message", the "Title" and the "Payload" that are to be delivered to the device. The message and title are displayed by the Android Operating system in different locations, depending on the version of the device and the activity of the application that is receiving the notification. The payload is delivered as a message to LiveCode.

What is in the Push Notification?

LiveCode uses the following fields to store and process push notification information.

data.body - The message body displayed in the status bar (default: "User interaction requested").

data.title - The title of the message displayed in the status bar (default: the app label).

data.badge_value - The badged number to display along with the statusbar message.

data.play_sound -  A Boolean that indicates whether or not a sound should be played when the notification is received (true / false).

data.payload - The message that is delivered to the app in its remoteAlert handler.

This information is needed when creating the notification you plan to send to an Android device.

14 Comments

peter

This is out of date now

C2DM has been officially deprecated as of June 26, 2012.

What do we do now?

The cloud version seems much more of their api integrated.

How do livecode uses proceed now?

Hanson Schmidt-Cornelius

Hi Peter,

the LiveCode engine can operate without any changes when you migrate from C2DM to GCM. However, it is necessary to make some changes to your LiveCode application. You have to replace the current developer email address that is passed in the sender parameter of the registered intent with the project ID that is registered when signing up with GCM.

The server side also requires modifications, but these are also minor.

You can get more information on this topic by referring to: http://developer.android.com/guide/google/gcm/c2dm.html
In particular have a look under the headings "Client changes" and "Server changes".

Kind Regards,

Hanson

Bernard

It's not at all clear from this lesson how one acquires a registration ID for the android device. As far as I can understand from the lesson, Google's GCM service should simply respond with a registration ID if a Livecode app running on an android device contains a handler with this signature: "on pushNotificationRegistered tMessage".

I've built such an app and loaded into an android device, but nothing happens. If the client registration ID is acquired by some kind of magic, I doubt that it will work for GCM as it does for C2DM, since the service endpoints (URLs) have changed. On my device the pushNotificationRegistered message is not called, and I don't see from this lesson what could possibly trigger an action resulting in it being called.

I've also seen (e.g. on StackOverflow and on the Google Groups forums), that there is a lot of confusion amongst java developers, concerning the different IDs (server, browser, client) used for GCM, and particularly how to get the client registration ID.

So this lesson needs to be updated for GCM. Most of the instructions to be found in other resources assume that people are going to install additional elements to the android SDK, modify java files, install ant, and build the google-supplied GCM demo app in java, then load that onto their devices -- all in order to obtain a registration ID. That seems like an unnecessary excursion for Livecode developers.

Bernard

I have got the pushNotificationRegistered message to trigger. The sender id (in the standalone application settings) needs to be the numeric project ID found in the URL of the Project page(s) in the Google API GCM console (something like "project:XXXXXXXXXXXX:access" in the URL when one is on the Google website, where the Xs represent the project's numeric ID required for the standalone settings).

I used the example in this lesson and emailed the client registration ID to myself, so that my server code could then address the individual devices in the GCM messages I was sending.

sampath

can we get push notification without using gcm and withour placing app in playstore?

i have a requirement to send notification/data to app user from our own webapplication . i know it is possible using google services but i want my application to send these maintanence to send notifications to android device is it possible iff please provide links regarding it,,thanks in advace

Hanson Schmidt-Cornelius

Hi Sampath,

you should not have to put your application into the android store in order to use push notification. That is basically what this lesson is doing. You will have to go through the GCM to set up your messaging, but this does not mean that you cannot send messages from your server. Have a look at the relevant android webpage for further information: http://developer.android.com/google/gcm/index.html

Kind Regards,

Hanson

Daniel DuPont

It works great. I sent a push notice and it worked. But, how do I get the message received to open in the app? Right now, when I select the message it just vanishes from my notification area. Thanks!

Hanson Schmidt-Cornelius

Hi Daniel,

you have to handle the pushNotificationReceived message that is called once the push notification is received by your device.

Have a look at the local notifications lesson for a bit of information on how the application is launched and the message processed when a notification is received: http://lessons.runrev.com/s/lessons/m/4069/l/58669

Kind Regards,

Hanson

Daniel DuPont

Thanks Hanson. That issue is solved. Now I have to figure out why a second, or later, notice sent does not update if the app is still in the recent app list. If I remove the app from the recent app list it is fine. But that is not commonly done.

Hanson Schmidt-Cornelius

Hi Daniel,

thank you for your comment.

If the application is in the recent app list and it is suspended, then it is possible that the notification does not get received. We will investigate if this is something we can address.

Kind Regards,

Hanson

Daniel DuPont

Hi Hanson,

Upon further testing, if the app is exited with the back button, it receives the next update. Only when exited with the home button is there an issue. If he app is cleared from recent apps, or is re-opened, exited with the back button, then re-opened it receives the update.

Hanson Schmidt-Cornelius

Hi Daniel,

thank you for the update. Yes, we were assuming that it had something to do with a particular state of the application. I have filed a bug report for development to look into the issue.

Kind Regards,

Hanson

William

Hi, I tried to use your push notification sample but I couldn't figure out the Device ID prompted.

Any help on how to locate the Device ID? Thanks

Hanson Schmidt-Cornelius

Hi William,

Bernard had the same issue and discussed the issue slightly further up the list of comments. This should address the issue you are having.

Kind regards,

Hanson

Add your comment

E-Mail me when someone replies to this comment