[firebase_messaging][iOS] 16.7.0 breaks foreground notifications posted by other plugins on UIScene apps that own the UNUserNotificationCenter delegate
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 74/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- dart, objective-c, swift
- Ambito
- mobile-dev
Direzione di ricerca
Start at registerWithRegistrar and the scene-connect path in the iOS messaging plugin, then review registerForRemoteNotificationsIfAutoInitEnabled and configureNotificationCenterDelegate. Reproduce with ios/Runner/AppDelegate.swift and lib/main.dart in firebase_messaging/example using a data-only foreground message. Done means APNs registration still works and flutter_local_notifications displays the notification without firebase_messaging taking over the host delegate.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Is there an existing issue for this?
- I have searched the existing issues.
Which plugins are affected?
Messaging
Which platforms are affected?
iOS
Description
After upgrading firebase_messaging from 16.5.0 to 16.7.0, foreground notifications posted by flutter_local_notifications are no longer displayed on iOS.
This affects apps that:
- adopt the UIScene lifecycle (
UIApplicationSceneManifest+FlutterImplicitEngineDelegate), - assign
UNUserNotificationCenter.current().delegatethemselves (so thatFlutterAppDelegateforwardswillPresentNotificationto every plugin registered viaaddApplicationDelegate), and - render foreground notifications themselves from data-only messages, without calling
setForegroundNotificationPresentationOptions.
FlutterLocalNotificationsPlugin.show() completes normally, but nothing appears on screen.
This is a regression: 16.5.0 works, 16.7.0 does not. 16.6.0 contains no iOS notification changes.
Calling FLTFirebaseMessagingPlugin.configureNotificationCenterDelegate() as documented for UIScene apps does not help either (see the verification table below).
Root cause
#18620 added an early setup call in registerWithRegistrar:
if ([UIApplication sharedApplication].connectedScenes.count > 0) {
instance->_sceneDidConnect = YES;
[instance setupNotificationHandlingWithRemoteNotification:nil];
}
The intent was to request APNs registration when UIScene apps register the plugin after the launch callbacks have already fired. However, setupNotificationHandlingWithRemoteNotification: also calls configureNotificationCenterDelegate, which contains this guard:
if (currentDelegate != nil) {
if ([currentDelegate conformsToProtocol:@protocol(FlutterAppLifeCycleProvider)]) {
shouldReplaceDelegate = NO;
}
}
The guard only applies when a delegate is already installed. Depending on when the scene connects, plugin registration can run before the host assigns its delegate, so currentDelegate is nil, the guard is skipped, and this plugin takes over the delegate while storing nil in _originalNotificationCenterDelegate.
The host then overwrites the delegate, but this plugin remains in the forwarding chain via addApplicationDelegate. When a local notification is about to be presented, its willPresentNotification runs and reaches:
} else {
UNNotificationPresentationOptions presentationOptions = UNNotificationPresentationOptionNone;
NSDictionary *persistedOptions = [[NSUserDefaults standardUserDefaults]
dictionaryForKey:kMessagingPresentationOptionsUserDefaults];
if (persistedOptions != nil) { ... }
completionHandler(presentationOptions);
}
_originalNotificationCenterDelegate is nil and setForegroundNotificationPresentationOptions was never called, so it answers UNNotificationPresentationOptionNone and suppresses a notification that belongs to another plugin.
Reproducing the issue
Applied to firebase_messaging/example on main.
ios/Runner/AppDelegate.swift — keep the documented call and additionally let the host own the delegate:
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// UIScene registers plugins after this method returns. Apple requires the
// UNUserNotificationCenter delegate to be set before launch completes.
FLTFirebaseMessagingPlugin.configureNotificationCenterDelegate()
+
+ // Apps that also use flutter_local_notifications must own the delegate so
+ // that FlutterAppDelegate forwards willPresentNotification to every plugin
+ // registered through addApplicationDelegate.
+ UNUserNotificationCenter.current().delegate = self
+
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
lib/main.dart — initialize() is required before any notification can be shown on iOS, and the presentation options are removed:
- await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
- alert: true,
- badge: true,
- sound: true,
- );
+ // Required before any notification can be shown on iOS.
+ await flutterLocalNotificationsPlugin.initialize(
+ settings: const InitializationSettings(
+ android: AndroidInitializationSettings('launch_background'),
+ iOS: DarwinInitializationSettings(),
+ ),
+ );
+
+ // NOTE: setForegroundNotificationPresentationOptions is intentionally NOT
+ // called. Apps that render foreground notifications themselves with
+ // flutter_local_notifications do not need FCM to present anything.
Also make showFlutterNotification read title/body from message.data so it posts a notification for data-only messages on iOS.
Then send a data-only message while the app is in the foreground:
{
"message": {
"token": "<fcm-token>",
"data": { "title": "title", "body": "body" }
}
}
Result on 16.7.0
test: delegate after configureNotificationCenterDelegate = Optional(<FLTFirebaseMessagingPlugin: 0x30154ce00>)
test: delegate after didFinishLaunching = Optional(<Runner.AppDelegate: 0x301d44910>)
test: delegate before plugin registration = Optional(<Runner.AppDelegate: 0x301d44910>)
test: delegate after plugin registration = Optional(<Runner.AppDelegate: 0x301d44910>)
test: onMessage received (1789709030567108)
test: localNotification.show start
test: localNotification.show done
show() completes, but no notification is displayed.
Proposed fix
Request APNs registration without performing the delegate handover. registerForRemoteNotificationsIfAutoInitEnabled was added in #18650 and is documented as safe to call repeatedly:
if ([UIApplication sharedApplication].connectedScenes.count > 0) {
instance->_sceneDidConnect = YES;
- [instance setupNotificationHandlingWithRemoteNotification:nil];
+ [instance registerForRemoteNotificationsIfAutoInitEnabled];
}
Full notification handling then runs from didFinishLaunching / scene:willConnect as before, by which time the host delegate is installed and the FlutterAppLifeCycleProvider guard applies.
Verification
Same device, same message, using the example described above:
configureNotificationCenterDelegate() |
plugin | install | foreground notification | APNs token |
|---|---|---|---|---|
| not called | 16.7.0 as released | existing | not displayed | received |
| not called | 16.7.0 + patch | existing | displayed | received |
| called | 16.7.0 as released | existing | not displayed | received |
| called | 16.7.0 + patch | existing | displayed | received |
| called | 16.7.0 + patch | fresh (app deleted first) | displayed | received |
show() completed in every run; the only difference is the delegate path on the iOS side.
The last row matches the test plan of #18620 (app deleted from a physical device, no cached APNs token). getToken() resolved without apns-token-not-set, so the goal of #18620 — fixing #18555 — is preserved.
Firebase Core version
4.15.0
Flutter Version
3.44.9
Relevant Log Output
test: delegate after configureNotificationCenterDelegate = Optional(<FLTFirebaseMessagingPlugin: 0x30154ce00>)
test: delegate after didFinishLaunching = Optional(<Runner.AppDelegate: 0x301d44910>)
test: delegate before plugin registration = Optional(<Runner.AppDelegate: 0x301d44910>)
test: delegate after plugin registration = Optional(<Runner.AppDelegate: 0x301d44910>)
test: onMessage received (1789709030567108)
test: localNotification.show start
test: localNotification.show done
Flutter dependencies
firebase_core: ^4.15.0
firebase_messaging: ^16.7.0
flutter_local_notifications: ^21.0.0
Additional context and comments
Tested on iPhone SE (3rd generation), iOS 18.0.1, Flutter 3.44.9.
The same result occurs when FLTFirebaseMessagingPlugin.configureNotificationCenterDelegate() is not called and only UNUserNotificationCenter.current().delegate = self is set.
- Lingua principale
- Dart
- Stelle
- 9.3k
- Fork
- 4.1k
- Merge medio
- 1g 14h
- PR unite (30g)
- 39
Preparare l'ambiente
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di firebase/flutterfire
-
[cloud_firestore]: a snapshots() listener cancelled while it registers is never removed nativelyApertaNeeds Attention platform: android platform: ios platform: macos plugin: cloud_firestore reproduced type: bug
Difficoltà 3/5 1-2 giorni Idoneità per principianti 72/100
firebase/flutterfire#18724 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
blocked: customer-response platform: web plugin: cloud_firestore type: bug
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
firebase/flutterfire#18700 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
blocked: flutter platform: windows plugin: storage reproduced type: bug
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
firebase/flutterfire#18664 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
[core] Decouple platform_interface from Flutter for pure-Dart webForse già presa @Lyokone l’ha presa 12 giorni fa. ApertaNeeds Attention platform: all platform: web plugin: core type: enhancement
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
firebase/flutterfire#18645 · 2 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
blocked: firebase-sdk platform: android plugin: messaging reproduced type: bug
Difficoltà 3/5 1-2 giorni Idoneità per principianti 56/100
firebase/flutterfire#18644 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di firebase/flutterfire
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
google/skills_lint.dart#58 ·
I maintainer di solito rispondono entro 1 giorno
-
P2 status: first-line-handled type: bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
a2ui-project/a2ui#2871 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
genkit-ai/genkit-dart#507 ·
I maintainer di solito rispondono entro 4 giorni
-
infra: device lab team-android
Difficoltà 2/5 1-2 giorni Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
google/webcrypto.dart#419 ·
I maintainer di solito rispondono entro 4 giorni