FIREBASE SETUP

Add Firebase to Your Flutter Project

1. Register Your App in Firebase

1. Go to the Firebase Console and create a new project.
2. Register your app:
  • For Android, provide the package name (e.g., com.example.app).
  • For iOS, provide the iOS bundle ID.
3. Download the configuration files:
  • Android: Download google-services.json and place it in android/app/.
  • iOS: Download GoogleService-Info.plist and place it in ios/Runner/.

2. Add Firebase Plugins to Your Flutter Project

Add the following dependencies to your pubspec.yaml file:
Copy code
dependencies:
firebase_core: ^2.15.0 # Latest version
firebase_messaging: ^14.6.0 # Latest version
Run
Copy code
flutter pub get

3. Configure Firebase for Android

1. Update android/build.gradle:
Copy code
buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.3.15'
}
}
2. Update android/app/build.gradle:
Copy code
apply plugin: 'com.google.gms.google-services'
3. Update AndroidManifest.xml: Add the required permissions for FCM:
Copy code
uses-permission android:name="android.permission.INTERNET"/
uses-permission android:name="android.permission.WAKE_LOCK"/
uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/
uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/

4. Configure Firebase for iOS

1. Enable Push Notifications in Xcode:
  • Open your project in Xcode (ios/Runner.xcworkspace).
  • Navigate to Signing & Capabilities > + Capability and add Push Notifications.
2. Update ios/Podfile: Ensure the platform is set to at least iOS 11.0:
Copy code
platform :ios, '11.0'
3. Set Up APNs:
  • Go to the Apple Developer Console and create an APNs key.
  • Upload the key to Firebase under Project Settings > Cloud Messaging > iOS Configuration.

5. Initialize Firebase in Flutter

Initialize Firebase in your app by editing the main.dart file:
Copy code
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}

6. Set Up Firebase Messaging

1. Request Permissions
Request notification permissions from the user: import 'package:firebase_messaging/fireba
Copy code
import 'package:firebase_messaging/firebase_messaging.dart';
Future void requestNotificationPermissions() async {
FirebaseMessaging messaging = FirebaseMessaging.instance;
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
print('User granted permission');
} else {
print('User declined or has not accepted permission');
}
}
Call this method in your app's initialization:
Copy code
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await requestNotificationPermissions();
runApp(MyApp());
}

6.2 Handle Notifications

1. Get FCM Token:
Copy code
FirebaseMessaging messaging = FirebaseMessaging.instance;
void getToken() async {
String? token = await messaging.getToken();
print('FCM Token: $token');
}
2. Foreground Message Handling: Handle notifications when the app is in the foreground:
Copy code
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Message received: ${message.notification?.title}');
});
3. Background Message Handling: Define a background message handler:
Copy code
Future void _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print('Background message received: ${message.messageId}');
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage
(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}

6.3 Notification Click Handling

Handle user interactions with notifications:
Copy code
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('Notification clicked: ${message.notification?.title}');
});

7. Test Firebase Notifications

1. Go to Firebase Console > Cloud Messaging > Send Your First Message.
2. Enter a title and message.
3. Target your app by the FCM Token or send it to all users.
4. Click Send.