Skip to main content

Push Notifications

Push notifications are essential for engaging users and keeping them informed about important updates and events within your Flutter application. This guide will walk you through the process of setting up push notifications for use with the LikeMinds Flutter Chat SDK.

Refer to Notification List to see the list of notifications triggered.

Prerequisites

Before proceeding with the integration, make sure you have the following prerequisites in place:

  1. A Flutter project set up and running.
  2. The LikeMinds Flutter Chat SDK installed and initialized in your project.
  3. A Firebase project created for your application.
  4. GCP Service Account File: Obtain the GCP service account file from the Firebase Console. Here is the guide to use them.

Implementation

To set up notifications, you'll need to use some common Flutter libraries to get device information, set up notifications, access tokens, and manage permissions. The LikeMinds Flutter Chat SDK requires the deviceId, and fcmToken to register the device for notifications.

Step 1: Set up Firebase in your Flutter project

  1. Add the Firebase SDK to your Flutter project by following the official Firebase Flutter setup guide.
  2. Obtain the Firebase configuration files (google-services.json for Android and GoogleService-Info.plist for iOS) and add them to your project.
  3. Initialize Firebase in your Flutter app.

Step 2: Set up notifications in your Flutter project

1. Set up notifications

Initialize your Firebase app, and initialise the LMChatNotificationHandler. Here's a boilerplate code example for reference:

void setupNotifications() async {
await Firebase.initializeApp();
final devId = await deviceId();
final fcmToken = await setupMessaging();
if (fcmToken == null) {
debugPrint("FCM token is null or permission declined");
return;
}
// Initialise the LMChatNotificationHandler
LMChatNotificationHandler.instance.init(
deviceId: devId,
fcmToken: fcmToken,
);
}

2. Get the device ID

You can use the device_info_plus plugin to get the device ID. Here's an example:

import 'package:device_info_plus/device_info_plus.dart';

Future<String> deviceId() async {
final deviceInfo = await DeviceInfoPlugin().deviceInfo;
final deviceId = deviceInfo.data["identifierForVendor"]
?? deviceInfo.data["id"];
debugPrint("Device id - $deviceId");
return deviceId.toString();
}

3. Set up Firebase Messaging

Use the firebase_messaging plugin to get the FCM token and initialize the messaging instance:

import 'package:firebase_messaging/firebase_messaging.dart';

Future<String?> setupMessaging() async {
final messaging = FirebaseMessaging.instance;
final settings = await messaging.requestPermission(...);
// Get the token only when permission is granted
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
final token = await messaging.getToken();
return token.toString();
} else {
return null;
}
}

4. Initialize in main()

Call the setupNotifications() function in your main() function before running your Flutter application:

void main() async {
WidgetsFlutterBinding.ensureInitialized();
setupNotifications();
runApp(const MyApp());
}

Handling Notifications

The LikeMinds Flutter Chat SDK provides methods to handle notifications in different app states:

  1. Use handleBackgroundNotification for notifications received when the app is in the background or terminated.
  await LMChatNotificationHandler.instance
.handleBackgroundNotification(RemoteMessage message);
  1. Use handleNotification for notifications received when the app is in the foreground.
   await LMChatNotificationHandler.instance
.handleNotification(
RemoteMessage message,
bool show,
GlobalKey<ScaffoldMessengerState> rootNavigatorKey,
);

For more detailed information on implementation, refer to the code examples in main.dart and notification_handler.dart provided in the SDK code here.