Open
Description
Is there an existing issue for this?
- I have searched the existing issues.
Which plugins are affected?
Messaging
Which platforms are affected?
Web
Description
In all the platforms, initializing FCM and getting the token works just fine. But the PWA version in iOS on Safari initialization fails with the following error, even though notifications permission is granted using a button just before FCM initialization:
NotAllowedError: User denied push permission
Reproducing the issue
Button on pressed:
onPressed: () {
PushNotificationsService.init();
},
PushNotificationsService init method:
bool _initialized = false;
Future<void> init({BackgroundMessageHandler? backgroundMessageHandler}) async {
if (_initialized) return;
try {
// Check FCM compatibility with the browser
if (kIsWeb && !(await FirebaseMessaging.instance.isSupported())) {
_logger.w('FCM is not supported on this browser');
return;
}
// Handle notifications permission
final settings = await FirebaseMessaging.instance.requestPermission();
_logger.d('User granted notifications permission: ${settings.authorizationStatus}');
// Initialize local notifications for mobile
if (!kIsWeb) _initializeLocalNotificationsMobile();
// For apple platforms, ensure the APNS token is available before making any FCM plugin API calls
if (!kIsWeb &&
(defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.macOS)) {
final apnsToken = await FirebaseMessaging.instance.getAPNSToken();
if (apnsToken != null) {
_logger.d('APNS token: $apnsToken');
} else {
_logger.w('APNS token is null, FCM plugin API calls may fail');
}
}
// Handle foreground messages
FirebaseMessaging.onMessage.listen((message) {
_logger.d('Got a message whilst in the foreground: ${message.toMap()}');
if (message.notification != null) {
if (kIsWeb) {
WebUtils.showNotification(message);
} else {
_showNotificationMobile(message);
}
}
});
// Handle initial message if app was launched from notification
final initialMessage = await FirebaseMessaging.instance.getInitialMessage();
if (initialMessage != null) handleMessage(initialMessage);
// Handle any interaction when the app is in the background
FirebaseMessaging.onMessageOpenedApp.listen(handleMessage);
// Handle background/terminated state messages
// Note: Background messages on web are handled in the web/firebase-messaging-sw.js file
if (!kIsWeb && backgroundMessageHandler != null) {
FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
}
// Handle FCM token
final token = await FirebaseMessaging.instance.getToken(vapidKey: _vapidKey);
if (token != null) {
_logger.d('FCM token: $token');
_saveFirebaseToken(token);
} else {
_logger.w('FCM token is null');
}
FirebaseMessaging.instance.onTokenRefresh.listen((newToken) {
_logger.d('FCM token refreshed: $newToken');
_saveFirebaseToken(newToken);
});
_initialized = true;
} catch (error, stackTrace) {
_logger.e('Error initializing push notifications', error: error, stackTrace: stackTrace);
}
}
Firebase Core version
3.13.0
Flutter Version
Flutter 3.32.2
Relevant Log Output
NotAllowedError: User denied push permission
Flutter dependencies
Expand Flutter dependencies
snippet
- firebase_core_platform_interface 5.4.0 [collection flutter flutter_test meta plugin_platform_interface]
- firebase_core_web 2.23.0 [firebase_core_platform_interface flutter flutter_web_plugins meta web]
- firebase_crashlytics_platform_interface 3.8.7 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_platform_interface 4.6.7 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_web 3.10.7 [_flutterfire_internals firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins meta web]
Additional context and comments
No response