getInitialNotification method

Future<UserNotification?> getInitialNotification()

Retrieves the notification that launched the app from terminated state.

This method checks if the app was opened by tapping a notification while it was completely closed. It returns null if the app was launched normally or if no notification is available.

Returns:

Example:

final initialNotif = await pushNotificationBloc.getInitialNotification();
if (initialNotif != null) {
  // Handle the notification (e.g., navigate to specific screen)
  navigateToNotificationTarget(initialNotif.linkMobile.value);
}

Implementation

Future<UserNotification?> getInitialNotification() async {
  final initialMessage = await FirebaseMessaging.instance.getInitialMessage();
  if (initialMessage != null) {
    final title = initialMessage.notification?.title ?? '';
    final body = initialMessage.notification?.body ?? '';
    final linkMobile = initialMessage.data['linkMobile'];
    final userNotification = UserNotification()
      ..title.value = title
      ..titleMobile.value = title
      ..content.value = body
      ..contentMobile.value = body
      ..link.value = linkMobile
      ..linkWeb.value = linkMobile
      ..linkMobile.value = linkMobile;
    return userNotification;
  }
  return null;
}