Chabok Push Client for Flutter

pub package

Flutter wrapper for chabok library. This client library supports Flutter to use chabok push library. A Wrapper around native library to use chabok functionalities in Flutter environment.

Installation

For installation refer to Flutter docs and platform specific parts (Android and iOS).

Release Note

You can find release note here.

Support

Please visit Issues.

Getting Started

Android

Add the dependency to your app build.gradle file.

plugins {
    id 'com.google.gms.google-services'
}

...

dependencies {
    implementation 'io.chabok:chabok-sdk:1.3.2'
}
Firebase Cloud Messaging

Firebase Cloud Messaging (FCM), formerly known as Google Cloud Messaging (GCM), is a cross-platform cloud solution for messages and notifications for Android.

You need to include the following code in your build.gradle file to get Firebase Cloud Messaging API support.

dependencies {
    implementation 'com.google.firebase:firebase-messaging: 23.0.0'
}
buildscript {
    dependencies {
        classpath "com.google.gms:google-services:4.3.2"
    }
}

Initialization

Assigning permissions.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

The AndroidManifest.xml file should include the following permission for apps targeting API level 31 (Android 12):

<uses-permission android:name="com.google.android.gms.permission.AD_ID" />

The AndroidManifest.xml file should include the following permission for apps targeting API level 33 (Android 13):

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

Initialize Chabok SDK in your MainApplication file:

Note: Calling Chabok.initialize() is required for using Chabok SDK.

import io.chabok.Callback;
import io.chabok.Chabok;
import io.flutter.app.FlutterApplication;

public class MainApplication extends FlutterApplication {
    @Override
    public void onCreate() {
        super.onCreate();

        Chabok.setActivity(MainActivity.class);

        Chabok.initialize("APP_ID", "SECRET", new Callback() {
            @Override
            public void onResponse(boolean success, @Nullable String message) {
                if(success) {
                    //Initialized.
                } else {
                    //Not Initialized.
                }
            }
        });
    }
}
import io.chabok.Chabok  
import io.chabok.Callback  
import io.flutter.app.FlutterApplication;
  
class MainApplication : FlutterApplication() {  
  
    override fun onCreate() {  
        super.onCreate()  

        Chabok.setActivity(HomeActivity::class.java)
  
        Chabok.initialize("APP_ID", "SECRET", object : Callback {  
            override fun onResponse(success: Boolean, message: String?) {  
                if (success) {  
                    print("The Chabok SDK has been successfully initialized")  
                } else {  
                    print("$message")  
                }  
            }  
        })  
    }  
}

Note: The application credentials (APP_ID and SECRET) are available in your dashboard space under app info.

Place your APP_ID and SECRET from your dashboard into the initialize method.

iOS

Initialize Chabok SDK in your MainApplication file:

Note: Calling Chabok.initialize() is required for using Chabok SDK.

#import <ChabokSDK/ChabokSDK-Swift.h>

[Chabok initializeWithAppId:@"APP_ID" secret:@"SECRET" callback:^(BOOL success,NSString* message) {
    if (success) {
      NSLog(@"The Chabok SDK has been successfully initialized");
    } else {
      NSLog(@"%@", message);
    }
}];
import ChabokSDK

Chabok.initialize(appId: "APP_ID", secret: "SECRET") {
    (success,message) in
    if (success) {  
      print("The Chabok SDK has been successfully initialized")  
    } else {  
      print("\(data)") 
    }
}

Usage

In your main.dart:

Initialize

For initializing the Chabok SDK in dart add bellow lines in import section:

import 'package:chabok_flutter/Chabok.dart';
import 'package:chabok_flutter/user/Profile.dart';
import 'package:chabok_flutter/user/Gender.dart';

User

Chabok USERNAME is a unique ID that can be assigned to each user to identify him/her. For example, a unique ID could be a generated UUID, a mobile number, etc.

Ideally, you should assign the unique ID to users when signing up, logging in, or on pages where their identity is known.

Login

