A Flutter plugin to use the Firebase Cloud Messaging (FCM) API.
With this plugin, your Flutter app can receive and process push notifications as well as data messages on Android and iOS. Read Firebase's About FCM Messages to learn more about the differences between notification messages and data messages.
For Flutter plugins for other Firebase products, see FlutterFire.md.
Note: This plugin is still under development, and some APIs might not be available yet. Feedback and Pull Requests are most welcome!
To use this plugin, add firebase_messaging
as a dependency in your pubspec.yaml file.
Check out the example
directory for a sample app using Firebase Cloud Messaging.
To integrate your plugin into the Android part of your app, follow these steps:
Using the Firebase Console add an Android app to your project: Follow the assistant, download the generated google-services.json
file and place it inside android/app
. Next, modify the android/build.gradle
file and the android/app/build.gradle
file to add the Google services plugin as described by the Firebase assistant.
(optional, but recommended) If want to be notified in your app (via onResume
and onLaunch
, see below) when the user clicks on a notification in the system tray include the following intent-filter
within the <activity>
tag of your android/app/src/main/AndroidManifest.xml
:
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
To integrate your plugin into the iOS part of your app, follow these steps:
Generate the certificates required by Apple for receiving push notifications following this guide in the Firebase docs. You can skip the section titled "Create the Provisioning Profile".
Using the Firebase Console add an iOS app to your project: Follow the assistant, download the generated GoogleService-Info.plist
file, open ios/Runner.xcworkspace
with Xcode, and within Xcode place the file inside ios/Runner
. Don't follow the steps named "Add Firebase SDK" and "Add initialization code" in the Firebase assistant.
In Xcode, select Runner
in the Project Navigator. In the Capabilities Tab turn on Push Notifications
.
Remove the use_frameworks!
line from ios/Podfile
(workaround for flutter/flutter#9694).
Follow the steps in the "Upload your APNs certificate" section of the Firebase docs.
From your Dart code, you need to import the plugin and instantiate it:
import 'package:firebase_messaging/firebase_messaging.dart';
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
Next, you should probably request permissions for receiving Push Notifications. For this, call _firebaseMessaging.requestNotificationPermissions()
. This will bring up a permissions dialog for the user to confirm on iOS. It's a no-op on Android. Last, but not least, register onMessage
, onResume
, and onLaunch
callbacks via _firebaseMessaging.configure()
to listen for incoming messages (see table below for more information).
Messages are sent to your Flutter app via the onMessage
, onLaunch
, and onResume
callbacks that you configured with the plugin during setup. Here is how different message types are delivered on the supported platforms:
App in Foreground | App in Background | App Terminated | |
---|---|---|---|
Notification on Android | onMessage | Notification is delivered to system tray. When the user clicks on it to open app onResume fires if click_action: FLUTTER_NOTIFICATION_CLICK is set (see below). | Notification is delivered to system tray. When the user clicks on it to open app onLaunch fires if click_action: FLUTTER_NOTIFICATION_CLICK is set (see below). |
Notification on iOS | onMessage | Notification is delivered to system tray. When the user clicks on it to open app onResume fires. | Notification is delivered to system tray. When the user clicks on it to open app onLaunch fires. |
Data Message on Android | onMessage | onMessage while app stays in the background. | not supported by plugin, message is lost |
Data Message on iOS | onMessage | Message is stored by FCM and delivered to app via onMessage when the app is brought back to foreground. | Message is stored by FCM and delivered to app via onMessage when the app is brought back to foreground. |
Additional reading: Firebase's About FCM Messages.
Refer to the Firebase documentation about FCM for all the details about sending messages to your app. When sending a notification message to an Android device, you need to make sure to set the click_action
property of the message to FLUTTER_NOTIFICATION_CLICK
. Otherwise the plugin will be unable to deliver the notification to your app when the users clicks on it in the system tray.
For testing purposes, the simplest way to send a notification is via the Firebase Console. Make sure to include click_action: FLUTTER_NOTIFICATION_CLICK
as a "Custom data" key-value-pair (under "Advanced options") when targeting an Android device. The Firebase Console does not support sending data messages.
Alternatively, a notification or data message can be sent from a terminal:
DATA='{"notification": {"body": "this is a body","title": "this is a title"}, "priority": "high", "data": {"click_action": "FLUTTER_NOTIFICATION_CLICK", "id": "1", "status": "done"}, "to": "<FCM TOKEN>"}'
curl https://fcm.googleapis.com/fcm/send -H "Content-Type:application/json" -X POST -d "$DATA" -H "Authorization: key=<FCM SERVER KEY>"
Remove the notification
property in DATA
to send a data message.
Breaking change. Migrate from the deprecated original Android Support Library to AndroidX. This shouldn't result in any functional changes, but it requires any Android apps using this plugin to also migrate if they're using the original support library.
This was originally incorrectly pushed in the 2.2.0
update.
3.0.0
instead. That's the correctly versioned new push
of 2.2.0
.BAD. This was a breaking change that was incorrectly published on a minor
version upgrade, should never have happened. Reverted by 2.2.0+1
.
Breaking change. Migrate from the deprecated original Android Support Library to AndroidX. This shouldn't result in any functional changes, but it requires any Android apps using this plugin to also migrate if they're using the original support library.
In FirebaseMessagingPlugin.m:
flutter run
happyexample/README.md
Demonstrates how to use the firebase_messaging plugin.
For help getting started with Flutter, view our online documentation.
Add this to your package's pubspec.yaml file:
dependencies:
firebase_messaging: ^4.0.0+1
You can install packages from the command line:
with Flutter:
$ flutter packages get
Alternatively, your editor might support flutter packages get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:firebase_messaging/firebase_messaging.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
4.0.0+1 | Feb 21, 2019 |
|
|
3.0.1 | Feb 8, 2019 |
|
|
3.0.0 | Jan 28, 2019 |
|
|
2.2.0+1 | Jan 28, 2019 |
|
|
2.2.0 | Jan 25, 2019 |
|
|
2.1.0 | Nov 14, 2018 |
|
|
2.0.3 | Oct 30, 2018 |
|
|
2.0.2 | Oct 18, 2018 |
|
|
2.0.1 | Oct 12, 2018 |
|
|
2.0.0 | Sep 4, 2018 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
99
|
Health:
Code health derived from static analysis.
[more]
|
100
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
100
|
Overall:
Weighted score of the above.
[more]
|
100
|
We analyzed this package on Feb 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter
References Flutter, and has no conflicting libraries.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=2.0.0-dev.28.0 <3.0.0 | ||
flutter | 0.0.0 | ||
meta | ^1.0.4 | 1.1.6 | 1.1.7 |
platform | ^2.0.0 | 2.2.0 | |
Transitive dependencies | |||
collection | 1.14.11 | ||
sky_engine | 0.0.99 | ||
typed_data | 1.1.6 | ||
vector_math | 2.0.8 | ||
Dev dependencies | |||
firebase_core | ^0.3.0 | ||
flutter_test | |||
mockito | ^3.0.0 | ||
test | ^1.3.0 |