Leanplum first basic instrumentation and registering for push [Sample: iOS, Android, Unity]

Introduction

In this Setup article we will see how setup Leanplum in a basic iOS and Android project, including guidance for configuring your project to set up push notifications.

iOS

Sample project (iOS)

Project sample: Click here.

Installation (iOS)

Follow the steps in the SDK Setup doc here: Leanplum SDK setup, following the instructions for the desired language (ObjC or Swift).

Once the project is ready and Leanplum is imported, we can start to configure Leanplum.

The general recommendation is to put [Leanplum start] inside didFinishLaunchingWithOptions in the AppDelegate, starting Leanplum after setting the Project keys, deviceId (if necessary) and defining any eventual callback.

Push notification registration (iOS)

First, obtain Push Certificates for iOS and upload them in the Leanplum dashboard, under App Settings. See Apple's documentation for obtaining the required .p12 certificates.

Now, for registering for Push Notifications, there are two different approaches:

  1. Register for push from the app.

See the Sample project linked above.

In this case, the code responsible for registering for Push is called when clicking on the 'Register for Push' button (see ViewController.m).

When clicking on the Register for Push button, the default Apple Push Notification prompt is displayed. This prompt can be displayed only once. Based on the user's response, Push Notifications are either enabled (and registered) or disabled. To change this setting, the User needs to manually go into the phone settings and change the preferences for receiving push notifications for that app.

  1. Register for push using the Push Pre-Permissions in-app message.

In this case, there is no need of adding any Push registration code in the app. Simply set up a new Push pre-permission message in the Dashboard. This message will ask the user if they are interested in proceeding to register for Push or give them more time to think about it.

If they proceed to register for Push, the default Apple Push Notification prompt is then displayed (see #1).

However, if the User decides to think about it and maybe register later, clicking on the other option would allow Push Pre-Permissions message to be displayed again (based on the targets or 'display when' settings in the Leanplum Dashboard).

Android

Sample project (Android)

Access to the sample project: Click here.

Installation (Android)

Follow the steps in the SDK Setup doc here: Leanplum SDK setup, following the instructions for the desired installation (manually or using Gradle).

The sample linked above assumes that Leanplum and other dependencies are all installed using Gradle. For example, your app module build.gradle file dependencies might look like this:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile('com.google.android.gms:play-services-gcm:9.0.0+')
    compile 'com.google.android.gms:play-services-location:10.0.1'
    compile 'com.leanplum:Leanplum:+'
}

In this case, we are including the Google Play Services components we need (play-services-gcm for Push Notifications and play-services-location for Geolocation) and Leanplum latest version available in the Maven repository.

There are few different ways that Leanplum can be instrumented into an Android project (as also detailed in the docs), based on the different needs. However, the recommended way (and typically the one easier to be adopted) is explained as follow (and reflected in the proposed sample):

  1. Put the Leanplum registration and instrumentation in the Application class. This includes the registration keys and Leanplum.start().
  2. Application class (and other Activity classes) do not necessarily need to be extending a Leanplum class.

As, shown in the sample, basically the only thing needed for Leanplum are the following lines in the Application class (inside the onCreate() method):

@Override
public void onCreate() {
 Leanplum.setApplicationContext(this);
 Parser.parseVariables(this);
 LeanplumActivityHelper.enableLifecycleCallbacks(this);
 super.onCreate();

}

Push notification registration (Android)

On Android, you need to send us your sender ID before calling Leanplum.start. Leanplum will automatically register the device for notifications.

Make sure every required permission and service is added to the AndroidManifest.xml, listed in the Android setup page.

When starting Leanplum, the required code for registering for Push is:

LeanplumPushService.setGcmSenderId(YOUR_SENDER_ID);

You may already have a sender ID that you are using for other push notification providers, or perhaps from your own server. If you do not have a sender ID, you have two options:

  1. Use the built-in sender ID (easiest): Simply use LeanplumPushService.LEANPLUM_SENDER_ID, and you're done.

  2. Create your own sender ID. You might want to do this if you want to send push notifications outside of Leanplum. Your sender ID is your Google Developer project number (is a 12 digits number).

If you're using your own sender ID, you need to give Leanplum permission to send notifications on your behalf. Simply paste your Google API key (this is different from your sender ID!) in your app's Push Notification settings.

Note: Google requires now to use the Server API key, while the Android API key would no longer be valid and returning "Unauthorized - Error 401". When generating the API key from the Google Cloud Platform website, choose Server instead of Android:

  • In your project Google Cloud Platform, click on Credentials > Create credentials > API key
760
  • Click 'Server' . Then copy the Server Key value.
842
  • Now paste the Server key value in the 'Firebase Cloud Messaging' field on Leanplum Dashboard
1441

Please note that in order to register correctly to Android Push Notifications, you need to use Google Play Services (at least version 8.3.0+, see the Gradle file configuration above) and include the version in the Android Manifest, under the Application tag:

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

See the sample app structure and files (AndroidManifest.xml and Application class) as a reference.

Unity

Sample project (Unity)

Sample project: here.

Installation (Unity)

For Unity, follow the instructions for the SDK Setup here.

Push notification registration (Unity)

For registering for Unity Push Notifications, see the Unity push notifications docs.

For Android, you can put the AndroidManifest.xml file in the Android Plugin folder and edit it from there. All the permissions and services required for a common Android project need to also be placed here, as well as the Google Play Services.

Current Google Play Services version included in the latest Unity package is 8487000. So, if using the Google Play Service included in the package, in the Application tag, the following could be put:

<meta-data
 android:name="com.google.android.gms.version"
 android:value="8487000" />