How do I use Push Notifications with Android?

Note: This lesson is now deprecated. If you are using Livecode 9.6.5 or above, see this new lesson: https://lessons.livecode.com/m/4069/l/1473964-how-do-i-use-push-notifications-with-android-in-livecode-9-6-5

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

Screen captures and a sample stack are provided.

You can download the sample stack from this url: https://tinyurl.com/cvyjrsx6

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's Firebase console at the following website https://console.firebase.google.com/ (N.B. you will need to sign in with a Google Account ID to create a project)

Give a name to your new project, accept the terms and click "Continue"

Enable Google Analytics if you wish, and then click on "Create Project".

The new project will be created.

Click on the cog icon next to "Project Overview":

Then choose "Project Settings", and click on the "Cloud Messaging" tab:

Write down the values of "Server Key" and "Sender ID". You'll need these later. I have hidden the last part of my key for security.

Write down the "Project Number". This will be used in the Standalone Application Settings for Android, in the field "Push Sender ID".

Enabling the LiveCode Stack for Push Notification Support

Go to Standalone Application Settings for Android, and in the field "Push Sender ID", fill in the "Sender ID" you wrote down in the previous step. Moreover, make sure you have enabled the "Internet" permission.

Writing a LiveCode Application with Push Notification Support

Writing a LiveCode Application with Push Notification Support

When you have registered with Firebase and activated 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 pMessage
   answer "Registered:" && quote & pMessage & 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 pMessage
   answer "Registered:" && quote & pMessage & quote with "Okay"
   // Update the e-mail address as required
   revMail "[email protected]",,"token",pMessage
end pushNotificationRegistered

 

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 "Server Key" you have written down in a previous step.

In the "Devices" field enter the token 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.

34 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

Ross Morrow

I used to use this lesson to work with Push Notifications. However, Google has now switched to a system called Firebase and I cannot get push notifications to work anymore with Livecode. Is there an update how to use Android Push Notifications with Livecode?

Elanor Buchanan

Dear Ross

This has been reported as an enhancement requested here

http://quality.livecode.com/show_bug.cgi?id=18075

I think the most relevant part of the discussion there is that you can use FCM to send push notifications to LiveCode apps.

--------------------
Hi all, thanks for your comments on this so far.

FCM itself is built upon the existing GCM messaging infrastructure so apps using GCM should continue to work.

It is also possible to use FCM to send push notifications to existing LiveCode apps with only a minor modification to the standalone settings, with no changes to the manifest required.

The process for setting up push notifications with FCM is somewhat different now though, as I will describe below:

1. log in to the Firebase console at https://console.firebase.google.com/
2. Create a new project for your app (or import an existing one if you already use GCM)
3. Click the settings icon and navigate to "Project Settings" then "CLOUD MESSAGING"
4. Replace the "Push Sender ID" in your app's Android standalone settings with the numeric "Sender ID" listed on that page.
5. Replace the server key you currently use to send push notifications with either of the "Server Key" or "Legacy Server Key" strings listed on that page.

You should now be able to send push notifications to your android apps once again.

Ian.
--------------------

There is some other information in the comments on the bug report that you might find useful.

I hope that helps.

Kind regards

Elanor

Simon Schvartzman

Hi Elanor, after following the lesson I made this simple App (named Notification) which is able to register with Firebase Cloud Messenger.
When I use exactly the same code and same Push Sender ID on my "full" App, the same device does not register, as a matter of fact the "on pushNotificationRegistered..." is not even called.
I should mention that before running the full App I deleted the Notification App in order to avoid any conflict.
What am I doing wrong?
Any hint will be greatly appreciated. Regards

Elanor Buchanan

Hi Simon

I'm afraid it is very difficult to know what the issue might be. Have you checked that Push Sender ID and Internet permissions are correct in the Standalone Application Settings for your "full" App?

Kind regards

Elanor

Simon Schvartzman

Hi Elanor, thanks for your reply. I know how difficult it is to identify the problem as I've been fighting with it for 2 days before posting here...
The Push Sender ID number is the same on both Standalone settings and the Internet Application Permission is also set on both Apps.
The Label and Identifier contents are different as you may expect.
I have also checked in the Android's App Notification permissions and both Apps are configured the same.
Is there anything else I should be checking for?
Could the problem somehow be related with Google Cloud Project Settings? One thing that is not clear to me is where do I "connect" the Google Cloud Project with my App? In other words as the Push Sender ID seems to be the only "link" between the two it seems that I could potentially have 2 or more Apps receiving the same Notifications which doesn't seem to make sense. I have the feeling this is where I'm doing something wrong.

Simon Schvartzman

Hi Elanor, I found the problem (still need help to find the solution) and reported it on the forum:
https://forums.livecode.com/viewtopic.php?f=53&t=32860

Elanor Buchanan

Hi Simon

I see you have added a comment to an older bug, I'll flag it with the team to be looked at.

Elanor

Simon

Hi team, does anyone know if there is any way to customize the notifications Icons shown on the Android device (the one on the status bar and the one on the home screen when the App is closed). I'm using FCM and I wonder if that would be possible, I would guess it is possible the question is how!
Thanks all...

Frédéric

Hi,
I try to send a push notification it is working but i just saw "Notification Received" Without the message that i send.

Elanor Buchanan

Hi Frédéric,

Was your app open or not when the notification was received? Have you added a pushNotificationReceived handler to your app?

Kind regards

Elanor

Elanor Buchanan

Hi Simon

You can set the Status Bar Icon in the Android Standalone Application Settings, the option is just below the Push Sender ID.

Kind regards

Elanor

Frédéric Jean Guillaume

Hi Elanor

I try it both way when the app is close and when the app is working is the same the message did not appear.I am not sure if i am the one doing something wrong. I use the "AndroidGCM" Stack to send the push notification and i follow one of the last comments i found here is not working. I try from the FCM console not working. But when i try with this link "https://www.apnstester.com/fcm/" i receive the push notification but without the text.

Thanks

Frédéric

Sphere

Hi, previously you could use multi-color icons. But not anymore since a year or so. So make sure your icon is only white on transparent background. This lesson needs a major update as well how you can use push notifications. There is a bug report alrady for support of FCM. As this is still based on GCM which still works via the FCM servers as GCM is still the base, but you need FCM. FCM can do lots more like multiple lines of text and even a little image in the push notifications as other options. Current you can only put a short single line of text in the message. So there is a feature request for it.

Frédéric Jean Guillaume

Hi Elanor

It's working now i just change the post url from the card script from GCM to FCM.
Thanks.
FREDERIC

Andrew Bell

The endpoint that messages should be sent through has changed again. This following command needs to be updated in the pushNotificationAndroid handler:

# URL updated on 19 Feb 2018
post tNotification to url "https://gcm-http.googleapis.com/gcm/send"

... should be updated to...
# URL updated on 29 May 2018 @ https://developers.google.com/cloud-messaging/android/android-migrate-fcm
post tNotification to url "https://fcm.googleapis.com/fcm/send"

Arthur Lehniger

Hello, in my case, it is functional. But it's not beatiful, when I have to get a key from every device which uses the app. Is it possible to establish the token for the app?

Elanor Buchanan

Hi Arthur

The token is a unique key for the app/device and allows notifications to be sent to the intended device so it is required. The best way to handle token management etc will depend on what you want to achieve. You can find out more at the Firebase documentation

https://firebase.google.com/docs/cloud-messaging/

I hope that is some help.

Kind regards

Elanor

Wes

Can someone update the tutorial to reflect the new Firebase setup? Someone mentioned it was outdated so that's why I'm asking. Ok, my real question is this. When I user registers on my APP, I want to store their device token in FCM servers at the same time upon registration so I can push notifications to their device.

I know how to save the user information in my backend database but not sure how to register the device on FCM. Any help will be appreciated.

Panos Merakos

Hello Wes,

The lesson was updated 2 weeks ago. It does show how to use the Firebase console to get the info you need for sending (and receiving) push notifications in LiveCode.

Once you start the android application, the stack receives a message "pushNotificationRegistered" with the device token as a parameter, just like the example above.

Hope this helps.

Kind regards,
Panos
--

Wes

Thanks Panos. I got it now.

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.