Integrating Chat SDK

Integrate the Chat SDK into your application to enable chat functionalities.

Try Our Demo App

Experience the Mfereji SDK in action by visiting our demo app.

Open Demo App

Step-by-Step Guide

  • Install SDK: Follow the instructions to install the SDK for your platform.
  • Initialize SDK: Use the provided code samples to initialize the SDK.

Flutter Integration Guide

The Mfereji Chat-SDK allows you to easily integrate chat functionality into your Flutter mobile applications.

Prerequisites

  • An authentication chat-api token for user authentication (chatApiToken) - Get from your backend after successfully creating an organization.
  • An application ID (appId), typically obtained from the mfereji.com dashboard.
  • An FCM token from Firebase FCM for your application (fcmToken) for push notification support.

Add Flutter Dependency

Add chatsasa_chat_sdk to your package's pubspec.yaml file, using the latest version:

dependencies:
  chatsasa_chat_sdk: ^latest_version

You should then run flutter packages get

Initialization

Initialize the Mfereji SDK in your main.dart file:

import 'package:flutter/material.dart';
import 'package:chatsasa_chat_sdk/chatsasa_chat_sdk.dart';

Future main() async {
  // initialize the sdk
  WidgetsFlutterBinding.ensureInitialized();
  await ChatSasaChatSDK().initSDK("company_name", "logo_name.extension"); 
  runApp(const MyApp());
}

Usage

To list your chat channels, use the following code:

chatApiToken: 'your_chat_api_token',
appId: 'your_app_id',
fcmToken: 'your_fcm_token'

Example

import 'package:chatsasa_chat_sdk/export.dart';
import 'package:chatsasa_chat_sdk/chatsasa_chat_sdk.dart';
import 'package:flutter/material.dart';

Future main() async {
  // initialize the sdk
  WidgetsFlutterBinding.ensureInitialized();
  await ChatSasaChatSDK().initSDK("company_name", "logo_name.extension"); 
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Chat-SDK Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('Mfereji Chat-SDK Demo'),
      ),
      body: const Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Mfereji Demo',
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          //Launch Mfereji Chat Channels
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (_) => const ChatChannels(
                      chatApiToken: '"Your chat-api-token"
                      }',
                      appId: 'your_app_id',
                      fcmToken: 'Your fcm_token')));
        },
        child: const Icon(Icons.message),
      ),
    );
  }
}

Add FCM Push Messaging

To enable Firebase Cloud Messaging in your application, follow these steps:

Foreground Notifications

To enable FCM notifications when your app is in the foreground, modify the FirebaseMessaging.onMessage.listen() method as below:

var fcmApiClient = await ChatSasaChatSDK().initFCM();
FirebaseMessaging.onMessage.listen((message) {
  // Add_this_code

  ChatSasaChatSDK().handleFCMEvents(message.data);
  var notificationMessage = ChatSasaChatSDK().getForegroundFCMNotification(fcmApiClient, message.data);
  if (notificationMessage == null) {
    return;
  }
  // show LocalNotificationService from flutter_local_notifications package you can use other notification packages of your preference

  LocalNotificationService().showNotificationAndroid(notificationMessage);

  // END
});
Background Notifications

To receive FCM notifications when your app is running in the background, modify the _firebaseMessagingBackgroundHandler method as below:

('vm:entry-point')
Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  // Add_this_code

  var notificationMessage = await ChatSasaChatSDK().getBackgroundFCMNotification(message.data);
  if (notificationMessage == null) {
    return;
  }
  // LocalNotificationService from flutter_local_notifications package you can use other notification packages of your preference

  LocalNotificationService().showNotificationAndroid(notificationMessage);

  // END
}