invertase / react-native-firebase

🔥 A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
https://rnfirebase.io
Other
11.65k stars 2.21k forks source link

[🐛] onMessage is not triggering on iOS when app is in FOREGROUND #7516

Closed MSetnik closed 7 months ago

MSetnik commented 9 months ago

Issue

Describe your issue here

Im having problem where onMessage function is not being triggered when iOS app is in foreground. On android, everything works fine. Im sending notifications over APNs via server in c# and everything works except onMessage handler.

my notification payload looks like this:

User pressed notification : when notification is pressed and logged in onBackgroundHandler.


{
   "body":"title",
   "data":{
      "content_available":true,
      "data":{
         "employeeId":"id_employee",
         "itemId":"item_id",
         "message":"",
         "notificationGroup":"group",
         "notificationTag":"tag",
         "notificationType":"type",
         "status":"status"
      }
   },
   "id":"id",
   "ios":{
      "categoryId":"",
      "launchImageName":"",
      "threadId":""
   },
   "remote":{

   },
   "title": "title"
 }

Project Files

Javascript

  useEffect(() => {
    getNotif()

    const notifeeForegroundEvent = notifee.onForegroundEvent(({ type, detail }) => {
      const notificationData = detail.notification?.data
      switch (type) {
        case EventType.DISMISSED:
          console.log('User dismissed notification', detail.notification)
          break
        case EventType.PRESS:
          console.log('User pressed notification', detail.notification)
          //  @ts-ignore
          navigation.navigate('Notifications', { notificationItemId: notificationData?.data?.itemId, notificationGroup: notificationData?.data?.notificationGroup })
          break
      }
    })

      const unsubscribe = messaging().onMessage(async (remoteMessage: any) => {
      console.log(remoteMessage)
      alert(JSON.stringify(remoteMessage))
      const data = remoteMessage.data
      onDisplayNotification(
        remoteMessage.notification?.title || '',
        remoteMessage.notification?.body || '',
        data.itemId,
        data.notificationGroup
      )
    })

    return () => {
      unsubscribe()
      notifeeForegroundEvent()
    }
  }, [])

package.json:

{
  "name": "wavyapp",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios --simulator='iPhone 15 Pro'",
    "start": "react-native start -- clear-cache",
    "test": "jest",
    "tsCheck": "tsc --noEmit",
    "lintCheck": "eslint src/** && npm run tsCheck",
    "lintFix": "eslint --fix src/**",
    "check-dependencies": "rnx-align-deps"
  },
  "dependencies": {
    "@notifee/react-native": "^7.8.0",
    "@react-native-async-storage/async-storage": "^1.17.7",
    "@react-native-firebase/app": "14.12.0",
    "@react-native-firebase/messaging": "^14.12.0",
    "@react-native/normalize-color": "^2.1.0",
    "@react-navigation/native": "^6.0.6",
    "@react-navigation/stack": "^6.0.11",
    "@shopify/flash-list": "^1.2.2",
    "axios": "^0.27.2",
    "deep-diff": "^1.0.2",
    "formik": "^2.2.9",
    "lodash.debounce": "^4.0.8",
    "lodash.uniqueid": "^4.0.1",
    "moment": "^2.29.4",
    "native-base": "^3.0.6",
    "qs": "^6.10.3",
    "react": "17.0.2",
    "react-native": "0.66.0",
    "react-native-azure-blob-storage": "^2.0.3",
    "react-native-blob-util": "^0.16.4",
    "react-native-calendars": "^1.1284.0",
    "react-native-collapsible": "^1.6.0",
    "react-native-date-picker": "^4.2.2",
    "react-native-device-info": "^9.0.2",
    "react-native-document-picker": "^8.2.1",
    "react-native-file-viewer": "^2.1.5",
    "react-native-gesture-handler": "^2.6.0",
    "react-native-gifted-charts": "^1.3.9",
    "react-native-image-picker": "^7.1.0",
    "react-native-keyboard-manager": "^6.5.4-4",
    "react-native-linear-gradient": "^2.8.3",
    "react-native-month-year-picker": "^1.8.1",
    "react-native-msal": "^4.0.4",
    "react-native-notifications": "^5.0.0",
    "react-native-pdf": "^6.7.1",
    "react-native-permissions": "^3.10.1",
    "react-native-reanimated": "^2.17.0",
    "react-native-reanimated-carousel": "^3.5.1",
    "react-native-safe-area-context": "^3.2.0",
    "react-native-screens": "^3.4.0",
    "react-native-splash-screen": "^3.3.0",
    "react-native-svg": "^12.5.1",
    "react-native-video": "5.1.1",
    "react-query": "^3.39.1",
    "styled-components": "^5.3.0",
    "styled-system": "^5.1.5",
    "yup": "^0.32.11"
  },
  "devDependencies": {
    "@babel/core": "^7.18.6",
    "@babel/runtime": "^7.18.6",
    "@react-native-community/eslint-config": "^3.0.3",
    "@testing-library/react-native": "^10.1.1",
    "@types/deep-diff": "^1.0.1",
    "@types/jest": "^28.1.4",
    "@types/lodash.debounce": "^4.0.7",
    "@types/lodash.uniqueid": "^4.0.7",
    "@types/qs": "^6.9.7",
    "@types/react-native": "^0.64.5",
    "@types/react-native-calendars": "^1.1264.3",
    "@types/react-native-video": "^5.0.14",
    "@types/react-test-renderer": "^18.0.0",
    "@typescript-eslint/eslint-plugin": "^5.59.6",
    "@typescript-eslint/parser": "^5.59.6",
    "babel-jest": "^28.1.2",
    "babel-plugin-root-import": "^6.6.0",
    "babel-plugin-transform-remove-console": "^6.9.4",
    "eslint": "^8.40.0",
    "eslint-config-standard": "^17.0.0",
    "eslint-plugin-import": "^2.27.5",
    "eslint-plugin-n": "^15.7.0",
    "eslint-plugin-promise": "^6.1.1",
    "eslint-plugin-react": "^7.32.2",
    "jest": "^28.1.2",
    "metro-react-native-babel-preset": "^0.66.2",
    "prettier": "^2.7.1",
    "react-test-renderer": "17.0.2",
    "typescript": "^4.7.4"
  },
  "resolutions": {
    "@types/react": "^17"
  },
  "jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ]
  },
  "lint-staged": {
    "*.tsx": [
      "aa",
      "lintCheck",
      "prettier --write"
    ]
  }
}

firebase.json for react-native-firebase v6:

# N/A

iOS

Click To Expand

