Integrate the Chat SDK into your application to enable chat functionalities.
The Mfereji Chat-SDK allows you to easily integrate chat functionality into your Flutter mobile applications.
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
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());
}
To list your chat channels, use the following code:
chatApiToken: 'your_chat_api_token',
appId: 'your_app_id',
fcmToken: 'your_fcm_token'
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),
),
);
}
}
To enable Firebase Cloud Messaging in your application, follow these steps:
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
});
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
}