React Native 0.64.1集成Notification Extension报sharedApplication不可用错误如何解决
问题根因
当前报错是因为RichNotificationsExtension继承了外层抽象目标BrandName的所有依赖,包括React Native核心库、各类RN原生插件,这些依赖大量使用了App Extension不允许的sharedApplicationAPI,触发了编译限制。你之前将扩展target移到外部后找不到模块,是因为没有单独给扩展配置自身需要的依赖。
适配后的Podfile配置
require_relative '../node_modules/react-native/scripts/react_native_pods' require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules' platform :ios, '12.0' # 建议至少升级到12.0,兼容新版RN和Batch SDK # 原抽象目标仅给主应用相关target继承 abstract_target 'BrandName' do config = use_native_modules! use_react_native!( :path => config[:reactNativePath], :hermes_enabled => false ) permissions_path = '../node_modules/react-native-permissions/ios' pod 'Permission-Notifications', :path => "#{permissions_path}/Notifications" target 'BrandNameApp' do #Production-specific pods target 'BrandNameAppTests' do inherit! :complete # Pods for testing end end target 'BrandNameAppDevelopment' do #Development-specific pods end target 'BrandNameAppStage' do #Stage-specific pods end # Enables Flipper. use_flipper!() end # 通知扩展单独放在抽象目标外部,不继承主应用的RN相关依赖 target 'RichNotificationsExtension' do # 仅添加扩展自身需要的依赖 pod 'BatchExtension-ObjC' platform :ios, '12.0' end post_install do |installer| react_native_post_install(installer) # 修正扩展target的编译配置 installer.pods_project.targets.each do |target| if target.name == "RichNotificationsExtension" target.build_configurations.each do |config| # 保持扩展安全API校验开启,无需改为NO config.build_settings['APPLICATION_EXTENSION_API_ONLY'] = 'YES' # 保持系统版本和主应用一致 config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '12.0' end end end end
后续操作步骤
- 删除
ios目录下的Podfile.lock、Pods文件夹,以及RichNotificationsExtension目录下自动生成的pod配置文件 - 执行
pod install --repo-update重新安装依赖 - 打开
.xcworkspace文件,确认RichNotificationsExtension的Build Phases中没有React Native相关的编译项 - 检查扩展的
Require Only App-extension-safe API配置保持默认YES即可
注意事项
不要给通知扩展添加任何React Native相关的依赖,扩展本身不需要加载RN运行时,仅需处理通知展示逻辑,Batch和主应用的联动配置在主应用target中完成即可。
内容的提问来源于stack exchange,提问作者Melanie Journe
相关产品推荐
相关产品推荐

