Coder Social home page Coder Social logo

liferay-push-android's Introduction

Liferay Mobile SDK logo

Liferay Push for Android

Build Status Coverage Status

Setup

Add the library as a dependency to your project's build.gradle file:

repositories {
	jcenter()
	mavenCentral()
}

dependencies {
	compile 'com.liferay.mobile:liferay-push:1.0.2'
}

Use

Registering a device

To receive push notifications, your app must register itself to the portal first. On the portal side, each device is tied to a user. Each user can have multiple registered devices. A device is represented by a device token string. Google calls this the registrationId.

Read Android's documentation to learn how to get the registrationId.

It's easy to obtain a registrationId with Liferay Push for Android, you just have to call to the following method:

import com.liferay.mobile.push.Push;

Session session = new SessionImpl("http://localhost:8080", new BasicAuthentication("[email protected]", "test"));

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

The SENDER_ID is the identifier of your GCM project, if you follow the official Android's documentation it should be easy to obtain one.

Since all operations are asynchronous, you can set callbacks to check if the registration succeeded or an error occurred on the server side:

Push.with(session)
	.onSuccess(new Push.OnSuccess() {

		@Override
		public void onSuccess(Object result) {
			System.out.println("Device was registered!");
		}

	})
	.onFailure(new Push.OnFailure() {

		@Override
		public void onFailure(Exception e) {
			System.out.println("Some error occurred!");
		}

	})
	.register(registrationId);

The onSuccess and onFailure callbacks are optional, but it's good practice to implement both. By doing so, your app can persist the device token or tell the user that an error occurred.

Liferay Push for Android is calling the GCM server, retrieving the results and storing your registrationId in the Liferay Portal instance for later use. If you obtain the token manually, you can register the device to the portal by calling the following method:

Push.with(session).register(registrationId);

Now each time the portal wants to send a push notification to the user [email protected], it looks up all registered devices for the user (including the one just registered) and sends the push notification for each registrationId found.

You should note that the Push class is a wrapper for the Mobile SDK generated services. Internally, it calls the Mobile SDK's PushNotificationsDeviceService class. While you can still use PushNotificationsDeviceService directly, using the wrapper class is easier.

Receiving push notifications

Once your device is registered, your app must be able to listen for notifications.

There are several ways to do this:

  • If you want to receive global notifications even if the application is not active at the moment you should implement a BroadcastReceiver instance in your app . Android's developer documentation shows you how to do this.

  • If you want to execute an action or show a notification only if the application is active, you could register a callback:

Push.with(session).onPushNotification(new Push.OnPushNotification()

	@Override
	public void onPushNotification(JSONObject jsonObject) {
	}

});

This method only works if you have already registered against Liferay Portal using the previous instructions.

Sending push notifications

There are many ways to send push notifications from Liferay Portal. See the Liferay Push documentation for more details. Alternatively, you can send push notifications from your Android app. Just make sure the user has the proper permissions in the portal to send push notifications.

JSONObject notification = new JSONObject();
notification.put("message", "Hello!");

Push.with(session).send(toUserId, notification);

In this code, the push notification is sent to the user specified by toUserId. Upon receiving the notification, the portal looks up all the user's registered devices (both Android and iOS devices) and sends notification as the body of the push notification.

Unregistering a device

If you want to stop receiving push notifications on a device, you can unregister it from from the portal with the following code:

Push.with(session).unregister(registrationId);

Users can only unregister devices they own.

liferay-push-android's People

Contributors

javiergamarra avatar ngaskill avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.