Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle notification open in a uniform way for both iOS and Android #764

Closed
Aman1706 opened this issue May 2, 2023 · 1 comment
Closed

Comments

@Aman1706
Copy link

Aman1706 commented May 2, 2023

I am receiving remote notifications via FCM. One minor issue I have is using the onForeground event to detect notification tap only works for iOS and for Android, I have to use getInitialNotification from the RNFCM package. This function does not work for iOS so I have to conditionally call both functions when handling the notification open event.
I have attached the code snipped below.

  const notificationOpenHandler = async (
    notifMessage:
      | Notification
      | undefined
      | FirebaseMessagingTypes.RemoteMessage
      | null,
  ): Promise<void> => {
    try {
      // checks whether the group id is available or not
      // then open the respective group chat screen
      if (notifMessage?.data?.group_id) {
        navigation.navigate(chatScreen, {
          groupId: String(notifMessage.data.group_id),
        });
      }
    } catch (e) {
      sentryError(
        `Unexpected error occured when opening group ${
          notifMessage?.data?.group_id
        } from notification.
         Error Details: ${(e as Error).message}`,
      );
    }
  };

// handling of notification click
useEffect(() => {
    if (Platform.OS === 'ios') {
      const foregroundEvent = notifee.onForegroundEvent(({type, detail}) => {
        const {notification} = detail;
        if (type === EventType.PRESS) {
          notificationOpenHandler(notification);
        }
      });
      const unsubscribe = notifee.onForegroundEvent(foregroundEvent);
      return unsubscribe();
    } else if (Platform.OS === 'android') {
      // Check more docs here: https://rnfirebase.io/messaging/notifications
      messaging()
        .getInitialNotification()
        .then(notificationOpenHandler)
        .catch(e =>
          sentryError(
            'Unexpected problem occured when opening app from quitted state' +
              e.message,
          ),
        );
      const unsubscribe = messaging().onNotificationOpenedApp(
        notificationOpenHandler,
      );
      return unsubscribe;
    } else {
      return;
    }
  }, []);

Although this works, is there a function which could work for both platforms? Having two separate functions being conditionally called doesn't seem very good in terms of code quality.
Thank you

@github-actions
Copy link

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant