Push notifications in Flutter are a great way to keep users engaged and informed about the latest updates and news related to the app. Flutter comes with a built-in Notification API that allows developers to send push notifications to iOS and Android devices. In this tutorial, we'll take a look at how to set up push notifications in Flutter and send notifications to devices using Firebase Cloud Messaging (FCM).
This tutorial assumes that you have a basic understanding of coding in Flutter, have installed the Flutter SDK and Android Studio, and have set up Firebase for your project. We will also assume that you have created a Firebase project, added the necessary Flutter and Firebase dependencies to your project, and that you are familiar with the Firebase Console.
The first step in setting up push notifications is to integrate Firebase Cloud Messaging into your app. You can do this by adding the following dependencies to your pubspec.yaml file:
firebase_messaging: ^10.0.3 firebase_core: ^1.0.3
Next, you need to configure Firebase Cloud Messaging in the Firebase Console. Here are the steps:
- Go to the Firebase Console and select your project.
- Click on the "Cloud Messaging" tab.
- In the "Android" section, click on "Add an Android App".
- Enter your app's package name and optional app nickname.
- Download the google-services.json file and save it to the android/app/ directory of your Flutter app (if it doesn't exist, create it).
- Then, in the "iOS" section, click on "Add an iOS App".
- Enter your bundle ID and optional app nickname.
- Download the GoogleService-Info.plist file and save it to the ios/Runner/ directory of your Flutter app (if it doesn't exist, create it).
Now that we have configured Firebase Cloud Messaging in the Firebase Console and added the necessary dependencies to our project, let's move on to implementing push notifications in our Flutter app.
Implementing push notifications in Flutter
The first step in implementing push notifications in Flutter is to initialize Firebase. This should be done in your app's main method before calling runApp().
// Initialize Firebase await Firebase.initializeApp(); runApp(MyApp());
Next, we need to request permission from the user to send push notifications and get the device token. This can be done using the FirebaseMessaging Flutter package:
// Request permission for push notifications FirebaseMessaging messaging = FirebaseMessaging.instance; NotificationSettings settings = await messaging.requestPermission(); // Get device token for push notifications String token = await messaging.getToken();
Next, we need to handle incoming push notifications. This can be done using the configure method in the FirebaseMessaging Flutter package:
// Handle incoming push notifications FirebaseMessaging.onMessage.listen((RemoteMessage message) { RemoteNotification notification = message.notification; AndroidNotification android = message.notification?.android; // Handle notification }); FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { // Handle opened notification });
Finally, we need to send a push notification from the Firebase Console. Here are the steps:
- Go to the Firebase Console and select your project.
- Click on the "Cloud Messaging" tab
- Click on "New Notification".
- Enter a title and message.
- Select the target platform (iOS, Android, or both).
- Click "Send test message" to test the notification on your device.
Conclusion
In conclusion, push notifications are an important feature in any modern Flutter app. In this tutorial, we explored how to set up and implement push notifications in Flutter using Firebase Cloud Messaging. We covered setting up Firebase Cloud Messaging in the Firebase Console and in our Flutter app, requesting permission and getting the device token for push notifications, handling incoming push notifications, and sending a push notification from the Firebase Console.