snip-sdk

Smart notifications SDK for Android devices

HOWTO integrate our Android SDK

Javadoc

To review all the available methods exposed by the SDK you could check: Javadoc

Add the Smart Notifications SDK library to your project

  1. Add a new repository source to your project level build.gradle:

     maven {
             url 'http://snip-dev.tid.es/artifactory/snip-sdk-android'
         }
    

    This is an example of the final result:

    buildscript {
     repositories {
         jcenter()
     }
     dependencies {
         classpath 'com.android.tools.build:gradle:2.3.3'
    
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files
     }
    }
    
    allprojects {
     repositories {
         google()
         jcenter()
         maven {
             url 'http://snip-dev.tid.es/artifactory/snip-sdk-android'
         }
     }
    }
    
  2. Edit your app level build.gradle and add the neccessary dependencies:

     implementation 'com.telefonica.snlib:SNlib:1.7.+'
     implementation 'com.telefonica.snlib:SNlibClassifier:1.7.+'
     implementation 'com.telefonica.snlib:SNlibCommons:1.7.+'
     implementation 'com.telefonica.snlib:SNlibSensors:1.7.+'
     implementation 'com.telefonica.snlib:SNlibPushNotifications:1.7.+'
     implementation 'com.google.android.gms:play-services-gcm:11.8.0'
     implementation 'com.google.firebase:firebase-messaging:11.8.0'
     implementation 'com.android.support:support-compat:26.1.0'
     implementation 'com.android.support:support-core-utils:26.1.0'
        
    

    This is an example of the final result:

    apply plugin: 'com.android.application'
    
    android {
       compileSdkVersion 26
       buildToolsVersion "26.0.2"
       defaultConfig {
           applicationId "com.telefonica.snlibintegrationtestapp"
           minSdkVersion 15
           targetSdkVersion 26
           versionCode 1
           versionName "1.0"
           testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
       }
       buildTypes {
           release {
               minifyEnabled false
               proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
           }
       }
    }
    
    dependencies {
       implementation fileTree(dir: 'libs', include: ['*.jar'])
       implementation 'com.android.support:appcompat-v7:26.1.0'
       implementation 'com.telefonica.snlib:SNlib:1.7.+'
       implementation 'com.telefonica.snlib:SNlibClassifier:1.7.+'
       implementation 'com.telefonica.snlib:SNlibCommons:1.7.+'
       implementation 'com.telefonica.snlib:SNlibSensors:1.7.+'
       implementation 'com.telefonica.snlib:SNlibPushNotifications:1.7.+'
       implementation 'com.google.android.gms:play-services-gcm:11.8.0'
       implementation 'com.google.firebase:firebase-messaging:11.8.0'
       implementation 'com.android.support:support-compat:26.1.0'
       implementation 'com.android.support:support-core-utils:26.1.0'
    }
    

Select the icon for your smart notifications

You need to select the small icon that the notifications enqueued through our system are going to show. To do this just add a meta-data tag to your app manifest (inside your <application> tag) with the drawable resource to use:

  <meta-data android:name="smart_notifications_icon" android:resource="@drawable/notification">
  </meta-data>

In the same way you can also choose the large icon to show:

  <meta-data android:name="smart_notifications_large_icon" android:resource="@drawable/notification_large">
  </meta-data>

Launching the SDK

SDK Initialization

Only once the SDK is started, it will begin to process push notifications and user information. Please ensure you have the necessary user consent — if required by the regulation authorities — before starting the SDK.

You start the SDK by running the method static void initialize(Application application, String licenseToken) from your app’s Application class with the Application instance and license token associated to your app package that we sent you.

In case your app doesn’t implement a custom Application class you need to create one. Just add a new class to your project that extends android.app.Application. Example implementation:

import android.app.Application;
import com.telefonica.snlib.SmartNotifLib;

public class SNLibTestApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        SmartNotifLib.initialize(this, "received_license_number");
    }
}

And then set the name in your app’s AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.snlibtestapp">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name=".SNLibTestApplication">
	
	...
	
    </application>
</manifest>

Setting the user ID in the SDK

You need to set in the SDK the unique identifier you use in the app for the user of the app (for example the email, UUID or whatever identifier you use for your users). For that you need to call the method static void setUserId(Context context, String clientUserId):

 SmartNotifLib.setUserId(getApplicationContext(), "unique_user_identifier_for_the_user_of_your_app"); 

The method we use to specify in our push notifications what to do when they are clicked is a URL. If you want that URL to open web content in the phone’s browser you can skip this part, but if you want your push notifications to open specific content in one of your app’s activities you need to configure deep linking in your app.

To do so, you can just follow Google’s tutorial: Create Deep Links to App Content

If your app already has Firebase cloud messaging implemented

If this is your case, you need to add two more methods calls in your implementations of the Firebase services.

  1. In your implementation of the FirebaseMessagingService, inside your onMessageReceived method call:
 
	if (SmartNotifLib.isFirebaseMessageForSNlib(this, remoteMessage)) {
            SmartNotifLib.processFirebaseMessage(this, remoteMessage);
        } else {
            //Process the message in your application as before
        }
  1. In your implementation of the FirebaseInstanceIdService, inside your onTokenRefresh method call:
 SmartNotifLib.updateFirebaseToken(context); 

If you didn’t have Firebase cloud messaging in the app before

You need to follow the instructions here: Integrate Firebase in your Android app so your app is correctly configured to work with Firebase