RNFIREBASE MESSENGER 未在 iOS 上工作,但在 Android 上工作
RNFIREBASE MESSENGER not working on iOS but working on Android
推送通知在 Android 上有效,但在 iOS 上无效。由于不支持模拟器,我已经在设备上进行了测试并通过试飞
我已经完成 iOS 所需的额外设置。
以下是我的 package.json 文件中的相关信息
Package.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 代码片段
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;
})
签名和证书
ios 上的 Firebase 消息传递需要更多配置,
首先,您需要在 AppDelegate.m 中添加此导入:
#import "RNFBMessagingModule.h"
然后在这一行之后:RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
添加这一行:NSDictionary *appProperties = [RNFBMessagingModule addCustomPropsToUserProps:nil withLaunchOptions:launchOptions];
那么你需要将 RCTRootView 中的 initialProperties 从 nil
更改为 appProperties
这样它就会像这样:
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"RETUЯИ" initialProperties:appProperties];
<-- 从 nil 更改为 appProperties
毕竟,我们需要在 firebase 控制台中添加一个 APNs 身份验证密钥 文件。
转到项目设置,然后向下滚动以查看您的 ios 应用程序,您将看到 APNs Authentication Key 标题,您需要上传一个 authKey.p8 文件或者您可以使用标题为 APNs Certificates
的其他两个字段
需要从 app store connect
生成 authKey.p8 文件
- Select 用户和访问,然后 select API 密钥选项卡。 // 见更新部分 ⏬⏬⏬
- 单击生成 API 密钥或添加 (+) 按钮。
- 为您的密钥命名,勾选“Apple Push Notifications Service”框,然后下载
- 将 .p8 文件上传到 firebase 控制台
Update regarding authkey.p8:
- 你需要从apple开发者账号生成,在auth
键列表,这是 link
- instructions
当然,不要错过将 GoogleService-info.plist 文件添加到您的 example.xcworkspace,您可以在 rnfirebase.io
另外,你需要你的Team id,你可以从app store connect profile中获取,生成后你会看到key id。
希望这对你有所帮助,
此致
推送通知在 Android 上有效,但在 iOS 上无效。由于不支持模拟器,我已经在设备上进行了测试并通过试飞
我已经完成 iOS 所需的额外设置。
以下是我的 package.json 文件中的相关信息
Package.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 代码片段
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;
})
签名和证书
ios 上的 Firebase 消息传递需要更多配置,
首先,您需要在 AppDelegate.m 中添加此导入:
#import "RNFBMessagingModule.h"
然后在这一行之后:RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
添加这一行:NSDictionary *appProperties = [RNFBMessagingModule addCustomPropsToUserProps:nil withLaunchOptions:launchOptions];
那么你需要将 RCTRootView 中的 initialProperties 从 nil
更改为 appProperties
这样它就会像这样:
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"RETUЯИ" initialProperties:appProperties];
<-- 从 nil 更改为 appProperties
毕竟,我们需要在 firebase 控制台中添加一个 APNs 身份验证密钥 文件。
转到项目设置,然后向下滚动以查看您的 ios 应用程序,您将看到 APNs Authentication Key 标题,您需要上传一个 authKey.p8 文件或者您可以使用标题为 APNs Certificates
的其他两个字段需要从 app store connect
生成 authKey.p8 文件- Select 用户和访问,然后 select API 密钥选项卡。 // 见更新部分 ⏬⏬⏬
- 单击生成 API 密钥或添加 (+) 按钮。
- 为您的密钥命名,勾选“Apple Push Notifications Service”框,然后下载
- 将 .p8 文件上传到 firebase 控制台
Update regarding authkey.p8:
- 你需要从apple开发者账号生成,在auth 键列表,这是 link
- instructions
当然,不要错过将 GoogleService-info.plist 文件添加到您的 example.xcworkspace,您可以在 rnfirebase.io
另外,你需要你的Team id,你可以从app store connect profile中获取,生成后你会看到key id。
希望这对你有所帮助,
此致