DartFirebaseFlutter

Implementing Push Notifications in Flutter (Firebase & OneSignal)

Implementing Push Notifications In Flutter Firebase OneSignal 683x1024

Introduction

Push notifications are one of the most effective ways to keep users engaged with a mobile app. They enable real-time alerts, reminders, and updates even when the app is closed. In Flutter, the most common approaches use Firebase Cloud Messaging (FCM) and OneSignal. While both solutions solve the same problem, they differ in setup, flexibility, and backend control. In this guide, you will learn how push notifications work in Flutter, how to implement them using Firebase and OneSignal, and when to choose one over the other.

Why Push Notifications Matter in Flutter Apps

Push notifications directly impact user retention and engagement. Therefore, implementing them correctly is essential for production apps.

• Deliver timely updates
• Re-engage inactive users
• Support real-time alerts
• Improve user experience
• Enable transactional messaging

However, notifications must always provide value to avoid user fatigue.

How Push Notifications Work

Before implementing notifications, it helps to understand the basic flow.

• App registers with a notification service
• Device receives a unique push token
• Token is stored on the backend
• Backend sends messages to the service
• Service delivers notifications to devices

This architecture keeps sensitive credentials on the server and ensures reliable delivery.

Firebase Cloud Messaging in Flutter

Firebase Cloud Messaging is Google’s push notification service and is widely used in Flutter apps.

Why Use Firebase Cloud Messaging

FCM integrates naturally with Flutter and offers strong control over messaging.

• Official Flutter support
• Works on Android and iOS
• Supports notification and data messages
• Integrates with Firebase Authentication
• Scales easily

Because of this, FCM is often the default choice for Flutter developers.

Setting Up Firebase in Flutter

Start by creating a Firebase project and adding your Flutter app.

Install Dependencies

dependencies:
  firebase_core: ^3.0.0
  firebase_messaging: ^15.0.0

Initialize Firebase when the app starts.

await Firebase.initializeApp();

Requesting Notification Permissions

Permissions must be handled explicitly, especially on iOS.

NotificationSettings settings =
    await FirebaseMessaging.instance.requestPermission();

Always explain why notifications are needed before requesting permission.

Getting the FCM Token

Each device receives a unique token used for targeting notifications.

final token = await FirebaseMessaging.instance.getToken();

Send this token to your backend and store it securely.

Handling Notifications in Flutter

Notification handling depends on the app state.

Foreground Messages

FirebaseMessaging.onMessage.listen((message) {
  print('Foreground message: ${message.notification?.title}');
});

Background and Terminated State

FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

This ensures messages are processed even when the app is not active.

Notification Messages vs Data Messages

FCM supports two message types.

• Notification messages are handled by the OS
• Data messages are handled by the app
• Notification messages are simpler
• Data messages allow full customization

Choosing the right type depends on how much control you need.

Using OneSignal with Flutter

OneSignal is a third-party notification platform that simplifies push notifications.

Why Choose OneSignal

OneSignal focuses on ease of use and advanced features.

• Simple setup
• Built-in analytics
• User segmentation
• Scheduling and automation
• No custom backend required for basic use

Because of this, OneSignal is popular for marketing-driven apps.

Setting Up OneSignal in Flutter

Install the OneSignal Flutter SDK.

dependencies:
  onesignal_flutter: ^5.0.0

Initialize OneSignal during app startup.

OneSignal.initialize("YOUR_ONESIGNAL_APP_ID");
OneSignal.Notifications.requestPermission(true);

After setup, OneSignal handles device registration automatically.

Handling Notifications with OneSignal

OneSignal provides callbacks for notification events.

• Notification received
• Notification opened
• App launched from notification

This simplifies UI navigation and tracking.

Firebase vs OneSignal: Key Differences

Choosing between Firebase and OneSignal depends on project needs.

Firebase Cloud Messaging

• Full backend control
• Requires server-side logic
• Strong integration with Firebase
• Ideal for transactional notifications

OneSignal

• Minimal backend setup
• Advanced dashboards
• Marketing-friendly features
• Faster initial setup

In many cases, teams start with FCM and add OneSignal for marketing workflows.

Best Practices for Push Notifications

Reliable notifications require thoughtful design.

• Ask permission at the right moment
• Keep messages short and clear
• Avoid sending too often
• Handle token refresh events
• Test on real devices

Following these practices improves opt-in rates and engagement.

Common Pitfalls to Avoid

Requesting Permission Too Early

Users are more likely to decline without context.

Ignoring Token Updates

Push tokens can change at any time.

Overusing Notifications

Too many messages lead to uninstalls.

Avoiding these mistakes keeps notifications effective.

When to Use Firebase, OneSignal, or Both

Use Firebase when you need:
• Full control over backend logic
• Secure transactional messages
• Deep Firebase integration

Use OneSignal when you need:
• Fast setup
• Marketing automation
• Analytics and segmentation

Some apps use both to cover different use cases.

Conclusion

Implementing push notifications in Flutter with Firebase and OneSignal allows you to deliver timely, relevant updates to users across platforms. Firebase Cloud Messaging offers full control and deep backend integration, while OneSignal simplifies setup and provides powerful engagement tools. If you are building robust Flutter systems, read Building Offline-First Flutter Apps: Local Storage and Sync. You can also explore the Firebase Cloud Messaging documentation. With the right strategy, push notifications become a powerful channel for user engagement.

1 Comment

Leave a Comment