Skip to content

[firebase-messaging]: IOS is not triggering onMessage #17637

@nijatumuyev

Description

@nijatumuyev

Is there an existing issue for this?

  • I have searched the existing issues.

Which plugins are affected?

Messaging

Which platforms are affected?

iOS

Description

On ios devices is not triggering onMessage. ON android everything is working.
Firebase Messaging plugin version: 16.0.0
Flutter version: 3.32.6-0.0.pre.1 • channel stable
Dart version: Dart 3.8.1

Here is my code sample

@singleton
class FirebaseMessagingService {
final FirebaseMessaging _fcm = FirebaseMessaging.instance;

FirebaseMessagingService();

Future init() async {
// Foreground
await _fcm.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
final token = await _fcm.getToken();
MyLogger.log("FCM Token: $token");

FirebaseMessaging.onMessage.listen(
  _onForegroundMessageHandler,
  onError: (e) => MyLogger.log("Foreground Push Error $e"),
  onDone: () => MyLogger.log("Foreground Push Done"),
);

// Handle terminated background messages
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);

// App was in background and opened from notification
FirebaseMessaging.onMessageOpenedApp.listen(_onMessageOpenedAppHandler);

// App was terminated and launched from notification
final initialMessage = await _fcm.getInitialMessage();
if (initialMessage != null) {
  WidgetsBinding.instance.addPostFrameCallback((_) {
    _onMessageOpenedAppHandler(initialMessage);
  });
}

}

void _onForegroundMessageHandler(RemoteMessage message) async {
MyLogger.log(
'onMessage data=${message.data} notif=${message.notification?.toMap()}',
);

final title = message.notification?.title ?? message.data['title'];
final body = message.notification?.body ?? message.data['body'];

final data = MyNotificationModel(
  title: title,
  description: body,
  entityType: message.data['entityType'],
  entityId: message.data['entityId'],
  createdAt: null,
  readed: false,
  type: message.data['type'],
);

WidgetsBinding.instance.addPostFrameCallback((_) {
  MyNotificationToast.showToast(data: data);
});

}

void _onMessageOpenedAppHandler(RemoteMessage message) {
appRouter.goNamed('notifications', extra: "fromPushNotification");
}
}

/// MUST be top-level and entry-point to work when app is terminated
@pragma('vm:entry-point')
Future firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
final notification = message.notification;

if (notification != null) {
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
const androidChannel = AndroidNotificationChannel(
'high_importance_channel',
'High Importance Notifications',
description: 'This channel is used for important notifications.',
importance: Importance.max,
enableLights: true,
);

final androidPlugin = flutterLocalNotificationsPlugin
    .resolvePlatformSpecificImplementation<
      AndroidFlutterLocalNotificationsPlugin
    >();

await androidPlugin?.createNotificationChannel(androidChannel);

const androidDetails = AndroidNotificationDetails(
  'high_importance_channel',
  'High Importance Notifications',
  channelDescription: 'This channel is used for important notifications.',
  importance: Importance.max,
  priority: Priority.max,
  playSound: true,
  enableVibration: true,
  enableLights: true,
);

const notificationDetails = NotificationDetails(android: androidDetails);

await flutterLocalNotificationsPlugin.show(
  DateTime.now().millisecondsSinceEpoch ~/ 1000,
  notification.title,
  notification.body,
  notificationDetails,
  payload: message.data.toString(),
);

}
}

Reproducing the issue

On ios devices is not triggering onMessage. ON android everything is working.
Firebase Messaging plugin version: 16.0.0
Flutter version: 3.32.6-0.0.pre.1 • channel stable
Dart version: Dart 3.8.1

Firebase Core version

4.0.0

Flutter Version

3.32.6-0.0.pre.1

Relevant Log Output

Flutter dependencies

Expand Flutter dependencies snippet
name: XXX
description: "XXXXX"

publish_to: "none"

version: 1.0.0+1

environment:
  sdk: ^3.8.0

dependencies:
  app_settings: ^6.1.1
  bloc: ^9.0.0
  camera: ^0.11.2
  chewie: ^1.12.1
  connectivity_plus: ^6.1.5
  crypto: ^3.0.6
  cupertino_icons: ^1.0.8
  device_info_plus: ^11.5.0
  dio: ^5.8.0+1
  encrypt: ^5.0.3
  equatable: ^2.0.7
  file_picker: ^10.2.0
  firebase_core: ^4.0.0
  firebase_messaging: ^16.0.0
  flutter:
    sdk: flutter
  flutter_bloc: ^9.1.1
  flutter_heic_to_jpg: ^1.0.0
  flutter_image_compress: ^2.4.0
  flutter_local_notifications: ^19.1.0
  flutter_localizations:
    sdk: flutter
  flutter_native_splash: ^2.4.6
  flutter_nfc_kit: ^3.6.0
  gallery_saver_plus: ^3.2.8
  get_it: ^8.0.3
  go_router: ^16.0.0
  google_fonts: ^6.2.1
  grouped_list: ^6.0.0
  http_parser: ^4.1.2
  hydrated_bloc: ^10.0.0
  injectable: ^2.5.0
  intl: ^0.20.2
  lottie: ^3.3.1
  meta: ^1.16.0
  mime: ^2.0.0
  path: ^1.9.1
  path_provider: ^2.1.5
  permission_handler: ^12.0.0+1
  pinput: ^5.0.1
  shared_preferences: ^2.5.3
  skeletonizer: ^2.0.1
  toastification: ^3.0.3
  uuid: ^4.5.1
  vector_graphics: ^1.1.18
  vector_graphics_codec: ^1.1.13
  video_player: ^2.10.0
  visibility_detector: ^0.4.0+2
  wakelock_plus: ^1.3.2

dev_dependencies:
  build_runner: ^2.4.15
  flutter_lints: ^5.0.0
  flutter_test:
    sdk: flutter
  injectable_generator: ^2.7.0

flutter:
  generate: true
  uses-material-design: true
  assets:
    - assets/animations/
    - assets/images/png/
    - assets/images/png/splash/
    - assets/images/svg/
    - assets/images/svg/icons/
fonts:
  - family: SFProText
    fonts:
      - asset: assets/fonts/SFProText.ttf

flutter_native_splash:
  color: "#ffffff"
  image: assets/images/png/splash/android_legacy.png
  image_ios: assets/images/png/splash/ios.png
  android_12:
    image: assets/images/png/splash/android_12.png
    color: "#ffffff"
  web: false

Additional context and comments

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    blocked: customer-responseWaiting for customer response, e.g. more information was requested.platform: iosIssues / PRs which are specifically for iOS.plugin: messagingtype: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions