activate method

  1. @override
Future<void> activate({
  1. required void onRead(
    1. Map<String, dynamic>
    ),
  2. required void onActivated(
    1. String?
    ),
})
override

activate remote push notification service when user is authenticated

Implementation

@override
Future<void> activate({
  required void Function(Map<String, dynamic>) onRead,
  required void Function(String?) onActivated,
}) async {
  _onReadNotification = onRead;
  await _activateForiOS();
  if (previousToken == null) {
    final String? fcmToken = await FirebaseMessaging.instance.getToken();
    onActivated(fcmToken);
  }
  //when app is started from terminated state
  final RemoteMessage? notificationStackMessage =
      await FirebaseMessaging.instance.getInitialMessage();

  if (notificationStackMessage != null) {
    Future.delayed(const Duration(seconds: 1), () {
      _navigateToLocalNavigation(_getSendableData(notificationStackMessage));
    });
  }

  //while app is in foreground
  FirebaseMessaging.onMessage.listen(
    (RemoteMessage message) {
      final data = _getSendableData(message);
      _notifyListeners(data);
      if (message.notification != null &&
          message.notification?.android != null) {
        notificationManager.displayPopup(
          message.notification?.title ?? '',
          message.notification?.body ?? '',
          data,
          (Map<String, dynamic> data) => markNotificationAsRead(data),
        );
      }
    },
  );

  //while app is in background state
  FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
    final data = _getSendableData(message);
    _navigateToLocalNavigation(data);
  });
}