#### `ios/Podfile`: - [ ] I'm not using Pods - [x] I'm using Pods and my Podfile looks like: require_relative '../node_modules/react-native/scripts/react_native_pods' require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules' require_relative '../node_modules/react-native-permissions/scripts/setup' platform :ios, '11.0' setup_permissions([ # 'AppTrackingTransparency', # 'BluetoothPeripheral', # 'Calendars', # 'Camera', # 'Contacts', # 'FaceID', # 'LocationAccuracy', # 'LocationAlways', # 'LocationWhenInUse', # 'MediaLibrary', # 'Microphone', # 'Motion', 'Notifications', # 'PhotoLibrary', # 'PhotoLibraryAddOnly', # 'Reminders', # 'Siri', # 'SpeechRecognition', # 'StoreKit', ]) target 'myapp' do config = use_native_modules! use_react_native!( :path => config[:reactNativePath], # to enable hermes on iOS, change `false` to `true` and then install pods # :hermes_enabled => false :hermes_enabled => true ) pod 'react-native-azure-blob-storage', :path => '../node_modules/react-native-azure-blob-storage' # pod 'rn-fetch-blob', :path => '../node_modules/rn-fetch-blob' target 'myappTests' do inherit! :complete # Pods for testing end # Enables Flipper. # # Note that if you have use_frameworks! enabled, Flipper will not work and # you should disable the next line. # use_flipper!() post_install do |installer| react_native_post_install(installer) __apply_Xcode_12_5_M1_post_install_workaround(installer) end end ```ruby # N/A ``` #### `AppDelegate.m`: #import "AppDelegate.h" #import #import "RNNotifications.h" #import #import #import #import #import "RNSplashScreen.h" // RN SPLASH SCREEN #ifdef FB_SONARKIT_ENABLED #import #import #import #import #import #import static void InitializeFlipper(UIApplication *application) { FlipperClient *client = [FlipperClient sharedClient]; SKDescriptorMapper *layoutDescriptorMapper = [[SKDescriptorMapper alloc] initWithDefaults]; [client addPlugin:[[FlipperKitLayoutPlugin alloc] initWithRootNode:application withDescriptorMapper:layoutDescriptorMapper]]; [client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]]; [client addPlugin:[FlipperKitReactPlugin new]]; [client addPlugin:[[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]]; [client start]; } #endif @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [FIRApp configure]; #ifdef FB_SONARKIT_ENABLED InitializeFlipper(application); #endif [RNNotifications startMonitorNotifications]; RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"wavyapp" initialProperties:nil]; if (@available(iOS 13.0, *)) { rootView.backgroundColor = [UIColor systemBackgroundColor]; } else { rootView.backgroundColor = [UIColor whiteColor]; } self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; UIViewController *rootViewController = [UIViewController new]; rootViewController.view = rootView; self.window.rootViewController = rootViewController; [self.window makeKeyAndVisible]; UIStoryboard *sb = [UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil]; UIViewController *vc = [sb instantiateInitialViewController]; rootView.loadingView = vc.view; [RNSplashScreen show]; return YES; } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [RNNotifications didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { [RNNotifications didFailToRegisterForRemoteNotificationsWithError:error]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { [RNNotifications didReceiveBackgroundNotification:userInfo withCompletionHandler:completionHandler]; } - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge { #if DEBUG return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil]; #else return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"]; #endif } - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options { return [MSALPublicClientApplication handleMSALResponse:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]]; } @end ```objc // N/A ```


Android

Click To Expand

#### Have you converted to AndroidX? - [ ] my application is an AndroidX application? - [ ] I am using `android/gradle.settings` `jetifier=true` for Android compatibility? - [ ] I am using the NPM package `jetifier` for react-native compatibility? #### `android/build.gradle`: ```groovy // N/A ``` #### `android/app/build.gradle`: ```groovy // N/A ``` #### `android/settings.gradle`: ```groovy // N/A ``` #### `MainApplication.java`: ```java // N/A ``` #### `AndroidManifest.xml`: ```xml ```


Environment

Click To Expand

**`react-native info` output:** ``` OUTPUT GOES HERE ``` - **Platform that you're experiencing the issue on**: - [x ] iOS - [ ] Android - [ ] **iOS** but have not tested behavior on Android - [ ] **Android** but have not tested behavior on iOS - [ ] Both - **`react-native-firebase` version you're using that has this issue:** - `v14.12.0` - **`Firebase` module(s) you're using that has the issue:** - `app & messaging` - **Are you using `TypeScript`?** - `Y v4.7.4`


waqaramjad commented 9 months ago

Any update i am facing same issue, it was work fine previously

MSetnik commented 9 months ago

Any update i am facing same issue, it was work fine previously

Unfortunatly no. I have made a frankenstein using another notifications plugin (react-native-notifications) and used their foreground function for iOS only.

github-actions[bot] commented 8 months ago

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.

ChaossAdept7 commented 7 months ago

Having the same: notifications are arriving, but none of callbacks is fired on ios

mikehardy commented 7 months ago

If you are using notifee, it may be that the notifee event handlers have taken priority