在iOS中永远不会调用react-native-firebase notifications()。onNotification()

时间:2019-12-10 02:20:57

标签: ios firebase react-native notifications react-native-firebase

我从firebase控制台发送了测试消息,但是firebase.notifications()。onNitification((notification:Notification)=> { console.log(notification); })从未被调用过。

版本

- "react-native": "^0.61.2",
 - "react-native-firebase": "^5.5.6", 

podfile

 1. pod 'RNFirebase', :path => '../node_modules/react-native-firebase/ios'
 2. pod 'Firebase/Core', '~> 6.3.0'
 3. pod 'Firebase/Messaging', '~> 6.3.0'

我所做的是...

  
      
  1. 我将APN密钥上传到了Firebase项目。

  2.   
  3. 我在项目中放入了GoogleService-info.plist。

  4.   
  5. 这是我的AppDelegate.m

  6.   
#import "AppDelegate.h"

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

#import <GoogleMaps/GoogleMaps.h>
#import <Firebase.h>
#import <FirebaseMessaging.h>
#import "RNFirebaseMessaging.h"
#import "RNFirebaseNotifications.h"
#import "RNSplashScreen.h"

@import Firebase;
@import UserNotifications;

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [FIRApp configure];
  [RNFirebaseNotifications configure];
  [GMSServices provideAPIKey:@""];
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@""
                                            initialProperties:nil];

  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1]; 
  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible]; 
  [RNSplashScreen show]; 
  return YES;
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
  [[RNFirebaseNotifications instance] didReceiveLocalNotification:notification];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo
fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
  [[RNFirebaseNotifications instance] didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
  [[RNFirebaseMessaging instance] didRegisterUserNotificationSettings:notificationSettings];
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}

@end
  1. 这是我的代码
this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
    alert('in method')
    console.log("onNotification");
    console.log(notification);
    notification.setSound("default");
    notification.ios.setBadge(notification.ios.badge ? notification.ios.badge + 1 : 0);
    firebase.notifications().displayNotification(notification);
});

1 个答案:

答案 0 :(得分:1)

仅基于您提供的信息进行一些疯狂的猜测:

1)在Xcode的功能标签中,打开:

a)推送通知

b)后台模式-仅检查远程通知

2)检查构建阶段:

a)在Project Navigator中,右键单击Libraries> Add Files To。导航到/ node_modules / react-native-firebase / ios /。选择RNFirebase.xcodeproj,然后单击“添加”按钮。

b)再次单击“ +”,选择libRNFirebase.a并将其添加。如果找不到,请清理并构建项目。

c)转到“构建设置”,找到“标题搜索路径”,双击其值,然后按“ +”按钮。在此添加以下行: $(SRCROOT)/../ node_modules / react-native-firebase / ios / RNFirebase

d)使用“ Cmd + Shift + Enter + K”键清除缓存,然后构建项目。现在,firebase依赖项应该可以通过xcode识别。

e)使用“ Cmd + Shift + Enter + K”键清除缓存,然后构建项目。现在,firebase依赖项应该可以通过xcode识别。