OneSignal / react-native-onesignal

React Native Library for OneSignal Push Notifications Service
Other
1.57k stars 373 forks source link

iOS checkIfInitialized() always return false #1202

Closed haorh closed 3 years ago

haorh commented 3 years ago

Description: iOS one signal never initialized but work fine in Android. checkIfInitialized() in react-native-onesignal's index.js always return false. In other word, NativeModules.OneSignal was not initialized on iOS. I understand that push notification does not work at simulator but the user will still show up at one signal dashboard. However, this didnt happen anymore starting a few months ago.

Environment react-native: 0.59.9 react-native-onesignal: 3.2.12 xCode: 12.4 Main project deployment target level: 9.0 OneSignal deployment target level: 10.0

Pod

# Uncomment the next line to define a global platform for your project - default is 8.0
platform :ios, '9.0'

# Reference to the React in node_module instead of generating a new one under Pod via pod install
pod 'React', :path => '../node_modules/react-native', :subspecs => [
   'CxxBridge',
    'DevSupport',
    'RCTImage', 
]

target 'Main' do
  pod 'XCDYouTubeKit', '~> 2.5'
  pod 'react-native-camera', :path => '../node_modules/react-native-camera'
  pod 'react-native-add-calendar-event', :path => '../node_modules/react-native-add-calendar-event'
  pod 'react-native-contacts', :path => '../node_modules/react-native-contacts'
  pod 'RCTRestart', :path => '../node_modules/react-native-restart/ios'
  pod 'ReactNativeExceptionHandler', :podspec => '../node_modules/react-native-exception-handler/ReactNativeExceptionHandler.podspec'
  pod 'react-native-webview', :path => '../node_modules/react-native-webview'
  pod 'RNCAsyncStorage', :podspec => '../node_modules/@react-native-community/async-storage/RNCAsyncStorage.podspec'
  pod 'react-native-orientation-locker', :path => '../node_modules/react-native-orientation-locker'
  pod 'BugsnagReactNative', :podspec => '../node_modules/bugsnag-react-native/BugsnagReactNative.podspec'
  pod 'RNVectorIcons', :path => '../node_modules/react-native-vector-icons/'
  pod 'react-native-youtube', :path => '../node_modules/react-native-youtube/'
  pod 'ReactNativePermissions', :path => '../node_modules/react-native-permissions/'
  pod 'react-native-image-resizer', :path => '../node_modules/react-native-image-resizer/'
  pod 'react-native-safe-area', :path => '../node_modules/react-native-safe-area/'
  pod 'CodePush', :path => '../node_modules/react-native-code-push'
  pod 'appcenter', :path => '../node_modules/appcenter'
  pod 'appcenter-analytics', :path => '../node_modules/appcenter-analytics'
  pod 'appcenter-crashes', :path => '../node_modules/appcenter-crashes'
  pod 'rn-fetch-blob', :path => '../node_modules/rn-fetch-blob'
  pod 'RNDeviceInfo', :path => '../node_modules/react-native-device-info'
    pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'

    ## Third party deps podspec link
    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'
end

target 'OneSignal' do
  pod 'OneSignal', '>= 2.9.3', '< 3.0'
end

post_install do |installer|
    installer.pods_project.targets.each do |target|
      targets_to_ignore = %w(React yoga)

      if targets_to_ignore.include? target.name
        target.remove_from_project
      end

      target.build_configurations.each do |config|
        config.build_settings["ONLY_ACTIVE_ARCH"] = "YES" # for xcode 12 Archive
        config.build_settings['APPLICATION_EXTENSION_API_ONLY'] = 'No' # for one signal cocoapod
      end
    end

    installer.pods_project.build_configurations.each do |config|
      config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "armv7"
    end
end

Main Target -> Build Phase -> Link Binary with Libraries

-- didnt add one signal .a file

Main Target -> Build Settings -> Search Paths -> FRAMEWORK_SEARCH_PATHS

$(inherited)
"$(PODS_ROOT)/OneSignal/iOS_SDK/OneSignalSDK/Framework"  [non-recursive]

