Flutter Firebase Cloud Messaging (FCM) | Send & Handle Push Notifications
### **Flutter Firebase Cloud Messaging (FCM) | Send & Handle Push Notifications π** **Description:** Want to implement **push notifications** in your **Flutter app** using **Firebase Cloud Messaging (FCM)**? π© In this step-by-step tutorial, I'll show you how to **send and handle push notifications** in your Flutter app using Firebase! π₯ By the end of this tutorial, youβll be able to: β **Set up Firebase Cloud Messaging (FCM) in Flutter** β **Send push notifications to Flutter apps** β **Handle foreground and background notifications** β **Use Firebase API to trigger notifications** β **Fix common FCM issues in Flutter** --- ## **1οΈβ£ Prerequisites for FCM in Flutter** β **Flutter Installed** - [Download Flutter](https://flutter.dev/docs/get-started/install) β **Android Studio or VS Code** (for Flutter development) β **A Firebase account** - [Create an account](https://firebase.google.com/) β **A Flutter Project** (`flutter create my_app` if you donβt have one) β **FlutterFire CLI Installed** --- ## **2οΈβ£ Set Up Firebase for Cloud Messaging in Flutter** ### β **Step 1: Create a Firebase Project** 1. Go to [Firebase Console](https://console.firebase.google.com/) 2. Click **"Create a Project"**, enter your project name, and continue 3. Enable **Google Analytics** (optional) and click **Create Project** --- ### β **Step 2: Enable Firebase Cloud Messaging (FCM)** 1. In **Firebase Console**, go to **Cloud Messaging** 2. Click on **Get Started** 3. Enable **Push Notifications** --- ### β **Step 3: Add Firebase to Your Flutter App** 1. Click on **Android** (for Android apps) or **iOS** (for iOS apps) 2. Download and add the `google-services.json` (for Android) or `GoogleService-Info.plist` (for iOS) to your project --- ## **3οΈβ£ Install Dependencies in Flutter** Run the following command in your Flutter project: ```bash flutter pub add firebase_core firebase_messaging ``` Then, update dependencies: ```bash flutter pub get ``` --- ## **4οΈβ£ Initialize Firebase in Your App** Open `main.dart` and initialize Firebase: ```dart import 'package:flutter/material.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: PushNotificationScreen(), ); } } ``` --- ## **5οΈβ£ Handle Firebase Push Notifications in Flutter** ### β **Step 1: Request Notification Permissions** ### β **Step 2: Implement Notification Handling in UI** ## **6οΈβ£ Sending Push Notifications from Firebase Console** 1. Go to **Firebase Console** β **Cloud Messaging** 2. Click **New Notification** 3. Enter a **Title** and **Body** 4. Select **Target App** 5. Click **Send Message** Your Flutter app should **receive the notification instantly!** π --- ## **7οΈβ£ Sending Notifications Using Firebase API** You can also send notifications via Firebase API: ### β **Step 1: Get Your Firebase Server Key** 1. Go to **Firebase Console** β **Project Settings** 2. Click on **Cloud Messaging** 3. Copy the **Server Key** --- ### β **Step 2: Send a Push Notification Using Postman or Curl** Use this API to send push notifications: Replace `YOUR_SERVER_KEY` with your actual **Firebase Server Key**. --- ## **8οΈβ£ Common Firebase Cloud Messaging Issues & Fixes** π¨ **Push notifications not working in background?** β Make sure to enable **background modes** in your app settings. π¨ **FCM message not received on iOS?** β Ensure youβve enabled **APNs** in **Apple Developer Portal**. π¨ **No permission dialog on Android?** β Call `requestPermission()` before subscribing to notifications. --- ## **π Conclusion** π **Congratulations!** You have successfully integrated **Firebase Cloud Messaging (FCM)** into your **Flutter app**! π Now, your app can receive **push notifications** and keep users engaged. If you found this tutorial helpful, **LIKE** π the video, **SUBSCRIBE** π for more Flutter tutorials, and **SHARE** with fellow developers! --- ## **π Useful Links:** π Firebase Console: [https://console.firebase.google.com/](https://console.firebase.google.com/) π FlutterFire Docs: [https://firebase.flutter.dev/](https://firebase.flutter.dev/) π FCM Documentation: [https://firebase.google.com/docs/cloud-messaging](https://firebase.google.com/docs/cloud-messaging) π **Hashtags:** #Flutter #Firebase #FCM #PushNotifications #CloudMessaging #FirebaseMessaging #FlutterFirebase #Dart #MobileAppDevelopment #FlutterTutorial
Download
0 formatsNo download links available.