Integrating Appgain SDK in your Flutter app

to integrate the Appgain SDK in your flutter app, you have to use the native SDK libraries; We will use the platform channels method, for more information about running native code in flutter apps, please visit the flutter dev guide

Android Specific Instructions

  • At the very top of your Android project's app/build.gradle, add the following code to the very top of the file:
      implementation 'io.appgain:sdk:4.1.5'
      implementation 'com.google.firebase:firebase-messaging:20.2.0'
      implementation 'com.google.android.gms:play-services-ads-lite:18.1.0'
      implementation 'com.google.android.gms:play-services-location:17.0.0'
      implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
      implementation 'io.reactivex.rxjava2:rxjava:2.2.19'
      implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'
      implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
      implementation 'com.android.support.constraint:constraint-layout:1.1.3'
      implementation 'com.squareup.retrofit2:retrofit:2.3.0'
      implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
      implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
      implementation 'com.google.android.exoplayer:exoplayer:2.7.2'
      implementation 'com.github.bumptech.glide:glide:4.10.0'
      annotationProcessor  'com.github.bumptech.glide:compiler:4.10.0'
      implementation 'com.google.firebase:firebase-messaging:20.1.0'
      implementation "androidx.lifecycle:lifecycle-runtime:2.2.0"
      implementation "androidx.lifecycle:lifecycle-process:2.2.0"
      implementation  'androidx.appcompat:appcompat:1.1.0'  
      implementation  'com.google.android.material:material:1.1.0-beta01'  
      implementation  'androidx.legacy:legacy-support-core-ui:1.0.0'
  • also, make sure that multiDex is enabled:
    android{
        multiDexEnabled = true
    }
  • and at the end of your file add the following line
   apply plugin: 'com.google.gms.google-services'
  • open your project/build.gradle file and add the following line under dependencies
 dependencies {  
    classpath 'com.google.gms:google-services:4.2.0'  
 }

Push Notification setup

  • Open Firebase console, Go to project settings firbase project settings
  • Go to Cloud Messaging tab and copy SenderID and Legacy server key cloud settings
  • Open appgain dashboard
  • Go to APP BACKEND > Settings
  • Navigate to Android push tab
  • enter your SenderID and Server key Appgain Android Push settings

  • Create a new class that extends from AppgainPushReceiver class

public class PushReceiver extends AppgainPushReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    }

    @Override
    protected void onReceive(Context context, ReceiveStatus receiveStatus, Intent intent) {

    }

    @Override
    protected void onSilentPushReceive(@NonNull String operation) {
        super.onSilentPushReceive(operation);
    }
}
  • Open manifest.xml file and add the following lines:
    <receiver
            android:name=".PushReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.parse.push.intent.RECEIVE" />
                <action android:name="com.parse.push.intent.DELETE" />
                <action android:name="com.parse.push.intent.OPEN" />
            </intent-filter>
        </receiver>


        <service
            android:name="io.appgain.sdk.controller.AppgainMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
  • In your android application class, implement LifecycleObserver interface and add the follwing lines
public class MainApplication extends Application implements LifecycleObserver {
    @Override
    public void onCreate() {
        // onCreate method body
        super.onCreate();
        SoLoader.init(this, false);
        initializeFlipper(this);
        // ...
        // add this line of code    
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
    }

    // also add the following two methods
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    void onAppBackgrounded() {
        //App in background
        Appgain.onAppBackgrounded();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    void onAppForegrounded() {
        // App in foreground
        Appgain.onAppForegrounded();
    }
}
  • Then in the MainActivity class, add the following
private val CHANNEL = "com.appgain.sdk.channel"

override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
    super.configureFlutterEngine(flutterEngine)
    MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
            call, result ->
            when (call.method) {
                "matchLink" -> {
                    Appgain.matchLink(object: AppgainDataCallback<DeferredDeepLinkingResponse>{
                        override fun onSuccess(response: DeferredDeepLinkingResponse?) {
                            Log.d("MainActivity", "matchLink: success")
                            if(response?.smartLinkId == null){
                                result.error("error", "cannot find smart link", null)
                            }else{
                                if(!response.extraData?.userId.isNullOrBlank()){
                                    result.success(response.deferredDeepLink)
                                }
                            }
                        }
                        override fun onFailure(failure: BaseResponse?) {
                            result.error("error", failure?.message?:"unknown error", null)
                        }

                    })
                }
                "initSDK" -> {
                    Appgain.initialize(
                        this,
                        "AppgainProjectId",
                        "AppgainApiKey",
                        true,
                        object : AppgainDataCallback<Void> {
                            override fun onFailure(failure: BaseResponse?) {
                                Log.e("MainActivity", "initSDK ERROR: " + failure?.message)
                                result.error(failure?.code?:"0", failure?.message?:"unknown error", null)
                            }
                            override fun onSuccess(data: Void?) {
                                Log.d("MainActivity", "initSDK: success")
                                result.success("success")
                            }
                    })
                }
                // add more methods here
                else -> {
                    result.notImplemented()
                }
            }
        }    
}
  • Then in your dart code, you call it as follows
static const platform = const MethodChannel('com.appgain.sdk.channel');

Future<void> _initSDK() async{
    try {
      return await platform.invokeMethod('initSDK');
    } on PlatformException catch (e) {
    }
    return;
}

Future<String> _match() async{
    var deferredLink = "";
    try {
      deferredLink = await platform.invokeMethod('matchLink');
    } on PlatformException catch (e) {
    }
    return deferredLink;
  }

For more info about all the methods in the Appgain SDK. please, refer to our native guides for android and ios