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.
You can download the sample stack from this url: https://tinyurl.com/cvyjrsx6
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)
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
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
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.