Connecting the Layer Client to Layer’s servers allows authentication to procede and data to flow.

How to do it

The LayerClient is initialized with a context, app ID, and optional flags. App IDs are specific to your application and should be kept private.

Create client

// Create a LayerClient ready to receive push notifications through GCM
LayerClient layerClient = LayerClient.newInstance(context, "layer:///apps/staging/cf10048c-d9ab-11e5-b6a9-c01d00006542",
new LayerClient.Options().googleCloudMessagingSenderId("GCM Project Number"));


Only one instance of LayerClient should be instantiated and used at all times. If your app is a messaging app, a good place to put this might be in the onCreate() method of your Application class or main Activity.

In order for Layer to properly manage connection and notifictaion states, it is necessary to call LayerClient.applicationCreated(app) from your Application.onCreate() method:

public class MyApp extends Application {
    public void onCreate() {
        // Lets the LayerClient track Application state for connection and notification management.


LayerClient leverages the listener pattern to notify your app about specific events. You will need to implement the LayerConnectionListener and LayerAuthenticationListener interfaces. Once implemented, register both:


Connect client

Once you have registered your listeners, connect the SDK:

// Asks the SDK to establish a network connection with the Layer service


The Layer client may become disconnected for three reasons:

  • You haven’t called the code to connect it yet
  • There’s no network access (note that it may look like the device is connected to the internet, but Layer may be blocked by a firewall, proxy, or other configuration setting)
  • You successfully deauthenticate a user

You can check if the client is connected, and reconnect as needed:

if (!layerClient.isConnected()) {

Note that if the client fails to connect the first time, we will automatically retry a few times before giving up.

Best practice

We recommend putting the connection code in exactly one place in your app — this may be when your app loads, or where your Client is initialized.

Install SDK Authentication