Before you useFCM, upload your APNs authentication key toFirebaseconsole. If you don't
already have an APNs authentication key, create one in theApple Developer
Member Center.
Inside your project in theFirebaseconsole, select the gear icon, selectProject Settings, and then select theCloud Messagingtab.
Select theUploadbutton to upload your development authentication key,
or production authetication key, or both. At least one is required.
For each authentication key, select the .p8 file, and provide the key ID and your Apple team ID. SelectSave.
Method swizzling
To use theFCMFlutter plugin on Apple devices, method swizzling is
required. Without it, key Firebase features such asFCMtoken handling
won't function properly.
Android
Google Play services
FCMclients require devices running Android 4.4 or higher that also
have Google
Play services installed, or an emulator running Android 4.4 with Google APIs.
Note that you are not limited to deploying your Android apps through Google Play
Store.
Apps that rely on the Play Services SDK should always check the device for a
compatible Google Play services APK before accessing Google Play services
features. It is recommended to do this in two places: in the main activity'sonCreate()method, and in itsonResume()method. The check inonCreate()makes sure that the app can't be used without a successful check. The check inonResume()makes sure that if the user returns to the running app through some
other means, such as through the back button, the check is still performed.
TheFCMWeb interface uses Web credentials called Voluntary
Application Server Identification, or "VAPID" keys, to authorize send requests
to supported web push services. To subscribe your app to push notifications, you
need to associate a pair of keys with your Firebase project. You can either
generate a new key pair or import your existing key pair through theFirebaseconsole.
Generate a new key pair
Open theCloud
Messagingtab of theFirebaseconsoleSettingspane and go to theWeb configurationsection.
In theWeb Push certificatestab, clickGenerate Key Pair. The
console displays a notice that the key pair was generated, and displays the
public key string and date added.
Import an existing key pair
If you have an existing key pair you are already using with your web app, you
can import it toFCMso that you can reach your existing web app
instances throughFCMAPIs. To import keys, you must have
owner-level access to the Firebase project. Import your existing public and
private key in base64 URL safe encoded form:
Open theCloud
Messagingtab of theFirebaseconsoleSettingspane and go to theWeb configurationsection.
In theWeb Push certificatestab, selectimport
an existing key pair.
In theImport a key pairdialog, provide your public and private keys in
the corresponding fields and clickImport. The console displays the
public key string and date added.
For more information about the format of the keys and how to generate them,
seeApplication server keys.
From the root of your Flutter project, run the following command to install
the plugin:
flutterpubaddfirebase_messaging
Once complete, rebuild your Flutter application:
flutterrun
Access the registration token
To send a message to a specific device, you need to know the device
registration token. To retrieve the registration token for an app instance, callgetToken(). If notification permission has not been granted, this method will
ask the user for notification permissions. Otherwise, it returns a token or
rejects the future due to an error.
// You may set the permission requests to "provisional" which allows the user to choose what type// of notifications they would like to receive once the user receives a notification.finalnotificationSettings=awaitFirebaseMessaging.instance.requestPermission(provisional:true);// For apple platforms, make sure the APNS token is available before making any FCM plugin API callsfinalapnsToken=awaitFirebaseMessaging.instance.getAPNSToken();if(apnsToken!=null){// APNS token is available, make FCM plugin API requests...}
On web platforms, pass your VAPID public key togetToken():
To be notified whenever the token is updated, subscribe to theonTokenRefreshstream:
FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken){// TODO: If necessary send token to application server.// Note: This callback is fired at each app startup and whenever a new// token is generated.}).onError((err){// Error getting token.});
Prevent auto initialization
When anFCMregistration token is generated, the library uploads
the identifier and configuration data to Firebase. If you prefer to prevent
token auto generation, disable auto-initialization at build time.
iOS
On iOS, add a metadata value to yourInfo.plist:
FirebaseMessagingAutoInitEnabled = NO
Android
On Android, disable Analytics collection and FCM auto initialization (you must
disable both) by adding these metadata values to yourAndroidManifest.xml:
Otherwise, on theCampaignstab, selectNew campaignand thenNotifications.
Enter the message text.
SelectSend test messagefrom the right pane.
In the field labeledAdd anFCMregistration token, enter your
registration token.
SelectTest.
After you selectTest, the targeted client device, with the app in the
background, should receive the notification.
For insight into message delivery to your app, see theFCMreporting
dashboard, which records the number of messages sent and opened on Apple and
Android devices, along with impression data for Android apps.
Handling interaction
When users tap a notification, the default behavior on both Android and iOS is
to open the application. If the application is terminated, it will be started,
and if it is in the background, it will be brought to the foreground.
Depending on the content of a notification, you may want to handle the user's
interaction when the application opens. For example, if a new chat message is
sent using a notification and the user selects it, you may want to open the
specific conversation when the application opens.
Thefirebase-messagingpackage provides two ways to handle this interaction:
getInitialMessage():If the application is opened from a terminated
state, this method returns aFuturecontaining aRemoteMessage. Once
consumed, theRemoteMessagewill be removed.
onMessageOpenedApp: AStreamwhich posts aRemoteMessagewhen the
application is opened from a background state.
To make sure your users have a smooth experience, you should handle both
scenarios. The following code example outlines how this can be achieved:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-12-16 UTC."],[],[]]