Tuesday, May 9, 2017

Send Notification to Mobile device using java



This is mobile era. Most of portal have it's own mobile app. Many time we have situation to access portal data from mobile app using web services. Also need to notify mobile user by notification after processing request successfull. I spend more then a day to find the api and implement to achive this requirement. So here i am sharing how we can send mobile notification from java.


public static Response sendAndroidNotification(String deviceToken,String message,String title) {
       OkHttpClient client = new OkHttpClient();
       MediaType mediaType = MediaType.parse("application/json");
       JSONObject obj = new JSONObject();
       JSONObject msgObject = new JSONObject();
       msgObject.put("body", message);
       msgObject.put("title", title);
      // msgObject.put("icon", ANDROID_NOTIFICATION_ICON);
       msgObject.put("color", "#ff0000");

       obj.put("to", deviceToken);
       obj.put("notification",msgObject);

       RequestBody body = RequestBody.create(mediaType, obj.toString());
       Request request = new Request.Builder().url(FIRE_BASE_SERVER_URL).post(body)
               .addHeader("Content-Type", "application/json")
               .addHeader("Authorization", "key="+SERVER_KEY).build();

       Response response;
try {
response = client.newCall(request).execute();
_log.info( "Response : " + response.body().string());
return response;
} catch (IOException e) {
_log.info(e.getMessage(),e);
}

return null;
}


Note:
FIRE_BASE_SERVER_URL = https://fcm.googleapis.com/fcm/send
SERVER_KEY = Your app server key. you can find it from firebase portal.
deviceToken = Your deviceId is generate when you install app. you can find it from firebase portal.


This is very simple implementation to send notification to mobile device using java.

HTH!!

Thank you,
Ketan Savaliya

Tuesday, April 18, 2017

Liferay7: Changes in search container tag

Hi,

Liferay search container is the very common practice for liferay developer to display list of data. So, here is am writing something interesting for search container in liferay new version called Liferay7 or Liferay DXP.

Earlier or older version, we use or setup search container tag like below....

<liferay-ui:search-container
emptyResultsMessage="no-users-were-found"
iteratorURL="<%= iteratorURL %>"
>
          <liferay-ui:search-container-results
                   results="<%= UserLocalServiceUtil.getUser(themeDisplay.getUserId()), searchContainer.getStart(), searchContainer.getEnd(), searchContainer.getOrderByComparator()) %>"
                     total="<%= UserLocalServiceUtil.getUser(themeDisplay.getUserId()).size() %>"
        />
       ...
       ...
       ...
</liferay-ui:search-container>


result & total attribute are configure in the liferay-ui:search-container-results tag. While in DXP version the total attribute move to the liferay-ui:search-container tag. So, we just have to
setup results attribute into liferay-ui:search-container-results. Below is the example how we use search container in liferay7 or DXP.


<liferay-ui:search-container
emptyResultsMessage="no-users-were-found"
iteratorURL="<%= iteratorURL %>"
 total="<%= UserLocalServiceUtil.getUserCount(themeDisplay.getUserId()) %>"
>
              <liferay-ui:search-container-results
                      results="<%= UserLocalServiceUtil.getUser(themeDisplay.getUserId()), searchContainer.getStart(), searchContainer.getEnd(), searchContainer.getOrderByComparator()) %>"            
             />
       ...
       ...
       ...
</liferay-ui:search-container>


total is moved from liferay-ui:search-container-results to liferay-ui:search-container tag is only change in search container in Liferay7 or DXP.

HTH!!

Regards,
Ketan Savaliya

Tuesday, March 28, 2017

Liferay7 : Removed the liferay-ui:journal-article Tag

 Hi,

In liferay till version 6.2, Very easy way to display journal article using `liferay-ui:journal-article`. The `liferay-ui:journal-article` tag has been removed.  Now, You should use the `liferay-ui:asset-display` tag instead. below is the piece example for how to use it.


**Example**

Old code:

    <liferay-ui:journal-article
        articleId="<%= article.getArticleId() %>"
    />

New code:   

 <liferay-ui:asset-display
            className="<%= JournalArticle.class.getName() %>"
            classPK="<%= journalArticle.getResourcePrimKey() %>"
            template="<%= AssetRenderer.TEMPLATE_FULL_CONTENT %>"
        />

HTH!!