init method

Future<void> init(
  1. BuildContext context
)

Implementation

Future<void> init(BuildContext context) async {
  // Requesting permission for notifications
  await _fcm.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );
  var permission = await _fcm.requestPermission(
    alert: true,
    announcement: false,
    badge: true,
    carPlay: false,
    criticalAlert: false,
    provisional: false,
    sound: true,
  );

  if (permission.authorizationStatus == AuthorizationStatus.authorized ||
      permission.authorizationStatus == AuthorizationStatus.provisional) {
    onNotificationAvailability(true, false);
    fcmToken = await _fcm.getToken();
    try {
      saveFcm({
        "name": username,
        "registration_id": fcmToken,
        "active": true,
        "cloud_message_type": "FCM",
        "application_id": applicationId
      });
    } catch (_) {}

    // Handling background messages using the specified handler
    FirebaseMessaging.onBackgroundMessage(
        _firebaseMessagingBackgroundHandler);
    // Listening for incoming messages while the app is in the foreground
    listen = FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      // debugPrint('Got a message whilst in the foreground!');
      // debugPrint('Message data: ${message.notification!.title.toString()}');
      if (message.notification != null) {
        if (message.notification!.title != null &&
            message.notification!.body != null) {
          // message.notification.
          //final notificationData = message.data;
          // print("notification ${notificationData}");
          // var image = '';
          // if(Platform.isAndroid){
          //   image = message.notification?.android?.imageUrl ?? "";
          // }
          // else{
          //   image = message.notification?.apple?.imageUrl ?? "";
          // }
          // if(onDataRecieved!=null){
          //   onDataRecieved(message.notification!.title, message.notification!.body, message.data);
          // }

          // firbaseNotification(message.notification!.title, message.notification!.body, image);
          // final screen = notificationData['screen'];

          // Showing an alert dialog when a notification is received (Foreground state)
          // TODO: add function argument for dialogue
        }
      }
    });

    // Handling the initial message received when the app is launched from dead (killed state)
    // When the app is killed and a new notification arrives when user clicks on it
    // It gets the data to which screen to open
    FirebaseMessaging.instance.getInitialMessage().then((message) async {
      if (message != null) {
        await _handleNotificationClick(message, context);
        // provider.activeTab(Tabs.notifications);
      }
    });
    // Handling a notification click event when the app is in the background
    listen2 = FirebaseMessaging.onMessageOpenedApp
        .listen((RemoteMessage message) async {
      // debugPrint(
      //     'onMessageOpenedApp: ${message.notification!.title.toString()}');
      await _handleNotificationClick(message, context);
      // provider.activeTab(Tabs.notifications);
    });
  } else {
    onNotificationAvailability(
        false, permission.authorizationStatus == AuthorizationStatus.denied);
  }
}