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

Dal*_*eZA 2 ios react-native firebase-cloud-messaging react-native-firebase

推送通知适用于 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"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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);
    }
  }
Run Code Online (Sandbox Code Playgroud)

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];
Run Code Online (Sandbox Code Playgroud)

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;
})
Run Code Online (Sandbox Code Playgroud)

签名和证书 在此输入图像描述

在此输入图像描述

Soh*_*aib 10

ios 上的 Firebase Messaging 需要更多配置,

\n

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

\n

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

\n

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

\n

那么你需要将RCTRootView中的initialProperties从更改为这样:nilappProperties

\n

RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"RETU\xd0\xaf\xd0\x98" initialProperties:appProperties]; <-- 从 nil 改为 appProperties

\n

毕竟,我们需要在 firebase 控制台中添加APNs 身份验证密钥文件。

\n

转到项目设置,然后向下滚动查看您的 ios 应用程序,您将看到APNs 身份验证密钥标题,您需要上传 authKey.p8 文件,或者您可以使用标题为 APNs 证书的其他两个字段

\n

authKey.p8 文件需要从app store connect生成

\n
    \n
  • 选择用户和访问权限,然后选择 API 密钥选项卡。//查看更新部分\xe2\x8f\xac\xe2\x8f\xac\xe2\x8f\xac
  • \n
  • 单击生成 API 密钥或添加 (+) 按钮。
  • \n
  • 为您的密钥命名,勾选“Apple 推送通知服务”框,然后下载
  • \n
  • 将 .p8 文件上传到 firebase 控制台
  • \n
\n
\n
\n

关于 authkey.p8 的更新:

\n
\n
    \n
  • 您需要从苹果开发者帐户生成它,在 auth\nkeys 列表中,这是链接
  • \n
  • 指示
  • \n
\n
\n

当然,不要错过将GoogleService-info.plist文件添加到您的 example.xcworkspace 中,您可以在rnfirebase.io中查看如何执行此操作

\n

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

\n

希望对你有帮助 ,

\n

问候

\n