Different Modes of Operation

Pre-requisite

Please make sure you are familiar with the PulseSdk integration before adding this feature.

Introduction

What is Foreground Mode in Android?

Foreground is a state in which user can interact with the application through android component like Activity or service. Take example of MusicPlayer playing music in foreground service. Also if you have to interact with application through Activity, the activity has to be in foreground. User can't interact with app even if the activity is visible but not in foreground.

SDK Foreground Mode

The Foreground mode allow SDK to serve the campaign to user when the application is in the foreground only. When the app is in background the SDK will not serve campaign to the users.

SDK Background and foreground mode together

The Background mode with foreground mode allow the SDK to serve campaign even when application is in background. The only difference is that app control on how to show the data in foreground mode.

SDK Background Mode

The Background mode is the default mode of SDK and no changes are needed to be done to enable this mode. For sake of readability we added a method to enable background mode.

Integration


The SDK uses LocalBroadcastManager for fast and secure data handling in when passing data to application.

The SDK sends Broadcast to the app to show data to user in any screen of app with two specific PulseEvents as IntentFilter.
1.CAMPAIGN_WEBVIEW_RECEIVED
2.CAMPAIGN_URL_RECEIVED

Initialize the SDK with a Specific mode

In your main application class initialize the SDK and keep a reference to it as shown in the example below:


package com.pulseid.app;

import android.app.Application;

import com.pulseid.sdk.PulseSdk;
import com.pulseid.sdk.PulseSdkConfig;

public class PulseSdkApplication extends Application {
    public static final String TAG = "PulseSdkApplication";

    private static final String APP_KEY = "YOUR_APP_KEY"; // TODO: Replace with your own!
    private static final String APP_URL = "YOUR_APP_URL"; // TODO: Replace with your own!

    private static PulseSdkApplication application;
    private PulseSdk pulseSdk;

    @Override
    public void onCreate() {
        super.onCreate();

        application = this;

        PulseSdkConfig config = new PulseSdkConfig(APP_KEY, APP_URL);
        //To run SDK in foreground mode only
        config.setForegroundModeEnabled();
        //To run SDK in background mode only do nothing but for clarity
        config.setBackgroundModeEnabled();
        //To run SDK in background and foreground mode Both
        config.setBothModesEnabled();
        //Warning Call only one method of the above.
        pulseSdk = new PulseSdk(this, config);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    public static PulseSdkApplication getInstance(){
        return application;
    }

    public PulseSdk getPulseSdk() {
        return pulseSdk;
    }
}

package com.pulse.demo

import android.app.Application

import com.pulseid.sdk.PulseSdk
import com.pulseid.sdk.PulseSdkConfig

class PulseSdkApplication : Application() {

    companion object {
        val TAG = "PulseSdkApplication"

        private val APP_KEY = "YOUR_APP_KEY" // TODO: Replace with your own!
        private val APP_URL = "YOUR_APP_URL" // TODO: Replace with your own!

        lateinit var instance: PulseSdkApplication
            private set
    }

    lateinit var pulseSdk: PulseSdk
        private set

    override fun onCreate() {
        super.onCreate()

        instance = this

        val config = PulseSdkConfig(APP_KEY, APP_URL)
        //To run SDK in foreground mode only
        config.setForegroundModeEnabled()
        //To run SDK in background mode only, do nothing but for clarity
        config.setBackgroundModeEnabled()
        //To run SDK in background and foreground mode Both
        config.setBothModesEnabled()
        //Warning Call only one method of the above.
        pulseSdk = PulseSdk(this, config)
    }
}

Warning

Please do not enable all three modes together in the Application class.

Handling data in foreground mode

Create a BroadcastReceiver to get broadcast of foreground campaign and register it in the app.As shown in the example below:<br/


@Override
protected void onResume() {
    super.onResume();
    CampaignDataReceiver.register(this);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    CampaignDataReceiver.Unregister(this);
}

override fun onResume() {
    super.onResume()
    CampaignDataReceiver.register(this)
}

override fun onDestroy() {
    super.onDestroy()
    CampaignDataReceiver.Unregister(this)
}



public class CampaignDataReceiver {

    private static ControlPanel controlPanel;

    public static void setControlPanel(ControlPanel controlPanel) {
        CampaignDataReceiver.controlPanel = controlPanel;
    }

    private static BroadcastReceiver campaignDataReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null && intent.getAction() != null) {
                String action = intent.getAction();
                if (action.equalsIgnoreCase(PulseEvents.CAMPAIGN_WEBVIEW_RECEIVED)
                        || action.equalsIgnoreCase(PulseEvents.CAMPAIGN_URL_RECEIVED)) {

                    controlPanel.showCustomUi(intent);
                }
            }
        }
    };

    public static void register(Context context) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(PulseEvents.CAMPAIGN_WEBVIEW_RECEIVED);
        filter.addAction(PulseEvents.CAMPAIGN_URL_RECEIVED);

        LocalBroadcastManager
                .getInstance(context.getApplicationContext())
                .registerReceiver(campaignDataReceiver, filter);
    }

    public static void Unregister(Context context) {
        LocalBroadcastManager.getInstance(context).unregisterReceiver(campaignDataReceiver);
    }
}

object CampaignDataReceiver {

    private var controlPanel: ControlPanel? = null


    private val campaignDataReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent?) {
            if (intent != null && intent.action != null) {
                val action = intent.action
                if (action!!.equals(PulseEvents.CAMPAIGN_WEBVIEW_RECEIVED, ignoreCase = true) || action.equals(PulseEvents.CAMPAIGN_URL_RECEIVED, ignoreCase = true)) {

                    controlPanel!!.showCustomUi(intent)
                }
            }
        }
    }

    fun setControlPanel(controlPanel: ControlPanel) {
        CampaignDataReceiver.controlPanel = controlPanel
    }

    fun register(context: Context) {
        val filter = IntentFilter()
        filter.addAction(PulseEvents.CAMPAIGN_WEBVIEW_RECEIVED)
        filter.addAction(PulseEvents.CAMPAIGN_URL_RECEIVED)

        LocalBroadcastManager
                .getInstance(context.applicationContext)
                .registerReceiver(campaignDataReceiver, filter)
    }

    fun Unregister(context: Context) {
        LocalBroadcastManager.getInstance(context).unregisterReceiver(campaignDataReceiver)
    }
}