Creating Hybrid Apps with Liferay DXP Headless API and Ionic Framework
Step by step guide
Lorenzo Carbone
July 15, 2020
2 Minute Read
If you are thinking of creating a hybrid mobile application for Android and iOS devices, surely you could have heard of the Ionic Framework. Personally, I think it is among one of the most complete and simple to use open source solution and today I would like to explain how to use Liferay DXP API to create a simple app that can show the blog posts of a site within our Liferay instance.
2. Find the Site Id It is possible to identify the Site Id by going to Configuration -> Settings in the "Site Administration Menu" of Liferay DXP.
3. Create Ionic App Install Node.Js and NPM on your computer and then install the Ionic CLI, then create a new project and start the local development server for our app:
npm install -g @ionic/cli
ionic start myApp sidemenu
cd myApp
ionic serve
4. Create Rest Service In order to call the Liferay DXP APIs from our Ionic app, create a simple Provider to have a single point of access within our App to manage the types of REST calls.
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { map } from 'rxjs/operators';
let apiBaseUrl = 'http://localhost:8080/o/headless-delivery/v1.0/sites/';
let siteId = "xxx";
let apiUrl = apiBaseUrl + siteId + "/blog-postings?sort=dateCreated%3Adesc";
let username = 'test';
let password = 'test';
let token = btoa(username + ":" + password);
@Injectable()
export class RestService {
constructor(
public http: HttpClient) {
}
getData(type){
return new Promise((resolve, reject) =>{
let headers = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Basic ' + token + ''
});
this.http.get(apiUrl + type, {headers}).
subscribe(res =>{
resolve(res);
}, (err) =>{
reject(err);
});
});
}
}
By default, for calling the APIs, authentication is required and Liferay DXP offers 3 modes:
Basic Authentication
OAuth 2.0
Cookie / Session Authentication
For this example, I used Basic Authentication mode and to use it, just add the string with Base64-encoded username and password in the HTTP Call Header:
5. Showing the Blog Post Let's put together what has been done so far and create what is necessary to show blog posts within our App. Create a new page within the App using the command:
ionic g page "Blog Post"
Open the generated file "blog-post.page.ts", import our "RestService" and add the call to the Liferay DXP API in the "ngOnInit ()" method to get the blog posts.
This website uses cookies and similar tools, some of which are provided by third parties (together “tools”). These tools enable us and the third parties to access and record certain user-related and activity data and to track your interactions with this website. These tools and the informationcollected are used to operate and secure this website, enhance performance, enable certain website features and functionality, analyze and improve website performance, and personalize user experience.
If you click “Accept All”, you allow the deployment of all these tools and collection of the information by us and the third parties for all these purposes.
If you click “Decline All” your IP address and other information may still be collected but only by tools (including third party tools) that are necessary to operate, secure and enable default website features and functionalities. Review and change your preferences by clicking the “Configurations” at any time.