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

How to check app state change from background to foreground with click remote notification #399

Closed
seo2im opened this issue Mar 23, 2023 · 2 comments

Comments

@seo2im
Copy link

seo2im commented Mar 23, 2023

I want to know app state change when click remote push because go to other navigation with notification data(dynamiclink)

'notification' event is fired only push received anytime app is background and foreground, not open.

getInitialNotification() get only notification info when app open after closed, not background state

this my AppDelegate.m

#import "AppDelegate.h"
#import <UserNotifications/UserNotifications.h>
#import <RNCPushNotificationIOS.h>

#import <React/RCTLinkingManager.h>
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
// #import <FBSDKCoreKit/FBSDKCoreKit-swift.h>
#import <FBSDKCoreKit/FBSDKCoreKit.h>

#import <Firebase.h>

#import <AppCenterReactNative.h>
#import <AppCenterReactNativeAnalytics.h>
#import <AppCenterReactNativeCrashes.h>
#import <CodePush/CodePush.h>

#import <ChannelIOFront/ChannelIOFront-swift.h>

#ifdef FB_SONARKIT_ENABLED
#import <FlipperKit/FlipperClient.h>
#import <FlipperKitLayoutPlugin/FlipperKitLayoutPlugin.h>
#import <FlipperKitUserDefaultsPlugin/FKUserDefaultsPlugin.h>
#import <FlipperKitNetworkPlugin/FlipperKitNetworkPlugin.h>
#import <SKIOSNetworkPlugin/SKIOSNetworkAdapter.h>
#import <FlipperKitReactPlugin/FlipperKitReactPlugin.h>

...

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  ...
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  center.delegate = self;

  return YES;
}

...

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
 [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  NSLog(@"Recive Clicked Push Noti?????");
  [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
 [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
}
// Required for localNotification event
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
  NSLog(@"Clicked Push Noti?????");
  [RNCPushNotificationIOS didReceiveNotificationResponse:response];
}

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  NSLog(@"foregroundWork?");
  completionHandler(UNNotificationPresentationOptionBadge);
}

@end

and my NotificationServiceContaier

const Container = () => {

  ...
  const apnsOpenHandler = async () => {
    const initial = await PushNotificationIOS.getInitialNotification();
    const data = initial?.getData();

    if (data && data.userInteraction === 1) {
     navigation.navigate('some page');
    }
  };

  const apnsNotificationHandler = async (notification: PushNotification) => {
    const data = notification?.getData();
    if (data) {
      if (AppState.currentState === 'active') {
           // show in app custom notify
      } else if (data.userInteraction === 1){
          navigation.navigate('some page')
      }
    }
    const result = PushNotificationIOS.FetchResult.NoData;
    notification.finish(result);
  };

  useEffect(() => {
    if (Platform.OS === 'ios') {
      PushNotificationIOS.addEventListener('notification', apnsNotificationHandler);
      apnsOpenHandlerSendbird();
    }

    return () => {
      PushNotificationIOS.removeEventListener('notification');
    };
  }, []);
  
  ...
}

Please let me way if you know.

@seo2im seo2im closed this as completed Mar 24, 2023
@rdick
Copy link

rdick commented Apr 3, 2023

Did this work?

@bang9
Copy link

bang9 commented Mar 16, 2024

@rdick
The issue with didReceiveRemoteNotification/didReceiveNotificationResponse not working was a bug in Notifee
invertase/notifee#925

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants