React-native-firebase: iOS Background Notifications are not working

Created on 27 May 2020  路  20Comments  路  Source: invertase/react-native-firebase

Issue

Describe your issue here

  • iOS Background Notifications are not working. But It's working without any issue in the Foreground.

I'm sending this from the backend

{
 "to" : "***FCM_TOKEN****",
 "collapse_key" : "type_a",
 "priority":"high",
 "apns":{
      "headers":{
         "apns-priority":"5",
         "apns-push-type":"background"
      },
      "payload":{
         "aps":{
            "content-available":1
         }
      }
   },
 "notification": {
    "title": "$GOOG up 1.43% on the day",
    "body": "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day."
  },
  "data":{
    "type":"MESSAGE",
    "meta-data":{
       "client-message-id":"bab1793c-d821-4d3f-bfa8-4fce6be0dd71",
       "read-info":{"f79ca397-5bbb-4b66-a82b-0dfd49bfcc89":true},
       "posted-by":"f79ca397-5bbb-4b66-a82b-0dfd49bfcc89",
       "message":"abbh",
       "thread-id":"96354d48-62ee-4b84-838c-59e289a8c417"
    }
  }
}

Project Files






Javascript

Click To Expand

#### `package.json`:

{
  "name": "go_app",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "@react-native-community/async-storage": "^1.7.1",
    "@react-native-community/geolocation": "^2.0.2",
    "@react-native-firebase/analytics": "^7.1.0",
    "@react-native-firebase/app": "^7.1.0",
    "@react-native-firebase/crashlytics": "^7.1.0",
    "@react-native-firebase/database": "^7.1.0",
    "@react-native-firebase/messaging": "^7.1.0",
    "axios": "^0.19.2",
    "libphonenumber-js": "^1.7.50",
    "lodash": "^4.17.15",
    "moment": "^2.25.3",
    "react": "16.9.0",
    "react-native": "^0.61.5",
    "react-native-action-button": "^2.8.5",
    "react-native-animatable": "^1.3.3",
    "react-native-device-info": "^5.5.3",
    "react-native-elements": "^2.0.0",
    "react-native-flash-message": "^0.1.15",
    "react-native-gesture-handler": "^1.5.3",
    "react-native-gifted-chat": "^0.16.1",
    "react-native-image-crop-picker": "^0.28.0",
    "react-native-keyboard-spacer": "^0.4.1",
    "react-native-linear-gradient": "^2.5.6",
    "react-native-loading-spinner-overlay": "^1.0.1",
    "react-native-location": "^2.5.0",
    "react-native-maps": "0.26.1",
    "react-native-masked-text": "^1.13.0",
    "react-native-material-ripple": "^0.9.1",
    "react-native-modal": "^11.5.3",
    "react-native-permissions": "^2.0.9",
    "react-native-progress": "^4.0.3",
    "react-native-push-notification-popup": "^1.4.0",
    "react-native-reanimated": "^1.7.0",
    "react-native-router-flux": "^4.2.0",
    "react-native-screens": "^2.4.0",
    "react-native-search-filter": "^0.1.5",
    "react-native-switches": "^1.2.5",
    "react-native-vector-icons": "^6.6.0",
    "react-native-webview": "^9.4.0",
    "react-redux": "^7.1.3",
    "redux": "^4.0.5",
    "redux-logger": "^3.0.6",
    "redux-persist": "^6.0.0",
    "redux-thunk": "^2.3.0"
  },
  "devDependencies": {
    "@babel/core": "^7.8.3",
    "@babel/runtime": "^7.8.3",
    "@bam.tech/react-native-make": "^1.0.3",
    "@react-native-community/eslint-config": "^0.0.6",
    "babel-jest": "^24.9.0",
    "eslint": "^6.8.0",
    "jest": "^24.9.0",
    "metro-react-native-babel-preset": "^0.58.0",
    "react-native-clean-project": "^3.3.0",
    "react-test-renderer": "16.9.0"
  },
  "jest": {
    "preset": "react-native"
  }
}

#### `firebase.json` for react-native-firebase v6:
{
    "react-native": {
        "messaging_ios_auto_register_for_remote_messages": true,
        "crashlytics_debug_enabled": true,
        "analytics_auto_collection_enabled": false
    }
}
### iOS
Click To Expand

#### `ios/Podfile`: - [ ] I'm not using Pods - [x] I'm using Pods and my Podfile looks like:

#$RNFirebaseAsStaticFramework = true

platform :ios, '9.0'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'

