【问题标题】:react-native-firebase notifications().onNotification() is never called in iosreact-native-firebase notifications().onNotification() 永远不会在 ios 中调用
【发布时间】:2019-12-10 02:20:57
【问题描述】:

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

版本

- "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. 我将 GoogleService-info.plist 放入我的项目中。

  3. 这是我的 AppDelegate.m

#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);
});

【问题讨论】:

  • notification.setSound("default") 和你一起工作?你听到声音了吗?
  • @OliverD 我愿意。它有效
  • 在后台还是前台?
  • @OliverD 在前台和后台
  • 很抱歉,但如果我想添加自己的声音,我该怎么做?

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


【解决方案1】:

根据您提供的信息进行一些疯狂的猜测:

1) 在 Xcode 的功能选项卡中,打开:

a) 推送通知

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

2) 检查构建阶段:

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

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

c) 进入 Build Settings,找到 Header Search Path,双击其值并按“+”按钮。在此处添加以下行: $(SRCROOT)/../node_modules/react-native-firebase/ios/RNFirebase

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

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

【讨论】:

  • 功能选项卡中的推送通知丢失。我很感激。你拯救了我的一天:)
猜你喜欢
  • 2019-02-22
  • 2016-12-07
  • 2018-12-16
  • 1970-01-01
  • 2023-02-08
  • 1970-01-01
  • 2020-10-13
  • 2022-01-02
  • 1970-01-01
相关资源
最近更新 更多