init method

Future<void> init({
  1. dynamic onClick(
    1. RemoteMessage
    )?,
  2. String? vapidKey,
  3. bool customOnClickAction = false,
})

Implementation

Future<void> init({
  Function(RemoteMessage)? onClick,
  String? vapidKey,
  bool customOnClickAction = false,
}) async {
  if (!FirebaseMessagingUtils.isSupported || !(await isBrowserSupported())) {
    return;
  }
  FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
    if (message.notification != null) {
      debugPrint("onMessage: $message");
      NotificationUtils.show(
        id: NOTIFY_ID,
        androidChannelId: NOTIFY_ANDROID_CHANNEL_ID,
        androidChannelDescription: androidChannelDescription,
        title: message.notification!.title ?? '',
        content: message.notification!.body ?? '',
        onSelectNotification: () async {
          await navigateToItemDetail(
            message,
            onClick,
            customOnClickAction,
          );
        },
      );
      message.save();
    }
  });
  FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
    debugPrint("onMessageOpenedApp: $message");
    await navigateToItemDetail(
      message,
      onClick,
      customOnClickAction,
    );
  });
  FirebaseMessaging.onBackgroundMessage((RemoteMessage message) async {
    debugPrint("onBackgroundMessage: $message");
    await navigateToItemDetail(
      message,
      onClick,
      customOnClickAction,
    );
    message.save();
  });
  messaging
      ?.requestPermission(
        alert: true,
        announcement: false,
        badge: true,
        carPlay: false,
        criticalAlert: false,
        provisional: false,
        sound: true,
      )
      .then(
        (value) => FirebaseAnalyticsUtils.instance.setUserProperty(
          AnalyticsConstants.hasEnableNotification,
          (value.authorizationStatus == AuthorizationStatus.authorized)
              .toString(),
        ),
      );
  messaging?.getToken(vapidKey: vapidKey).then((String? token) {
    if (token != null && kDebugMode) {
      print("Push Messaging token: $token");
    }
  });
}