RNFIREBASE MESSENGER 无法在 iOS 上运行,但在 Android 上运行

Posted

技术标签:

【中文标题】RNFIREBASE MESSENGER 无法在 iOS 上运行,但在 Android 上运行【英文标题】:RNFIREBASE MESSENGER not working on iOS but working on Android 【发布时间】:2021-10-27 13:11:39 【问题描述】:

推送通知适用于 android,但不适用于 ios。我已经在设备上进行了测试,并通过了测试飞行,因为不支持模拟器

我已完成 iOS 所需的额外设置。

以下是我的 package.json 文件中的相关信息

包.json

  "name": "####",
  "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": "*",
    "@react-native-community/masked-view": "*",
    "@react-native-firebase/analytics": "*",
    "@react-native-firebase/app": "*",
    "@react-native-firebase/auth": "*",
    "@react-native-firebase/firestore": "*",
    "@react-native-firebase/messaging": "*",
    "prop-types": "*",
    "react": "16.13.1",
    "react-dom": "^17.0.2",
    "react-native": "0.63.4",
    "react-native-fbsdk": "*",
    "react-native-gesture-handler": "*",
    "react-native-reanimated": "^1.0.0-alpha",
    "react-native-render-html": "*",
    "react-native-safe-area-context": "*",
    "react-native-screens": "*",
    "react-navigation": "^4.4.4",
    "react-navigation-drawer": "^2.7.1",
    "react-navigation-stack": "^2.10.4",
    "react-navigation-tabs": "^2.11.1",
    "typescript": "^4.2.4"
  ,
  "devDependencies": 
    "@babel/core": "^7.8.4",
    "@babel/runtime": "^7.8.4",
    "@react-native-community/eslint-config": "^1.1.0",
    "babel-jest": "^25.1.0",
    "eslint": "^6.5.1",
    "jest": "^25.1.0",
    "metro-react-native-babel-preset": "^0.59.0",
    "react-test-renderer": "16.13.1"
  ,
  "jest": 
    "preset": "react-native"
  

包含在 index.js 中,我在其中添加了相应的 react-native-firebase 代码 sn-ps

import messaging from '@react-native-firebase/messaging';
// Register background handler
messaging().setBackgroundMessageHandler(async remoteMessage => 
  console.log('Hey');
  console.log('Message handled in the background!', remoteMessage);
);

const Applicaiton: () => React$Node = () => 
  useEffect(() => 
    console.log('RN Message Called on USEFFECT!');
    const unsubscribe = messaging().onMessage(async remoteMessage => 
      Alert.alert('A new FCM message arrived!', JSON.stringify(remoteMessage));
    );

    return unsubscribe;
  , []);


async function requestUserPermission() 
    const authStatus = await messaging().requestPermission();
    const enabled =
      authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
      authStatus === messaging.AuthorizationStatus.PROVISIONAL;
  
    if (enabled) 
      console.log('Authorization status:', authStatus);
    
  

AppDelegate.m

#import "AppDelegate.h"

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


@implementation AppDelegate

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

  
   if ([FIRApp defaultApp] == nil) 
     [FIRApp configure];
   
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"appName"
                                            initialProperties:nil];

Xcode 目标输出标记了 Firebase 的一些问题

2021-08-27 11:23:39.606662-0400 appName[69219:5588124] 8.1.0 - [Firebase/Core][I-COR000003] The default Firebase app has not yet been configured. Add `[FIRApp configure];` "(`FirebaseApp.configure()`" in Swift) to your application initialization. Read more:
2021-08-27 11:23:39.700654-0400 appName[69219:5588429] 8.1.0 - [Firebase/Messaging][I-FCM001000] FIRMessaging Remote Notifications proxy enabled, will swizzle remote notification receiver handlers. If you'd prefer to manually integrate Firebase Messaging, add "FirebaseAppDelegateProxyEnabled" to your Info.plist, and set it to NO. Follow the instructions at:
https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in_firebase_messaging
to ensure proper integration.
2021-08-27 11:23:39.706694-0400 appName[69219:5588429] 8.1.0 - [Firebase/Analytics][I-ACS023007] Analytics v.8.1.0 started
2021-08-27 11:23:39.751967-0400 appName[69219:5588429] 8.1.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see )
2021-08-27 11:23:39.799721-0400 appName[69219:5578599] [native] Running application appName (
    initialProps =     
    ;
    rootTag = 1;
)

签名和证书

【问题讨论】:

【参考方案1】:

ios 上的 Firebase 消息传递需要更多配置,

首先,您需要在 AppDelegate.m 中添加此导入: #import "RNFBMessagingModule.h"

然后在这一行之后:RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];

添加这一行:NSDictionary *appProperties = [RNFBMessagingModule addCustomPropsToUserProps:nil withLaunchOptions:launchOptions];

那么您需要将 RCTRootView 中的 initialPropertiesnil 更改为 appProperties,这样会是这样的:

RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"RETUЯИ" initialProperties:appProperties];

毕竟,我们需要在firebase控制台中添加一个APNs Authentication Key文件。

进入项目设置,然后向下滚动查看你的ios应用,你会看到APNs Authentication Key标题,你需要上传一个authKey.p8文件或者你可以使用其他两个字段标题 APNs 证书

authKey.p8 文件需要从app store connect

生成 选择用户和访问权限,然后选择 API 密钥选项卡。 点击生成 API 密钥或添加 (+) 按钮。 为您的密钥命名,勾选“Apple Push Notifications Service”框,然后下载 将 .p8 文件上传到 Firebase 控制台

当然,不要错过将 GoogleService-info.plist 文件添加到您的 example.xcworkspace 中,您可以在 rnfirebase.io 中看到如何做到这一点

另外,您需要您的团队 ID,您可以从应用商店连接配置文件中获取它,生成后您将看到密钥 ID。

希望对你有帮助?,

问候

【讨论】:

以上是关于RNFIREBASE MESSENGER 无法在 iOS 上运行,但在 Android 上运行的主要内容,如果未能解决你的问题,请参考以下文章

Messenger:附件类型“视频”无法正常工作?

友盟 SDK 分享到 Messenger 无法发送消息

React Native 和 RNFirebase iOS 版本构建失败

RNFirebase 核心模块在 android 上原生找不到

在 iOS 上未原生找到 RNFirebase 核心模块 - 未修复

Messenger Bot 无法响应