Sub topic 4
Asking the User for Notification Permission
This chapter covers how a Flutter app asks for notification permission with firebase_messaging. It also covers how to read the answer and what to do when the user refuses. It assumes you have a Flutter project connected to Firebase (the firebase_core setup from the earlier chapter) and that you are comfortable with async/await. You should also know your way around AndroidManifest.xml and Xcode capabilities.
Why permission is a separate step
Having a working FCM setup does not mean the user will see notifications. The operating system decides whether your app may show alerts, play sounds, or update the badge count on its icon. The user controls that decision, and your app must ask for it.
Two things are easy to mix up:
- Receiving a message. FCM can deliver data to the device even when notifications are blocked. A data message can still reach your Dart code.
- Displaying a notification. The OS only shows a visible notification if the user has granted permission.
So the permission request controls the visible part of the experience. It does not control whether your registration token exists.
How each platform treats permission
| Platform | Permission required? | How you ask |
|---|---|---|
| iOS | Always, for alerts, badges and sounds | `requestPermission()` shows a system dialog |
| Android 13 (API 33) and higher | Yes, the `POST_NOTIFICATIONS` runtime permission | `requestPermission()` shows a system dialog |
| Android 12 (API 32) and lower | No, notifications are on by default | `requestPermission()` returns `authorized` without a dialog |
The same Dart call covers all three cases. On older Android, it simply reports that notifications are allowed.
The Android manifest entry
Apps that target Android 13 or higher must declare the permission. Without this line, the system never shows the dialog and notifications stay blocked.
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application>
<!-- existing application configuration -->
</application>
</manifest>Add the uses-permission line to android/app/src/main/AndroidManifest.xml. Keep your existing application content as it is.
The iOS side
On iOS the permission dialog comes from the same Dart call. For it to work, the Xcode project needs the Push Notifications capability and the Background Modes capability with Remote notifications checked. You configured these in the platform setup chapter. If they are missing, requestPermission() can still show a dialog, but no messages will arrive.
Calling requestPermission
FirebaseMessaging.instance.requestPermission() shows the system dialog and returns a NotificationSettings object.
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
Future<NotificationSettings> askForPermission() async {
final messaging = FirebaseMessaging.instance;
final settings = await messaging.requestPermission(
alert: true,
badge: true,
sound: true,
);
debugPrint('Permission status: ${settings.authorizationStatus}');
return settings;
}The three flags say which capabilities you want. All three default to true, so listing them is optional. They are useful when you want to ask for fewer, for example sound: false for a quiet app.
Reading the result
settings.authorizationStatus is an AuthorizationStatus enum. It has four values:
| Value | Meaning |
|---|---|
| `authorized` | The user allowed notifications. |
| `denied` | The user refused, or turned notifications off in system settings. |
| `notDetermined` | The user has not been asked yet. |
| `provisional` | iOS only. Notifications are delivered quietly (see below). |
Branch on the status instead of assuming success:
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
Future<void> initNotifications() async {
final messaging = FirebaseMessaging.instance;
final settings = await messaging.requestPermission();
switch (settings.authorizationStatus) {
case AuthorizationStatus.authorized:
debugPrint('Notifications allowed');
case AuthorizationStatus.provisional:
debugPrint('Quiet notifications allowed');
case AuthorizationStatus.denied:
debugPrint('Notifications refused');
case AuthorizationStatus.notDetermined:
debugPrint('User has not decided');
}
}This switch statement needs Dart 3 or newer, where cases end without break. On older Dart versions, add break; to each case.
What happens on repeated requests
The system dialog is a one-shot tool. The behavior differs by platform:
- iOS shows the dialog once. After the user answers, later calls to
requestPermission()return the stored answer and show nothing. The only way to change a refusal is for the user to open the Settings app. - Android 13+ shows the dialog the first time. If the user denies it twice, the system stops showing it, and later requests return
deniedimmediately. The exact rules vary by Android version and device maker, so treat "ask at most once or twice" as a safe design assumption.
This has a practical consequence. Do not call requestPermission() at app launch with no context. A user who sees an unexplained dialog is more likely to tap Don't allow, and you may never get a second chance.
The diagram below shows the decision flow that follows from this.
Checking the status without asking
You often want to know the current status before deciding whether to show a prompt. getNotificationSettings() returns the same NotificationSettings object and never shows a dialog.
import 'package:firebase_messaging/firebase_messaging.dart';
Future<bool> canShowNotifications() async {
final settings =
await FirebaseMessaging.instance.getNotificationSettings();
final status = settings.authorizationStatus;
return status == AuthorizationStatus.authorized ||
status == AuthorizationStatus.provisional;
}Use this at startup to decide whether to show your own explanation screen. A common pattern is:
- Call
getNotificationSettings()when the relevant screen opens. - If the status is
notDetermined, show a short screen that explains what notifications you will send and why. - When the user taps your Enable button, call
requestPermission(). - If the status is
denied, show a message with a button that opens the system settings page.
Your own pre-prompt screen costs nothing. If the user declines on it, you have not used up the one-time system dialog.
Provisional authorization on iOS
iOS supports a softer option. With provisional: true, the app gets permission without showing a dialog. Notifications go straight to the Notification Center, with no sound or banner. The user can then choose to keep or turn them off from the notification itself.
import 'package:firebase_messaging/firebase_messaging.dart';
Future<void> requestQuietPermission() async {
final settings = await FirebaseMessaging.instance.requestPermission(
provisional: true,
);
print(settings.authorizationStatus); // AuthorizationStatus.provisional
}Provisional mode is useful when you want to show value before asking for full permission. It has no effect on Android, which ignores the flag.
Other fields on NotificationSettings
NotificationSettings reports more than the overall status. Each capability has its own AppleNotificationSetting value (enabled, disabled, or notSupported):
alert,badge,sound: whether each was granted.lockScreen,notificationCenter: where notifications may appear.announcement,carPlay,criticalAlert,timeSensitive: special iOS cases.
These matter mainly on iOS, where the user can allow banners but block sounds. On Android most of them report notSupported. Check them only if your app depends on a specific capability.
Common errors
- No dialog appears on Android 13+. The manifest lacks
POST_NOTIFICATIONS, or the app'stargetSdkVersionis below 33. Checkandroid/app/build.gradleand the manifest. - The status stays
deniedafter the user changes their mind. The dialog will not appear again. Send them to system settings. Thepermission_handlerpackage offersopenAppSettings()for this. - Calling
requestPermission()beforeFirebase.initializeApp(). The call throws because the Firebase app does not exist yet. Initialize first, as shown in the setup chapter. - Expecting the status to be
authorizedon Android 12 and lower. It is, but only because no permission exists there. Do not treat it as a signal that the user made a choice. - Testing a second time on the same device. The old answer is stored. Uninstall the app, or reset the permission in system settings, to see the dialog again.
Putting it together
A short helper that combines the checks above looks like this:
import 'package:firebase_messaging/firebase_messaging.dart';
enum PermissionOutcome { granted, needsSettings, declined }
Future<PermissionOutcome> ensureNotificationPermission() async {
final messaging = FirebaseMessaging.instance;
var settings = await messaging.getNotificationSettings();
if (settings.authorizationStatus == AuthorizationStatus.notDetermined) {
settings = await messaging.requestPermission();
}
switch (settings.authorizationStatus) {
case AuthorizationStatus.authorized:
case AuthorizationStatus.provisional:
return PermissionOutcome.granted;
case AuthorizationStatus.denied:
return PermissionOutcome.needsSettings;
case AuthorizationStatus.notDetermined:
return PermissionOutcome.declined;
}
}Call this from your Enable notifications button. When it returns granted, continue to fetch the token, which the next chapter covers.
Exercise
A user on iOS taps "Don't Allow" on the permission dialog. A week later your app calls requestPermission() again from a settings screen. What does the call do, and how should your app respond?
Show answer
The call shows no dialog and returns AuthorizationStatus.denied, because iOS only prompts once. The app should explain that notifications are off and offer a button that opens the system settings page, where the user can turn them on.
Exercise
Your app works on an Android 12 emulator but shows no permission dialog and no notifications on an Android 13 device. What is the most likely cause, and what do you check first?
Show answer
The POST_NOTIFICATIONS permission is probably missing from AndroidManifest.xml, or targetSdkVersion is below 33. Android 12 and lower need no permission, so the app worked there. Check the manifest and build.gradle first.
Exercise
What is the difference between requestPermission() and getNotificationSettings()?
Show answer
requestPermission() can show the system dialog and returns the user's answer. getNotificationSettings() only reads the current status and never shows a dialog, so you can call it safely to decide whether to show your own explanation screen.