Using Liferay Push in Android Apps

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.

Configuring to Use Liferay Push Notifications

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:

  1. 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.

  2. 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 ConfigurationSystem 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.

Figure 2: Set the API key and number of retries in your Liferay DXP instance.

Figure 2: Set the API key and number of retries in your Liferay DXP instance.

Great! Your Liferay DXP instance is now ready to send push notifications to your Android apps!

Receiving and Sending Push Notifications

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:

dependencies {

	...

	implementation 'com.liferay.mobile:liferay-push:1.2.1'
}

Make sure your app’s liferay-plugin-package.properties file specifies the Push Notifications portlet as a required deployment context:

required-deployment-contexts=\
    push-notifications-portlet\
    ...

Next, you’ll learn how to register listeners for push notifications.

Receiving Push Notifications

First, register your device in GCM with the SENDER_ID you generated previously:

Session session = new SessionImpl(YOUR_SERVER, new BasicAuthentication(YOUR_USER, YOUR_PASSWORD));

Push.with(session).register(this, YOUR_SENDER_ID);

If you’re using Liferay Screens to maintain a session, you can retrieve it and use it instead of creating a new one:

Push.with(SessionContext.createSessionFromCurrentSession()).register(this, YOUR_SENDER_ID);

If you use these example lines of code, make sure to replace YOUR_SERVER, YOUR_USER, YOUR_PASSWORD, and 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 to the Push Notifications project as an example push notifications implementation.

Next, you’ll learn how to send push notifications.

Sending 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:

PushNotificationsDeviceLocalServiceUtil.sendPushNotification(userIds, 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 creating a value.object.listener property, set to a comma separated list of intended listener classes. Here’s an example listener setting for DDLRecord objects:

value.object.listener.com.liferay.portlet.dynamicdatalists.model.DDLRecord=com.liferay.push.hooks.DDLRecordModelListener

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!

Preparing Android Projects for Liferay Screens

Using Screenlets in Android Apps

« Packaging Your Android ScreenletsAccessing the Liferay Session in Android »
Este artigo foi útil?
Utilizadores que acharam útil: 0 de 0