在检索发件人 ID 的 FCM 令牌之前未设置 APNS 设备令牌 - React Native Firebase

新手上路,请多包涵

我一直在按照 教程使用 react-native-firebase 5.2.0 版在我的 react-native 应用程序上设置远程推送通知。在我配置完所有内容并运行应用程序后,出现以下错误:

在检索发件人 ID “ 的 FCM 令牌之前未设置 APNS 设备令牌。此 FCM 代币的通知将不会通过 APNS 传递。设置 APNS 令牌后,请务必重新检索 FCM 令牌。

一直试图弄清楚如何解决这个问题,但不是很成功。在 react-native 上运行:0.61.2,react-native-firebase:5.2.0

以下是我的 AppDelegate.m

 #import "AppDelegate.h"

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <RNCPushNotificationIOS.h>
#import <UserNotifications/UserNotifications.h>
#import <Firebase.h>

@import Firebase;
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [FIRApp configure];
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"helloworld"
                                            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];

  // define UNUserNotificationCenter
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  center.delegate = self;

  return YES;
}

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

// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
  [RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
}
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
  [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
}
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
  [RNCPushNotificationIOS didReceiveLocalNotification:notification];
}

//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  NSLog(@"User Info : %@",notification.request.content.userInfo);
  completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

@end

和我的 App.js 上的令牌检索:

 const messaging = firebase.messaging();

messaging.hasPermission()
  .then((enabled) => {
    if (enabled) {
      messaging.getToken()
        .then(token => { console.log("+++++ TOKEN ++++++" + token) })
        .catch(error => { console.log(" +++++ ERROR GT +++++ " + error) })
    } else {
      messaging.requestPermission()
        .then(() => { console.log("+++ PERMISSION REQUESTED +++++") })
        .catch(error => { console.log(" +++++ ERROR RP ++++ " + error) })
    }

  })
  .catch(error => { console.log(" +++++ ERROR +++++ " + error) });

任何帮助将非常感激!谢谢!

原文由 Prabash Darshana 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 704
2 个回答

我能够解决这个问题:老实说,这很简单。我的 iPhone 连接到互联网时出现问题,我修复了它,这个问题也得到了修复! :)

原文由 Prabash Darshana 发布,翻译遵循 CC BY-SA 4.0 许可协议

我在 3 天内解决了这个问题。您需要将 p8 密钥连接到您的 Firebase

创建 p8 密钥的链接 https://stackoverflow.com/a/67533665/13033024

在此处输入图像描述

将以下代码添加到您的 AppDelegate.swift

  if #available(iOS 10.0, *) {
     UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
    }

 application.registerForRemoteNotifications()

转到 xCode 并单击“功能”选项卡。添加背景模式和推送通知。在后台模式选项卡中,启用后台获取和远程通知

不要忘记添加它以用于发布、调试、配置文件

在此处输入图像描述

从手机中删除您的应用程序并再次运行它。

我希望这对你也有帮助!

原文由 Artur 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题