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/yaq4dxcm
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 api's console at the following website https://console.developers.google.com/cloud-resource-manager (N.B. you will need to sign in with a Google Account ID to create a project)
Give a name to your new project and click "Create"
Click in the "Products & Services" hamburger icon
Then choose "APIs & Services" and then "Library"
Search for "Google Cloud Messaging" and click on the first result, as shown above.
Now click on the "ENABLE" button
Creating the API Key for Access
Next, click on the "Credentials" option in the left pane.
Next, click on the "Create Credentials" button, as shown above.
Choose "API Key" in the dropdown menu
You have now created the API Key. Click on "Copy" and paste the API Key to a safe place. I have hidden the last part of my key for security.
Select the "Key restriction" level, and click on "Save". For simplicity I have selected "None", but it is suggested to add a restriction level.
Click on the hamburger icon (1), then select "IAM & admin" from the dropdown (2), and then "Settings" (3).
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 "Project Number" 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 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 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@example.com",,"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 key created in the step "Creating API Key for Access".
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.