Firebase Messaging Handler Plugin: Streamlined Notifications, Simplified Setup
Tired of the complexities involved in handling Firebase Cloud Messaging (FCM) notifications? Our Firebase Messaging Handler plugin makes it a breeze!
Key Benefits
- Unified Stream: Get all your notification click callbacks (terminated, background, and foreground) delivered in a single, easy-to-manage stream.
- Optimized Backend Calls: Rest assured that the FCM update-to-backend callback triggers only once, preventing unnecessary API hits.
- User-Friendly Permissions: The plugin intelligently handles notification permission requests, ensuring a smooth user experience.
Installation Steps
-
Add the Dependency: Include the
firebase_messaging_handler
package in yourpubspec.yaml
file dependencies. This plugin takes care of everything, so you don't need separatefirebase_messaging
orflutter_local_notifications
dependencies in your app. -
Android and IOS specific changes
Android
Ensure the following permissions and receivers are added within the <manifest>
section of your android/app/src/main/AndroidManifest.xml
file:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.VIBRATE" />
<application>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_notification" />
<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
</application>
For In-App Messaging just add firebase_in_app_messaging in your yaml.
And enable multidex support to your build.gradle
(android/app/build.gradle):
android {
defaultConfig {
...
multiDexEnabled true
...
}
...
dependencies {
...
implementation "androidx.multidex:multidex:2.0.1"
...
}
IOS
-
Apple Developer Portal Configuration:
- Register your app in Certificates, Identifiers & Profiles.
- Add the Push Notifications capability to your app identifier.
- Generate an APNs key or certificate and upload it to Firebase Console > Project Settings > Cloud Messaging.
-
Firebase Initialization:
- Ensure Firebase is properly initialized in your
AppDelegate
.
- Ensure Firebase is properly initialized in your
-
Notification Permissions:
- Ensure the app requests notification permissions at launch.
-
Capabilities:
- Enable Push Notifications capability in Xcode.
- Add Background Modes capability and check both:
- Background Fetch
- Remote Notifications
3.Firebase Initialization: Complete the standard Firebase initialization process for your Flutter project.
4.Get Your Sender ID: Find your project's Sender ID within your Firebase settings.
5.Crucial Step:
Immediately following Firebase initialization within your app's main
function, add this line:
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); await FirebaseMessagingHandler.instance.checkInitial();
Sample Functions
Purpose: Initializes the Firebase Messaging Handler, establishes notification channels (Android-specific), and sets up a callback to handle updates to your Firebase Cloud Messaging (FCM) token.
Example Usage: (anywhere in the application after Firebase Initialization)
import 'package:firebase_messaging_handler/firebase_messaging_handler.dart';
Future<void> _initFirebaseMessagingHandler() async {
_messagingHandler = FirebaseMessagingHandler.instance;
Stream<NotificationData?>? clickStream = await _messagingHandler.init(
androidChannelList: [
NotificationChannelData(
id: 'android_notification_channel_id',
name: 'name can be anything',
importance: NotificationImportanceEnum.max,
priority: NotificationPriorityEnum.high,
),
],
androidNotificationIconPath: '@drawable/ic_notification',
senderId: DefaultFirebaseOptions.android.messagingSenderId,
updateTokenCallback: (final String fcmToken) async {
log('FCM Token: $fcmToken');
setState(() {
_showClearButton = true;
});
//Use print for release mode FCM Debugging
//print('FCM Token: $fcmToken');
//Returning true lets the utility know that the token has been saved by the backend.
//And so this function should not be called till the token has been cleared with the removeToken()
//Note: Re-installing or clearing data will also recall this function.
return Future.value(true);
},
);
if (clickStream != null) {
clickStream.listen((NotificationData? data) {
if (data != null) {
setState(() {
_currentPayload =
(data.payload.isNotEmpty ? data.payload : '').toString();
});
}
});
}
}