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 walks you through setting up push notifications with the LikeMinds Flutter Chat SDK.

Refer to Notification List for the complete list of notifications.

Prerequisites

Before proceeding, ensure you have:

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

Implementation

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


Step 1: Set up Firebase in your Flutter project

  • Follow the official Firebase Flutter setup guide.
  • Add Firebase configuration files (google-services.json for Android, GoogleService-Info.plist for iOS).
  • Initialize Firebase in your Flutter app.

Step 2: Set up notifications in your Flutter project

1. Initialize Notifications (common for Android & iOS)

Add this boilerplate code in your Flutter app:

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. Configure iOS-specific setup (required)

Modify your ios/Runner/AppDelegate.swift file to configure notifications for iOS:

Final code should look like this:

import UIKit
import Flutter
import flutter_local_notifications

@main
@objc class AppDelegate: FlutterAppDelegate {

override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Required to make notifications work in background isolates.
FlutterLocalNotificationsPlugin.setPluginRegistrantCallback { (registry) in
GeneratedPluginRegistrant.register(with: registry)
}

if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
}

GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}

3. Get the device ID

Use device_info_plus to fetch the device ID:

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();
}

4. Set up Firebase Messaging

Use firebase_messaging to manage the FCM token:

import 'package:firebase_messaging/firebase_messaging.dart';

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

5. Initialize Notifications in main()

Invoke setupNotifications() before your app runs:

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

Handling Notifications

The LikeMinds Flutter Chat SDK provides built-in handlers:

  • Background notifications:
await LMChatNotificationHandler.instance
.handleBackgroundNotification(RemoteMessage message);
  • Foreground notifications:
await LMChatNotificationHandler.instance.handleNotification(
RemoteMessage message,
bool show,
GlobalKey<ScaffoldMessengerState> rootNavigatorKey,
);

Detailed implementation examples available here.


Now, your Flutter application should successfully handle push notifications across both Android and iOS platforms.