After initializing Chabok, use the login method to identify your users in the system to monitor all behaviors and attributes with user identity. We recommend that you to use Chabok's login on otp pages.

When a user logs in, all the stored information is associated with the identified user.

To login user in the Chabok service use login method:

Chabok.user().login("USERNAME");

loginUser method with callback:

Chabok.user().login("USERNAME").then((response) {
    setState(() {
      Map<String, dynamic> responseJson = json.decode(response);
      final String message = responseJson['message'];
      final bool success = responseJson['success'];
      if(success) {
        print('(CHABOK): Flutter ~~~~~~ logged in suucessfully');
      } else {
        print('(CHABOK): Flutter ~~~~~~ error in login $message');
      }
    });
});
Example

When verifying user OTP codes, we should login to the Chabok platform to identify user by user ID

Chabok.user().login("989100360500");

Logout

By calling the following method, even if the user is logged out of his/her account, you can still have the user in your system with a guest ID and interact with the user as usual.

When the user logs out of your app, call the Chabok Logout method to avoid attaching future attributes, events, and other data to this user until the login method is called again.

logout method can be used to log out a user from Chabok:

Chabok.user().logout();

logout method with callback:

Chabok.user().logout().then((response) {
    setState(() {
      Map<String, dynamic> responseJson = json.decode(response);
      final String message = responseJson['message'];
      final bool success = responseJson['success'];
      if(success) {
        print('(CHABOK): Flutter ~~~~~~ logged out suucessfully');
      } else {
        print('(CHABOK): Flutter ~~~~~~ error in logout $message');
      }
    });
});

Check user is logged in:

To check a user is logged in Chabok you can use the following method.

Chabok.user().isLoggedIn((result) {
      if(result) {
        print("User is loggedIn");
      } else {
        print("User is not loggedIn");
      }
});

Tip: In case you have implemented Chabok in your application, you can use the following method to check and login users who have already logged into your system but not into Chabok.

Chabok.user().isLoggedIn((result) {
      if(!result) {
        Chabok.loginUser("USER_ID")
      }
});

Tag

To set user tag in the Chabok service use setTag method:

Chabok.user().setTag("TAG");

To set array of tags in the Chabok service use setTags method:

Chabok.user().setTags(List<String>);
Example
Chabok.user().setTags("VIP");
List<String> tags = ["VIP", "PURCHASED"];
Chabok.user().setTags(tags);

Set user's tag method with callback:

Chabok.user().setTag(tag.text.toString()).then((response) {
    setState(() {
      Map<String, dynamic> responseJson = json.decode(response);
      final String message = responseJson['message'];
      final bool success = responseJson['success'];
      if(success) {
        print('(CHABOK): Flutter ~~~~~~ set user tag suucessfully');
      } else {
        print('(CHABOK): Flutter ~~~~~~ error in set user tag $message');
      }
    });
});

To unset user tag in the Chabok service use unsetTag method:

Chabok.user().unsetTag("TAG");

To unset array of tags in the Chabok service use unsetTags method:

Chabok.user().unsetTags(List<String>);
Example
Chabok.user().unsetTag("VIP");
List<String> tags = ["VIP", "PURCHASED"];
Chabok.user().unsetTags(tags);

Unset user's tag method with callback:

Chabok.user().unsetTag(tag.text.toString()).then((response) {
    setState(() {
      Map<String, dynamic> responseJson = json.decode(response);
      final String message = responseJson['message'];
      final bool success = responseJson['success'];
      if(success) {
        print('(CHABOK): Flutter ~~~~~~ unset user tag suucessfully');
      } else {
        print('(CHABOK): Flutter ~~~~~~ error in unset user tag $message');
      }
    });
});

Attributes

The user attributes you collect can give you a comprehensive picture of who your users are, where they're from, what they do, and a whole lot more, depending on your business. An attribute is something like favorites, user preferences, or etc. You can segment users based on their contextual relevance and personalize marketing campaigns sent through all channels of engagement with such granular user data.

To set user attributes in the Chabok service use setAttribute method:

Chabok.user().setAttribute("KEY","VALUE");
Example
Chabok.user().setAttribute("City","Karaj");

Set user's attributes method with callback:

Chabok.user().setAttribute(attributeKey.text.toString(), attributeValue.text.toString()).then((response) {
    setState(() {
      Map<String, dynamic> responseJson = json.decode(response);
      final String message = responseJson['message'];
      final bool success = responseJson['success'];
      if(success) {
        print('(CHABOK): Flutter ~~~~~~ set user attribute suucessfully');
      } else {
        print('(CHABOK): Flutter ~~~~~~ error in set user attribute $message');
      }
    });
});

To unset user attributes in the Chabok service use unsetAttribute method:

Chabok.user().unsetAttribute("KEY");
Example
Chabok.user().unsetAttribute("City");

Unset user's attributes method with callback:

 Chabok.user().unsetAttribute(attributeKey.text.toString()).then((response) {
    setState(() {
      Map<String, dynamic> responseJson = json.decode(response);
      final String message = responseJson['message'];
      final bool success = responseJson['success'];
      if(success) {
        print('(CHABOK): Flutter ~~~~~~ unset user attribute suucessfully');
      } else {
        print('(CHABOK): Flutter ~~~~~~ error in unset user attribute $message');
      }
    });
});

Profile

Use the setProfile method to enter user information such as first name, last name, gender, etc.

To set user's profile information in the Chabok service use setProfile method:

final profile = Profile();
    profile.email = "EMAIL"; //e.g. dev.chabok@gmail.com
    profile.phoneNumber ="PHONE_NUMBER"; //e.g. 989100360500
    profile.firstName = "FIRSTNAME"; //e.g. Hossein
    profile.lastName = "LASTNAME"; //e.g. Shooshtari
    profile.birthDate = "BIRTH_DATE"; //e.g. (timestamp) 3131231232 
    profile.gender = Gender; //e.g. Gender.MALE
    Chabok.user().setProfile(profile);

Set user's profile information method with callback:

final profile = Profile();
    profile.email = "EMAIL"; //e.g. dev.chabok@gmail.com
    profile.phoneNumber ="PHONE_NUMBER"; //e.g. 989100360500
    profile.firstName = "FIRSTNAME"; //e.g. Hossein
    profile.lastName = "LASTNAME"; //e.g. Shooshtari
    profile.birthDate = "BIRTH_DATE"; //e.g. (timestamp) 3131231232 
    profile.gender = Gender; //e.g. Gender.MALE
    Chabok.user().setProfile(profile).then((response) {
      setState(() {
         Map<String, dynamic> responseJson = json.decode(response);
        final String message = responseJson['message'];
        final bool success = responseJson['success'];
        if(success) {
          print('(CHABOK): Flutter ~~~~~~ set user profile suucessfully');
        } else {
          print('(CHABOK): Flutter ~~~~~~ error in set user profile $message');
        }
      });
    });

Notification features

Get passed data in notification payload.

addNotificationHandler() {
    print('(CHABOK): Flutter ~~~~~~ Notification handler called ~~~~~~');
    Chabok.addNotificationHandler((data) {
      print('(CHABOK): Flutter ~~~~~~ New notification data received ~~~> $data');
      Map<String, dynamic> responseJson = json.decode(data);
    });
}

Send notification permission status:

To receive push notifications on Android 13 devices, the user must allow notification permission. Call the following method after getting permission:

Chabok.setNotificationPermissionStatus(false);

Send Chabok intents to SDK:

To get the push notification delivery report for Android 12 devices, call the following method:

Note: Methode should be called in onCreate and onNewIntent.

Chabok.passIntent(intent);

Debugging features

Enable/Disable Chabok SDK:

Chabok.disableSdk(false);

Set log level:

Chabok.setLogLevel(LogLevel.VERBOSE);

Lock SDK's logging:

Chabok.lockLogging(false);