Firebase Cloud Messaging (FCM) is a cross-platform messaging service that reliably delivers messages at no cost. An FCM implementation has two main parts.
A trusted environment, such as Cloud Functions for Firebase or an app server, that creates, targets, and sends messages.
An Apple, Android, or web (JavaScript) client that receives messages through its platform's transport service.
The first part is typically your backend. The second is your Flutter app. This guide focuses on the Flutter side: obtaining an FCM token and receiving push notifications.
Select App IDs, choose App on the next screen, and click Continue.
Enter a description and bundle ID. Scroll down, select Push Notifications, and click Continue, then Register.
The bundle ID is the PRODUCT_BUNDLE_IDENTIFIER value in /ios/Runner.xcodeproj/project.pbxproj, for example com.example.promleeblog.
In VS Code, right-click the /ios folder and choose Open in Xcode.
Go to Runner > Runner > Signing & Capabilities, click + Capability, then search for and add Push Notifications.
In the Signing & Capabilities tab, also select the Background Modes options shown below.
Add the following to the top-level main.dart file.
Keep these requirements in mind when writing a background message handler.
It must be a named function.
It must be a top-level function, such as a standalone function instead of a method on a class that requires initialization.
With Flutter 3.3.0 and later, annotate it with @pragma('vm:entry-point') immediately above the declaration. Otherwise, tree shaking may remove it in release mode.
Install the app on the device first.
Sign in to Firebase Console โ Cloud Messagingโ and choose your project.
Click Create your first campaign, select Firebase ์๋ฆผ ๋ฉ์์ง (Firebase Notification messages), and click Create.
Open the notification composer, enter a title and message, and click Send test message to send it to the FCM token you obtained earlier.
apns token has not been set yet. please ensure the apns token is available by calling getapnstoken()
Even with the basic configuration in place, this error can appear when Flutter does not obtain the APNs token automatically. In that case, retrieve the APNs token explicitly.
You may see this error after installing and running the app on a physical iOS device. On iOS, the APNs token must be available before you request an FCM token. Update your code as shown above to retrieve the APNs token first, then request the FCM token.
Firebase Cloud Messaging makes sending push notifications straightforward. Opening a specific page or performing an action when the user taps a notification takes additional work. We'll cover that in the next post.