How do I use Push Notifications with Android in LiveCode 9.6.5?
In LiveCode 9.6.5, the Android engine is built using version 30 of the Android API, a requirement for new apps submitted to the Google Play store.
This has required some changes for Android apps. One of them is that GMC (Google Cloud Messaging) has now been retired, and replaced with Firebase Messaging. This requires a small change to Android Standalone Application Settings: the
Push Sender ID should be replaced with the path to a
google-settings.json configuration file which can be downloaded from the Firebase console.
In this lesson we will go through all the necessary steps, from setting up a new project in Firebase Console to sending a push notification to your LiveCode Android app.
Basic setup in Firebase Console
First, visit https://console.firebase.google.com/u/0/ and click on "Add project"
Give your project a name and press Continue:
In the next screen, you can turn off Google Analytics if you wish, and then click Create Project:
Wait while your project is created:
Click Continue once the new project is ready:
Next step is to add an Android app to this project:
Click on the Android icon to add an Android app to the project:
In the next screen, enter the Android Package Name. This should match the "Identifier" field in the Android Standalone Settings, e.g.
Once you enter a package name, the "Register app" button is enabled. Press it to register your app:
Now you should have reached the next step, which is to download the
Download this file (google-services.json) and store it in a safe place. You will need this file in the Android Standalone Settings
Configuring Push Notification in the Android Standalone Settings
You need to check the
Push Notification checkbox, and then select the
google-services.json file you downloaded in the previous step. Moreover, make sure you have enabled the "Internet" permission.
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 "firstname.lastname@example.org",,"token",pMessage end pushNotificationRegistered
on pushNotificationReceived pMessage answer "Push Notification:" && quote & pMessage & quote with "Okay" end pushNotificationReceived
Sending a Push Notification
Visit Firebase's Notification Composer here: https://console.firebase.google.com/u/0/project/_/notification and choose your project:
In the next screen, choose Send your first message:
In the next screen, you can compose your notification. Enter a title and text for the notification, and click Next:
In the Target section, choose your target app id from the dropdown, and click Next:
In the Scheduling section, choose "Now" and click Next:
In the Additional options section, you can specify the "Payload" value. Note that the key is "livecode.payload", not just "payload". Then enter a value for the key "livecode.payload". This will be the payload string that will be received in the
Now go back to the "Notification" section, and click on the Send test message button:
In the dialog that opens, enter the device token, i.e. the one you got in the
Once you enter the device token, press Test:
The registered device will receive the notification: