@blumsoft/inbrain-cordova
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

inBrainCordova Cordova Plugin

Use this plugin to integrate native inBrain SDKs in your iOS and Android Cordova apps. In addition to Javascript, the plugin also provides a TypeScript type definitions.

Compatibility

The plugin is compatible with Cordova 8+ / cordova-ios 4.5+ / cordova-android 8.0+ / iOS 10+ / Android 5+.

Preparation

Installing plugin

  1. Clone the plugin into a folder beside your Cordova app from https://github.com/inbrainai/inbrainCordova.
  2. Goto your Cordova app's root folder and issue the command

 ionic cordova plugin add ../inbrainCordova

Setting up your ionic project

To use the plugin, import the followings.

import InBrainCordova from "inbrain-cordova";
import * as InBrain from "inbrain-cordova";

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
...

InBrainCordova

InBrainCordova class represents the wrapper of the InBrain SDK for Cordova. We have the following methods

init

Initializes the SDK. Use this method after platform ready.

InBrainCordova.init(
        <<API_CLIENT_ID>>,
        <<API_SECRET>>,
        <<S2S>>, 
        <<USER_ID>>,
        <<SESSION_ID>>,
        <<InitOptions>>
      ): Promise<void>

where the parameters are the following:

API_CLIENT_ID:
The apiCientID provided in inBrain.ai dashboard

API_SECRET:
The apiSecret provided in inBrain.ai dashboard

S2S:
Bool value. true if it is a server-to-server communication.

USER_ID:
User ID

SESSION_ID:
Session ID.


InitOptions:

type ConfigOptionName = "sessionID" | "userID" | "dataOptions";
type StylingOptionName = "title" | "navbarColor";
type InitOptionName = ConfigOptionName | StylingOptionName;
type ConfigOptionTypes = {
    sessionID: string;
    userID: string;
    dataOptions: DataOptions;
};
type ConfigOptions = {
    [opt in ConfigOptionName]?: ConfigOptionTypes[opt];
};
type StylingOptionType = {
    title: string;
    navbarColor: string;
};
type StylingOptions = {
    [opt in StylingOptionName]?: StylingOptionType[opt];
};
type InitOptions = ConfigOptions & StylingOptions;

Example:

InBrainCordova.init(
        "c0bbffc5-3c2b-44e7-a89e-f720c1a5867f",
        "5iwiMGX3nWBLtNFHDinib7OfHb1mLUVII9x6Q+5bCLT+CMZZ9YbN9MWdywT/rfGFkmvRV+EwD2ltTAFzGGx1lQ==",
        false,
        "testing@test.com",
        "testing@test.com",
        {
          dataOptions: data,
          title: "Test title",
          navbarColor: "#FF00FF"
        }
      ).then(() => {
        console.log("Init success...");
        // ...
      })
      .catch((err: InBrain.InBrainError) => {
        console.log("Error happened in init: " + JSON.stringify(err))
      })

Fields for init options:

title: string;
The title of the Surveys' nav bar.

navbarColor: string;
The color of the nav bar in hexcode string.

dataOptions: DataOptions;
Array of key-value pairs of strings to provide InBrain profiler data for custom profiler user experience.
e.g.
{ age : “23”, gender : “female” }

showSurveys

Shows surveys.

InBrainCordova.showSurveys(): Promise<void>

getRewards

Get the details of all the rewards, in case the server mode is YES.

InBrainCordova.getRewards(): Promise<InBrainReward[]>

Where InBrainReward is

type InBrainReward = {
    transactionId: number;
    amount: number;
    currency: string;
    transactionType: number;
};

Callback functions

The survey SDK can trigger some events that we can get through callbacks. To take effect, the callbacks needs to set by the following method.

setListeners

Sets the callbacks.

static setListeners(callbacks: InBrainCallbacks): Promise<void>;

Where InBrainCallbacks is defined like

type InBrainCallbacks = {
    surveyClosed: () => void,
    rewardsReceived: (rewards: InBrainReward[]) => void
};

surveyClosed: Triggered when the survey is closed

rewardsReceived: Triggered when new rewards are received.

Example:

InBrainCordova.setListeners({
            surveyClosed : () => {
              console.log("inBrain form is closed")
            },
            rewardsReceived: (rewards: [InBrain.InBrainReward]) => {
              console.log("inBrain receive a reward: " + JSON.stringify(rewards));
              this.zone.run(() => {
                this.numTokens = this.getTokens(rewards);
              })
            }
          })
          .then(() => {
            console.log("Callback setListeners successfully set");
          })
          .catch((err: InBrain.InBrainError) => {
            console.log("setListeners error: " + JSON.stringify(err));
          });

Readme

Keywords

Package Sidebar

Install

npm i @blumsoft/inbrain-cordova

Weekly Downloads

1

Version

1.0.0

License

MIT License

Unpacked Size

4.21 MB

Total Files

98

Last publish

Collaborators

  • laszloblum