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 Feed 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:
- A Flutter project set up and running.
- The LikeMinds Flutter Feed SDK installed and initialized in your project.
- A Firebase project created for your application.
- FCM Service Account Keys: Obtain the Firebase Cloud Messaging service account keys from the Firebase Console. Here is the guide to obtain 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 Feed SDK requires the deviceId
, and fcmToken
to register the device for notifications.
Step 1: Set up Firebase in your Flutter project
- Add the Firebase SDK to your Flutter project by following the official Firebase Flutter setup guide.
- Obtain the Firebase configuration files (
google-services.json
for Android andGoogleService-Info.plist
for iOS) and add them to your project. - 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 LMNotificationHandler
. 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 LMNotificationHandler
LMNotificationHandler.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();
if(!kIsWeb){
setupNotifications();
}
runApp(const MyApp());
}
Handling Notifications
The LikeMinds Flutter Feed SDK provides methods to handle notifications in different app states:
- Use
handleNotification
for notifications received in any state (foreground, background, or terminated):
await LMNotificationHandler.instance.handleNotification(
RemoteMessage message,
bool show,
GlobalKey<NavigatorState> navigatorKey,
);
The show
parameter determines whether to display a visible notification (true for foreground, false for background/terminated).
For more detailed information on implementation, refer to the code examples in main.dart
and notification_handler.dart
provided in the SDK code here.