Liferay Screens supports push notifications in Android apps. To use them, you must configure some APIs and modify your app to consume and/or produce push notifications. This tutorial shows how to do all these things.
Your first step is to create and configure a Google project to use Google Cloud Messaging (GCM). You also need to configure the Liferay Push app to use the project’s GCM API.
Follow these steps to create and configure a Google project to support cloud messaging:
On the Google Cloud Messaging page, create a configuration file by clicking Get a Configuration File. On the screen that appears, set your App name and Android package name, and then click Continue To Choose and Configure Services. On the next screen, click Enable Google Cloud Messaging.
Copy and save the Server API Key and Sender ID values you’re presented with. You’ll need to use these values later as the push notifications API keys for Liferay Push.
![Figure 1: You need the Server API Key and Sender ID to enable Liferay Push.](../../../images/screens-android-push-project-and-server-key.png)
Now that you’ve set up your Google project, you can configure the Liferay Push app to use the project’s GCM API. Install the Liferay Push app from the Liferay Marketplace. In your Liferay DXP instance’s Control Panel, navigate to Configuration → System Settings, select the Other tab, then select Android Push Notifications Sender. Set the push notifications API Key to the value of the Server API Key you generated in your Google project. You can also set the number of retries in the event that sending a notification fails.
Great! Your Liferay DXP instance is now ready to send push notifications to your Android apps!
The Liferay Push Client for Android streamlines registering a device with the portal for receiving and sending push notifications. Although the information below contains the main steps needed to use the client, the readme explains them in detail.
In your Android application’s Gradle build file, add a new dependency on the Liferay Push Client for Android:
Make sure your app’s
liferay-plugin-package.properties file specifies the Push
Notifications portlet as a required deployment context:
Next, you’ll learn how to register listeners for push notifications.
First, register your device in GCM with the
SENDER_ID you generated
Session session = new SessionImpl(YOUR_SERVER, new BasicAuthentication(YOUR_USER, YOUR_PASSWORD));
If you’re using Liferay Screens to maintain a session, you can retrieve it and use it instead of creating a new one:
If you use these example lines of code, make sure to replace
YOUR_SENDER_ID with your own values.
That’s it! You can attach a listener to store the registration ID or to process
the notification sent to the activity (using
onPushNotification()). You can
also register a receiver and service to process the notification. You can refer
Push Notifications project
as an example push notifications implementation.
Next, you’ll learn how to send push notifications.
Using the Liferay Push app, sending notifications to your app’s users is straightforward. You can specify the user IDs along with the message content:
This example hook plugin sends a push notification each time a user creates a new DDL record or updates an existing one.
In your app’s
portal.properties file, you can add a listener for a class by
property, set to a comma separated list of intended listener classes. Here’s an
example listener setting for
Great! Now you know how to configure your Android apps to receive push notifications from Liferay DXP.
In this tutorial, you’ve configured your portal to accommodate push notifications, registered notification listeners, and implemented sending push notifications. Way to go!