react-native-push-notification / ios

React Native Push Notification API for iOS.
MIT License
731 stars 280 forks source link

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

Closed seo2im closed 1 year ago

seo2im commented 1 year ago

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.

rdick commented 1 year ago

Did this work?

bang9 commented 3 months ago

@rdick The issue with didReceiveRemoteNotification/didReceiveNotificationResponse not working was a bug in Notifee https://github.com/invertase/notifee/issues/925