chatsasa_chat_sdk 1.0.10 copy "chatsasa_chat_sdk: ^1.0.10" to clipboard
chatsasa_chat_sdk: ^1.0.10 copied to clipboard

Welcome to the ChatSasa Chat SDK. This SDK allows you to easily integrate ChatSasa's chat functionality into your Flutter mobile applications.

ChatSasa Chat-SDK #

Welcome to the ChatSasa Chat-SDK. This SDK allows you to easily integrate chat functionality into your Flutter mobile applications.

Changelog #

Check out the changelog on pub.dev to see the latest changes in the package.

Getting started #

For you to start using Chatsasa Chat-SDK you need to first create an account on ChatSasa developer dashboard.

Here are the steps to get you started.

  • Create an account in developer.chatsasa.com dashboard.
  • Create an organisation
  • Create an app.
  • Add Users to your App.

API Integration #

  • On your backend application use the provided API endpoints to register and login users.
  • Provide ChatSasa-API token to mobile users.

Add Flutter Dependency #

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

dependencies:
 chatsasa_chat_sdk: ^latest_version

You should then run flutter packages get

Chat Theme and Branding #

Company Name and Logo Icon.

To add your logo icon on Chat Listing page.

Steps:

  • Add your logo under assets folder, create one under the project root folder the folder does not exist.
  • on your pubspec.yaml file enable asset path as below.
  # To add assets to your application, add an assets section, like this:
  assets:
    - assets/
  • Pass your company Name and logo image name to the initChatSDK method.
  await ChatSasaChatSDK().initSDK("company_name", "logo_name.extension"); 

Initialization #

Initialize the ChatSasa SDK in your main.dart file:

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

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

Prerequisites #

  • An authentication chat-api token for user authentication (chatApiToken) - Get from your backend after successfully creating an organization.
  • An application ID (appId), typically got from the developer.chatsasa.com dashboard.
  • An FCM token from Firebase FCM for your application (fcmToken) for push notification support. If you intend to use push notifications for real-time message updates, ensure that you have obtained an FCM token (fcmToken) from Firebase Cloud Messaging for your app. This token is required to enable push notifications within the ChatSasa chat-SDK.

Usage #

To list your chat channels, use the following code:

  • chatApiToken: 'Your chat-api-token',
  • appId: 'Your chat-api application_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<void> main() async {
  // initilize the sdk
  WidgetsFlutterBinding.ensureInitialized();
  await ChatSasaChatSDK().initSDK("company_name", "logo_name.extension"); 
  runApp(const MyApp());
}

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

  @override
  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});

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

Add FCM push messaging #

To enable the Firebase Cloud messaging to your application, follow these steps:

  • Foreground notifications #

To enable FCM notifications when your app is in 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 nofitications #

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

@pragma('vm:entry-point')
Future<void> _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
}

modify your showNotificationAndroid method as below. #

  void showNotificationAndroid(Map<String, dynamic> data) async {
    var title = data['title'];
    var value = data['value'];
    var channelId = data['channelId'];
    var channelName = data['channelName'];
    AndroidNotificationDetails androidNotificationDetails =
        AndroidNotificationDetails(channelId, channelName,
            importance: Importance.max,
            priority: Priority.high,
            ticker: 'ticker');

    int notificationId = 1;
    NotificationDetails notificationDetails =
        NotificationDetails(android: androidNotificationDetails);

    await flutterLocalNotificationsPlugin.show(
        notificationId, title, value, notificationDetails,
        payload: 'Not present');
  }