Recent Bloggers

Saurang Suthar
Posts: 5
Stars: 0
Date: 7/4/17
Jyoti Verma
Posts: 3
Stars: 0
Date: 6/28/17
Pankti Patel
Posts: 1
Stars: 0
Date: 5/26/17
Chandrika Baraiya
Posts: 5
Stars: 0
Date: 5/26/17
Atith Patel
Posts: 5
Stars: 0
Date: 5/22/17
Nisarg Pujara
Posts: 6
Stars: 0
Date: 5/10/17
Pranavsinh Parmar
Posts: 4
Stars: 0
Date: 5/3/17
Ankit Prajapati
Posts: 5
Stars: 0
Date: 5/3/17
Pratik Solanki
Posts: 5
Stars: 0
Date: 4/7/17
Chirag Patel
Posts: 4
Stars: 0
Date: 3/30/17
« Back

Liferay Push Notification in Android Application

Push notifications is a technology that broadcast messages to mobiles from server. Rather than having your application ask the server for new messages after every certain time interval, your application can wait for notifications from the server. Your application will get notification even when it is not running.

 

Here, I am going to show when any document or media content is updated, deleted or newly inserted in liferay’s document and media library then the registered users will get a push notification in android application. I have used Liferay 6.2 CE GA4 Portal and Android Studio 1.3.2 for this example.  

 

Prerequisite :

  • Basic knowledge of android and Liferay’s document and media portlet.

  • Android SDK with any IDE (Eclipse or Android Studio etc.) and Liferay should be properly setup.   

 

Step-1: Configuration to use liferay push notification:

 

Step-1(A): Create a google project and configure it to use Google Cloud Messaging (GCM) service.

  • Go to https://console.developers.google.com/project URL and create a project. Note your project number (ex. 901902458912). This project number is used as SENDER_ID for push notification.
  • Enable push notification for your project by going Enable and Manage API’s . In the API search bar type Google Cloud Messaging for Android, open it and Enable this API by clicking on Enable API.
  • To access google project’s messaging API at Liferay side, we need a key. To generate the required key, click on the credentials tab. Go to create new credentials and  click on API key, choose server key option from the available options. Leave Name field as it is (you can change also) and IP address field empty and click on create button. This will generate your API key. Note this API key, because this API key will be used for Liferay push app.

Step-1(B): configure the Liferay push app.

  • Downlaod liferay push app from http://www.liferay.com/marketplace and install it. After installation go to
    portal’s control panel, choose Push Notifications tab. Set the API key field value to the API key generated in
    google project.
    Now the portal is ready to send push notification to android app.


Step-2: Setup to receive push notification at android app side:

 

Step-2(A): Add the following libraries to project’s build.gradle file.

 

repositories {

jcenter()

mavenCentral()

}

 

dependencies {

compile ‘com.liferay.mobile:liferay-push:1.0.2’

}

 


Step-2(B): Below is the code snippet to register your device and receive push notifications:

 

public class MainActivity extends AbstractPushActivity {

 

   public static final String SENDER_ID = "672875010101";

 

   @Override

   protected Session getDefaultSession() {

       return new SessionImpl("http://10.0.2.2:8080", new BasicAuthentication("test@liferay.com", "test"));

   }

   @Override

   protected String getSenderId() {

       return SENDER_ID;

   }

   @Override

   protected void onPushNotificationReceived(JSONObject jsonObject) {

   }

   @Override

   protected void onErrorRegisteringPush(String s, Exception e) {

   }

}

 

 

In the above code, replace SENDER_ID field value with your project number which is generated when you created

google project.

 

Step-3: Creating a liferay-hook for document and media library to listen the event and send push notifications:

 

We need to create a hook which implements a ModelListener<DLFileEntry> to track the activity on Document and Media

portlet files and directories.


Below is the code snippet to implement a hook for document and media portlet to send push notification.
 

public class DocumentMediaFilesListener implements ModelListener<DLFileEntry> {

 

@Override

public void onAfterCreate(DLFileEntry arg0) throws ModelListenerException {

}

 

@Override

public void onBeforeCreate(DLFileEntry arg0) throws ModelListenerException {

}

}

 

 

Send push notification with the following code from any of the above method:

 

JSONObject notification = new JSONObject();

notification.put(“message”,”Hello”);

PushNotificationsDeviceLocalServiceUtil.sendPushNotification(toUserId, notification);

 

Step-4: Add the following entry in your portal.properties file:

 

value.object.listener.com.liferay.portlet.documentlibrary.model.DLFileEntry =

com.st.custom.hook.DocumentMediaFilesListener


Update the package and file name in the above entry according to your configuration.

 

Step-5: Once all the above steps are properly configured, then your android device will receive update notifications whenever any document is added, deleted or updated by any other user.  

 

For more implementation details or support you may contact us at enquiry@surekhatech.com.

l

Comments
Trackback URL:

Smith Felix
Thanks for all your valuable effort on this article.
http://www.techgreeks.com/
Posted on 8/22/16 3:43 PM.

Contact Us

Loading

Get in touch

Headquarter :
302, Landmark,
Nr. Titanium City Center,
Prahlad Nagar Road,
Ahmedabad, India - 380015.