Main Target -> Build Settings -> Search Paths -> HEADER_SEARCH_PATHS

$(inherited)
$(SRCROOT)

OneSignal Target -> Build Phase -> Link Binary with Libraries

UiKit.framework
SystemConfiguration.framework
libPods-OneSignal.a

OneSignal Target -> Build Settings -> Search Paths -> FRAMEWORK_SEARCH_PATHS "$(PODS_ROOT)/OneSignal/iOS_SDK/OneSignalSDK/Framework" [non-recursive]

OneSignal Target -> Build Settings -> Search Paths -> HEADER_SEARCH_PATHS

--empty

Anything else: I have tried:

  1. Install react-native-onesignal@3.6.5 with pod 'OneSignal', '>= 3.0', '< 4.0'
  2. Set iOS deployment level to 10 for main target with pod platform :ios, '10.0'
  3. Manual linking
  4. App Group
  5. Remote debug on real device/release on test flight

I still cant see user at dashboard. With v3.6.5, it works on Android but not iOS. My project was initially using manual linking, but to solve this issue (also ready to migrate to RN0.6x), I'm changing to use cocoapod so there may be some residue of manual linking config left in my xcode.

I have no log to show because OneSignal.setLogLevel(6, 0); return; due to if (!checkIfInitialized()) return; I hope to remain at react-native 0.59.9 for now.

rgomezp commented 3 years ago

Howdy, Can you please upgrade to the latest version of the SDK?

haorh commented 3 years ago

I see going to version 4.x.x require RN0.6x. I cannot afford to upgrade to RN0.6x now as i would need to check ALL my dependencies, which I know a lot will break. I have tried upgrade to react-native-onesignal@3.9.3 and placed console.log at checkIfInitialized(), its the same result (works on android). I think its something wrong related to my xcode/cocoapod. Based on podlock, the installed onesignal version is 3.4.1

haorh commented 3 years ago

If i follow iOS SDK setup and place OneSignal initWithLaunchOptions:launchOptions appId:@"xxxxx"]; at AppDelegate.m, onesignal dashboard shows iOS Simulator Unsupported. So I assume this means my one signal files are at correct place, its just that the linkage to JS is broken as NativeModules.OneSignal return null. How does one-signal exports the native function to JS side? I cannot find in RCT_EXPORT_MODULE within the files under RCTOneSignal folder. nvm its at RCTOnesignalEventEmitter.m

My AppDelegate.m

#import "AppDelegate.h"
#import <OneSignal/OneSignal.h> <-- this works
#import <CodePush/CodePush.h>
#import "Orientation.h"

#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <AVFoundation/AVFoundation.h>
#import <BugsnagReactNative/BugsnagReactNative.h>
#import <Bugsnag.h>
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

  [BugsnagReactNative start];
  NSURL *jsCodeLocation;
// - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
// {
#ifdef DEBUG
  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
  NSLog(@"-[XH] DEBUG %@", [jsCodeLocation path]);
#else
  jsCodeLocation = [CodePush bundleURL];
  NSLog(@"-[XH] PRODUCTION %@", [jsCodeLocation path]);
#endif
// }

  [OneSignal setLogLevel:ONE_S_LL_NONE visualLevel:ONE_S_LL_NONE]; <-- this works
  [OneSignal initWithLaunchOptions:launchOptions appId:@"xxxxxxxx"];

   RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"Main"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  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];

  [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: nil];
  return YES;
}
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
  return [Orientation getOrientation];
}
@end
rgomezp commented 3 years ago

Howdy, Sorry for the delayed response.

We no longer support manual linking. If you would like to support it, you can do so by following the instructions here. You will need to copy the binaries over from the native SDK.

haorh commented 3 years ago

I have tried both method and didnt work initially. Now I'm using cocoapod way and I had to implement my own .m class to export OneSignal function to js side with RCT_EXTERN_METHOD.

rgomezp commented 3 years ago

Glad to hear it's working now. Cheers