target 'go_app' do
  # Pods for go_app
  pod 'FBLazyVector', :path => "../node_modules/react-native/Libraries/FBLazyVector"
  pod 'FBReactNativeSpec', :path => "../node_modules/react-native/Libraries/FBReactNativeSpec"
  pod 'RCTRequired', :path => "../node_modules/react-native/Libraries/RCTRequired"
  pod 'RCTTypeSafety', :path => "../node_modules/react-native/Libraries/TypeSafety"
  pod 'React', :path => '../node_modules/react-native/'
  pod 'React-Core', :path => '../node_modules/react-native/'
  pod 'React-CoreModules', :path => '../node_modules/react-native/React/CoreModules'
  pod 'React-Core/DevSupport', :path => '../node_modules/react-native/'
  pod 'React-RCTActionSheet', :path => '../node_modules/react-native/Libraries/ActionSheetIOS'
  pod 'React-RCTAnimation', :path => '../node_modules/react-native/Libraries/NativeAnimation'
  pod 'React-RCTBlob', :path => '../node_modules/react-native/Libraries/Blob'
  pod 'React-RCTImage', :path => '../node_modules/react-native/Libraries/Image'
  pod 'React-RCTLinking', :path => '../node_modules/react-native/Libraries/LinkingIOS'
  pod 'React-RCTNetwork', :path => '../node_modules/react-native/Libraries/Network'
  pod 'React-RCTSettings', :path => '../node_modules/react-native/Libraries/Settings'
  pod 'React-RCTText', :path => '../node_modules/react-native/Libraries/Text'
  pod 'React-RCTVibration', :path => '../node_modules/react-native/Libraries/Vibration'
  pod 'React-Core/RCTWebSocket', :path => '../node_modules/react-native/'

  pod 'React-cxxreact', :path => '../node_modules/react-native/ReactCommon/cxxreact'
  pod 'React-jsi', :path => '../node_modules/react-native/ReactCommon/jsi'
  pod 'React-jsiexecutor', :path => '../node_modules/react-native/ReactCommon/jsiexecutor'
  pod 'React-jsinspector', :path => '../node_modules/react-native/ReactCommon/jsinspector'
  pod 'ReactCommon/jscallinvoker', :path => "../node_modules/react-native/ReactCommon"
  #pod 'ReactCommon/callinvoker', :path => "../node_modules/react-native/ReactCommon"
  pod 'ReactCommon/turbomodule/core', :path => "../node_modules/react-native/ReactCommon"
  pod 'Yoga', :path => '../node_modules/react-native/ReactCommon/yoga'

  pod 'DoubleConversion', :podspec => '../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
  pod 'glog', :podspec => '../node_modules/react-native/third-party-podspecs/glog.podspec'
  pod 'Folly', :podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec'


  #permissions
  permissions_path = '../node_modules/react-native-permissions/ios'

  pod 'Permission-LocationAlways', :path => "#{permissions_path}/LocationAlways.podspec"
  pod 'Permission-LocationWhenInUse', :path => "#{permissions_path}/LocationWhenInUse.podspec"



 # React Native Maps dependencies
 rn_maps_path = '../node_modules/react-native-maps'
 pod 'react-native-google-maps', :path => rn_maps_path
 pod 'GoogleMaps'
 pod 'Google-Maps-iOS-Utils'



 pod 'react-native-location', :path => '../node_modules/react-native-location/react-native-location.podspec'


  target 'go_appTests' do
    inherit! :search_paths
    # Pods for testing
  end

  use_native_modules!
end

target 'go_app-tvOS' do
  # Pods for go_app-tvOS

  target 'go_app-tvOSTests' do
    inherit! :search_paths
    # Pods for testing
  end

end

#### `AppDelegate.m`:
/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */


#import "AppDelegate.h"

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <Firebase.h>
#import <FirebaseMessaging.h>
#import <GoogleMaps/GoogleMaps.h>



@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [FIRApp configure];

  [GMSServices provideAPIKey:@"AIzaSyD-avfbTwckgBwFq9-h8-DVc0j4WJubvm4"];


   for (NSString* family in [UIFont familyNames]) { NSLog(@"%@", family); for (NSString* name in [UIFont fontNamesForFamilyName: family]) { NSLog(@" %@", name); } }

  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"go_tradie"
                                            initialProperties:nil];

  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];




  return YES;
}







- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}





@end


Environment

Click To Expand

**`react-native info` output:**

System:
    OS: macOS 10.15.4
    CPU: (4) x64 Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz
    Memory: 377.73 MB / 8.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 12.14.0 - /usr/local/bin/node
    Yarn: 1.17.3 - /usr/local/bin/yarn
    npm: 6.13.4 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
    Android SDK:
      API Levels: 22, 23, 24, 25, 26, 27, 28
      Build Tools: 23.0.1, 25.0.0, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.2, 27.0.3, 28.0.2, 28.0.3
      System Images: android-28 | Google Play Intel x86 Atom_64
  IDEs:
    Android Studio: 3.4 AI-183.6156.11.34.5692245
    Xcode: 11.2.1/11B53 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.9.0 => 16.9.0 
    react-native: ^0.61.5 => 0.61.5 
  npmGlobalPackages:
    react-native-cli: 2.0.1
    react-native-rename: 2.4.1
- **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:** - `7.1.0` - **`Firebase` module(s) you're using that has the issue:** - `"@react-native-firebase/messaging": "^7.1.0"` - **Are you using `TypeScript`?** - `N`

Most helpful comment

Hello guys, I have the same issue, the way I make it works is restart the phone or the Macbook. It sounds silly but it works.

All 20 comments

+1 same here

When adding onMessage() method in the foreground it works fine, but when the app is closed or in the background, messages are not receiving

Same here. I think setBackgroundMessageHandler func is not working with IOS device, Android is ok

@15110011 Yh same here.

weird, it worked today

@15110011 How is it work? what is your version RNF?

    "@react-native-firebase/app": "^7.1.0",
    "@react-native-firebase/messaging": "^7.1.0",
    "react-native-push-notification": "^3.5.1",
    "@react-native-community/push-notification-ios": "^1.2.0",

here bro

@15110011 Have you added FirebaseAppDelegateProxyEnabled to info.plst? And are you getting notifications on background in both dev and release builds?

Hello guys, I have the same issue, the way I make it works is restart the phone or the Macbook. It sounds silly but it works.

@Buwaneka-Sumanasekara No, didn't. I tested on dev build and on TestFlight build. I think release build will work as well

@dilipchandima , @15110011 can you guys share your firebase.json, info.plst here?

i don't have any firebase.json in my project
and here is my info.list

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleDisplayName</key>
    <string>Verofax</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>localhost</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
            </dict>
        </dict>
    </dict>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string/>
    <key>UIAppFonts</key>
    <array>
        <string>AntDesign.ttf</string>
        <string>Entypo.ttf</string>
        <string>EvilIcons.ttf</string>
        <string>Feather.ttf</string>
        <string>FontAwesome.ttf</string>
        <string>FontAwesome5_Brands.ttf</string>
        <string>FontAwesome5_Regular.ttf</string>
        <string>FontAwesome5_Solid.ttf</string>
        <string>Foundation.ttf</string>
        <string>Ionicons.ttf</string>
        <string>MaterialIcons.ttf</string>
        <string>MaterialCommunityIcons.ttf</string>
        <string>SimpleLineIcons.ttf</string>
        <string>Octicons.ttf</string>
        <string>Zocial.ttf</string>
        <string>Roboto-Black.ttf</string>
        <string>Roboto-BlackItalic.ttf</string>
        <string>Roboto-Bold.ttf</string>
        <string>Roboto-BoldItalic.ttf</string>
        <string>Roboto-Italic.ttf</string>
        <string>Roboto-Light.ttf</string>
        <string>Roboto-LightItalic.ttf</string>
        <string>Roboto-Medium.ttf</string>
        <string>Roboto-MediumItalic.ttf</string>
        <string>Roboto-Regular.ttf</string>
        <string>Roboto-Thin.ttf</string>
        <string>Roboto-ThinItalic.ttf</string>
        <string>Fontisto.ttf</string>
    </array>
    <key>UIBackgroundModes</key>
    <array>
        <string>fetch</string>
        <string>remote-notification</string>
    </array>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>armv7</string>
    </array>
    <key>UIStatusBarStyle</key>
    <string>UIStatusBarStyleLightContent</string>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
    </array>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
</dict>
</plist>

About the config I just followed this instruction video, maybe it could help you a lot
https://www.youtube.com/watch?v=dyAwv9HLS60

@15110011 Thank you :) finally, its worked. Thank you so much :) :)

how did you fix the issue @Buwaneka-Sumanasekara

@dilipchandima I watched this video and followed it. Thanks to @15110011

https://www.youtube.com/watch?v=dyAwv9HLS60

I have this problem, I watched this video but the background not work for me on ios, Only foreground, anyone can help me?

Have to done any change in info.plst, firebase.json ?

Same issue here.

Worked after change NSAppTransportSecurity on info.plist to:


NSAllowsArbitraryLoads

NSExceptionDomains

localhost

NSExceptionAllowsInsecureHTTPLoads



before it was restricted to some urls

Edit: I resolved using thehappydinoa's solution on https://github.com/invertase/react-native-firebase/issues/3367#issuecomment-628963057.

Hello guys, I have the same issue, the way I make it works is restart the phone or the Macbook. It sounds silly but it works.
@LuongTruong Omg... Wow... I can't believe it... I was skeptical about your word at first but your way solved my problem like magic...!!! I spent two days for solving the problem of background notification and you saved my life @.@! Thank you so much :)

I got this working on iOS 13 and 14 by changing the p8 APN auth certificate on firebase console cloud messaging to p12 certificate generated from the Push Notifications feature under the app's provisioning profile in the apple developer account

Was this page helpful?
0 / 5 - 0 ratings