您没有收到通知可能是因为 firebase 未正确集成到您的应用程序中。正确集成的步骤如下:
react-native-firebase 库对我不起作用。使用 react-native-fcm 集成 firebase 以响应本机应用程序成功接收推送通知。
1.安装 react-native-fcm:
npm install react-native-fcm --save
- 从 Firebase 控制台,
对于 Android:下载 google-services.json 文件并将其放在 android/app 目录中。
对于 iOS:下载 GoogleService-Info.plist 文件并将其放在 /ios/your-project-name 目录中(Info.plist 旁边)
3.配置项目使用:
cd ios && pod init
4.编辑新创建的 Podfile:
吊舱安装
5.编辑AppDelegate.h:
@import UserNotifications;
@interface AppDelegate:UIResponder<UIApplicationDelegate,UNUserNotificationCenterDelegate>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
6.编辑AppDelegate.m:
#import "RNFIRMessaging.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[FIRApp configure];
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
return YES;
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^) .
(UNNotificationPresentationOptions))completionHandler
{
[RNFIRMessaging willPresentNotification:notification
withCompletionHandler:completionHandler];
}
#if defined(__IPHONE_11_0)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler
{
[RNFIRMessaging didReceiveNotificationResponse:response
withCompletionHandler:completionHandler];
}
#else
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void(^)())completionHandler
{
[RNFIRMessaging didReceiveNotificationResponse:response
withCompletionHandler:completionHandler];
}
#endif
//You can skip this method if you don't want to use local notification
-(void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
[RNFIRMessaging didReceiveLocalNotification:notification];
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo
fetchCompletionHandler:(nonnull void (^) .
(UIBackgroundFetchResult))completionHandler{
[RNFIRMessaging didReceiveRemoteNotification:userInfo
fetchCompletionHandler:completionHandler];
}
7.XCode中添加头文件路径:
打开 XCode。
按 CMD+1 或单击 XCode 项目。
转到构建设置,选择全部和组合。
在 searchg 选项卡中搜索标题搜索路径。
确保有一行 $(SRCROOT)/../node_modules/react-native-fcm/ios。如果没有,请添加它。
8.添加GoogleService-Info.plist:
确保文件不只是移动到文件夹中。您需要右键单击 XCode 中的项目文件夹并将文件添加到 .选择文件时,在选项页面中根据需要选择复制。
9.共享库设置:
如果您使用 Pod 安装方法,请确保您在 Library 文件夹下看到 Pods.xcodeproj。
确保您在 Library 文件夹下看到 RNFIRMessaging.xcodeproj。
如果您没有看到任何这些文件,请通过右键单击库文件夹并添加文件来添加它们以将它们添加回来。 Pods.xcodeproj 应该在 ios/Pods/ 下,RNFIRMessaging.xcodeproj 应该在 node_modules/react-native-fcm/ios 下。
确保在 Build Phases 选项卡下的 Link Binary With Libraries 下看到 libRNFIRMessaging.a。如果没有,请将Library文件夹下的RNFIRMessaging.xcodeproj下的文件拖到那里。
10.添加功能
选择您的项目功能并启用:
推送通知
后台模式 > 远程通知。
FirebaseAppDelegateProxyEnabled
此说明假定您有 FirebaseAppDelegateProxyEnabled=YES(默认),以便 Firebase 将挂钩推送通知注册事件。如果您关闭此标志,您将自行管理 APNS 令牌并与 Firebase 令牌链接。