To get started with FCM, build out the simplest use case: sending a test notification message from the Notifications composer to a development device when the app is in the background on the device. This page lists all the steps to achieve this, from setup to verification — it may cover steps you already completed if you have set up a Flutter app for FCM.
Install the FCM plugin
Install and initialize the Firebase SDKs for Flutter if you haven't already done so.
From the root of your Flutter project, run the following command to install the plugin:
flutter pub add firebase_messaging
Once complete, rebuild your Flutter application:
Access the registration token
To send a message to a specific device, you need to know that device's registration token. Because you'll need to enter the token in a field in the Notifications console to complete this tutorial, make sure to copy the token or securely store it after you retrieve it.
To retrieve the current registration token for an app instance, call
getToken(). 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.
final fcmToken = await FirebaseMessaging.instance.getToken();
Send a test notification message
Install and run the app on the target device. On Apple devices, you'll need to accept the request for permission to receive remote notifications.
Make sure the app is in the background on the device.
In the Firebase console, open the Messaging page.
If this is your first message, select Create your first campaign.
- Select Firebase Notification messages and select Create.
Otherwise, on the Campaigns tab, select New campaign and then Notifications.
Enter the message text. All other fields are optional.
Select Send test message from the right pane.
In the field labeled Add an FCM registration token, enter the registration token you obtained in a previous section of this guide.
After you select Test, the targeted client device (with the app in the background) should receive the notification.
For insight into message delivery to your app, see the FCM reporting dashboard, which records the number of messages sent and opened on Apple and Android devices, along with data for "impressions" (notifications seen by users) for Android apps.
Send messages to foregrounded apps
Once you have successfully sent notification messages while your app is in the background, see Receive Messages in a Flutter App to get started sending to foregrounded apps.
Go beyond notification messages
To add other, more advanced behavior to your app, you'll need a server implementation.
Then, in your app client: