setupOnMessage function

void setupOnMessage(
  1. TargetPlatform platform,
  2. AndroidNotificationChannel? androidChannel,
  3. FlutterLocalNotificationsPlugin localNotificationsPlugin,
  4. OnMessageCallback? callback,
)

setupOnMessage is responsible for setting up what happens when a message is received Disclaimer : please note that this method is excluded from coverage since we have have not identified a way to extend/implement firebase or use generic for it's static methods which function consumes Anyone with an idea of how to do, please implement and teach us.

Implementation

void setupOnMessage(
  TargetPlatform platform,
  AndroidNotificationChannel? androidChannel,
  FlutterLocalNotificationsPlugin localNotificationsPlugin,
  OnMessageCallback? callback,
) {
  FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
  FirebaseMessaging.onMessage.listen(
    (RemoteMessage message) {
      /// handle [notifications].The payload contains a notification property, which will be used to present a visible notification to the user.
      final RemoteNotification notification = message.notification!;
      late NotificationDetails notificationDetails;
      final NotificationPayloadBehaviorObject
          notificationPayloadBehaviorObject =
          NotificationPayloadBehaviorObject();

      /// setup android NotificationDetails
      if (platform == TargetPlatform.android && androidChannel != null) {
        final AndroidNotification? android = message.notification?.android;
        notificationDetails = NotificationDetails(
          android: AndroidNotificationDetails(
            androidChannel.id,
            androidChannel.name,
            channelDescription: androidChannel.description,
            icon: android?.smallIcon,
            importance: Importance.max,
            priority: Priority.high,
            showProgress: true,
          ),
        );
      }

      /// setup ios NotificationDetails
      if (platform == TargetPlatform.iOS) {
        notificationDetails = const NotificationDetails();
      }

      // setup macos NotificationDetails
      if (platform == TargetPlatform.macOS) {
        notificationDetails = const NotificationDetails();
      }

      // save the notification payload
      notificationPayloadBehaviorObject.notificationBody
          .add(notification.body!);
      notificationPayloadBehaviorObject.notificationTitle
          .add(notification.title!);
      notificationPayloadBehaviorObject.notificationData.add(message.data);

      localNotificationsPlugin.show(notification.hashCode, notification.title,
          notification.body, notificationDetails,
          payload: notification.title);
    },
  );

  FirebaseMessaging.onMessageOpenedApp.listen(
    (RemoteMessage message) {
      // ignore: unused_local_variable
      late NotificationDetails notificationDetails;

      callback!(message.data, message.notification!.title,
          message.notification!.body);

      /// setup android NotificationDetails
      if (platform == TargetPlatform.android && androidChannel != null) {
        final AndroidNotification? android = message.notification?.android;
        notificationDetails = NotificationDetails(
          android: AndroidNotificationDetails(
            androidChannel.id,
            androidChannel.name,
            channelDescription: androidChannel.description,
            icon: android?.smallIcon,
            importance: Importance.max,
            priority: Priority.high,
            showProgress: true,
          ),
        );
      }

      /// setup ios NotificationDetails
      if (platform == TargetPlatform.iOS) {
        notificationDetails = const NotificationDetails();
      }

      // setup macos NotificationDetails
      if (platform == TargetPlatform.macOS) {
        notificationDetails = const NotificationDetails();
      }
    },
  );
}