Skip to main content

Sample Flutter Set up

This section contains helpful links to set up a sample Flutter Android application to integrate push notifications and corresponding notification events.

Complete implementation for reference.

Step 1: Initialization

Set up a client with Flutter for Android.

Step 2: Plugins

Add the following plugins:

  1. Google Analytics
  2. Firebase Cloud Messaging

Step 3: FCM Token

Make code changes to get the FCM token for the developer's device.

  @override
void initState() {
super.initState();
_initializeFirebaseMessaging();
}

/// Initializes Firebase Messaging and retrieves the FCM token.
Future<void> _initializeFirebaseMessaging() async {
FirebaseMessaging messaging = FirebaseMessaging.instance;

await messaging.requestPermission(
alert: true,
badge: true,
sound: true,
provisional: true,
);

// Get the initial FCM token
String? token = await messaging.getToken();
_updateFcmToken(token);

// Handle token refresh
FirebaseMessaging.instance.onTokenRefresh.listen(_updateFcmToken);
}

/// Updates the FCM token and prints it for debugging.
void _updateFcmToken(String? token) {
setState(() => _fcmToken = token);
debugPrint("FCM Token: $_fcmToken");
}