Sub topic 4
Asking the user for notification permission
What permission controls
Assume you already have a Flutter app connected to Firebase and understand Dart async/await. This chapter focuses on runtime notification permission, not Firebase project creation.
A push message can reach a device without appearing as a visible notification. The operating system decides whether your app may show alerts, sounds, or badges. Your app must ask the user before requesting those capabilities.
Android and iOS use different rules:
| Platform | When permission is needed | Important detail |
|---|---|---|
| Android 12 and earlier | Usually granted during installation | Notification channels still affect display behavior |
| Android 13 and later | At runtime | Add `POST_NOTIFICATIONS` and request permission |
| iOS | At runtime | The user can allow alerts, sounds, badges, or none |
Add Android permission
For apps that target Android 13, add this permission to android/app/src/main/AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application
android:label="notification_demo"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<!-- Existing application configuration -->
</application>
</manifest>The permission declaration makes the request possible. It does not automatically show the permission dialog.
Request permission with Firebase Messaging
The FlutterFire plugin provides one API for the permission request:
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
Future<bool> requestNotificationPermission() async {
final messaging = FirebaseMessaging.instance;
final settings = await messaging.requestPermission(
alert: true,
badge: true,
sound: true,
provisional: false,
);
debugPrint(
'Notification authorization: ${settings.authorizationStatus}',
);
return settings.authorizationStatus == AuthorizationStatus.authorized ||
settings.authorizationStatus == AuthorizationStatus.provisional;
}Call this after you have explained the benefit of notifications, such as after sign-in or when the user enables order updates. Avoid showing the system dialog immediately on the first screen. A user who selects Don’t Allow may need to change the choice in system settings later.
authorized means the user granted normal permission. On iOS, provisional can provide quieter notifications without a full permission decision. Use it only when that behavior fits your product.
Interpret the result
The returned NotificationSettings object also contains the user's choices for alerts, badges, and sounds. Treat denied and not-determined states differently:
notDetermined: the app has not completed the permission decision.denied: the user rejected permission or disabled it in system settings.authorized: visible notifications are allowed.provisional: iOS allows quiet notifications.
Do not repeatedly call the permission method after denial. Instead, show an in-app explanation and provide a button that opens the application settings page. The exact settings-navigation code depends on the platform plugin you choose.
Exercise
Why can an Android 13 app need both a manifest permission and a runtime request?
Show answer
The manifest declares that the app may request notification permission, while the runtime request asks the user to grant it.