App Setup: 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 setup Push Notifications. 

iOS

Sample project 

Project sample: Click here

Installation

For installing Leanplum follow the steps in the SDK Setup doc here: https://www.leanplum.com/docs#/setup , following the instructions for the desired technology (ObjC or Swift). 

Once the project is ready and Leanplum in 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

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

Now, for registering for Push Notifications,  we have basically 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 reply, the 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. 

  2. Register for Push using the Soft Ask Push Permissions in-app message

    In this case, there is no need of adding any Push registration code in the app. Simply setup in the Dashboard a new Push Ask-to-Ask message to be displayed when desired and the user will have to choose first whether is interested proceeding to register for Push or would like to still think about it.

    If proceeding in registering 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 the Soft Ask Push Permissions message to be displayed again (based on the targets or 'display when' settings in the Leanplum Dashboard).  

Android

Sample project

Access to the sample project: Click here

Installation

For installing Leanplum follow the steps in the SDK Setup doc here: https://www.leanplum.com/docs#/setup/android , following the instructions for the desired installation (manually or using Gradle). 

The sample linked above is assuming that Leanplum and other dependencies are all installed using Gradle. For example, your app module build.gradle file dependecies 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:8.3.0+')
compile('com.google.android.gms:play-services-location:8.3.0+')
compile 'com.leanplum:Leanplum:+'
}

Where 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

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 SDK 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:
    1. In your project Google Cloud Platform, click on Credentials > Create credentials > API key 
      push notice android required element
    2. Click on 'Server' . Then copy the Server Key value.
    3. Now paste the Server key value in the 'Google Cloud Messaging' field on Leanplum Dashboard 


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

Sample project: here

Installation

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

Push Notification registration

For registering for Unity Push Notifications, see also this article https://support.leanplum.com/hc/en-us/articles/209085146-Messaging-Push-Notifications

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" />


Was this article helpful?
Have more questions? Submit a request