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

Pra*_*ana 56 ios firebase react-native 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
Run Code Online (Sandbox Code Playgroud)

以及我的 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) });

Run Code Online (Sandbox Code Playgroud)

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

Art*_*tur 57

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

创建 p8 密钥的链接/sf/answers/4727356581/

在此输入图像描述

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

 if #available(iOS 10.0, *) { 
     UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
    }
   
 application.registerForRemoteNotifications()
Run Code Online (Sandbox Code Playgroud)

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

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

在此输入图像描述

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

我希望这也能帮助你!

  • 就我而言,我错过了“背景模式”,谢谢! (4认同)
  • 它在我的情况下不起作用。 (4认同)

Vla*_*noy 25

在浪费了一整天的时间试图找到这个奇怪错误的修复方法之后,我找到了一个为我修复它的解决方案。就我而言,云消息不知何故搞砸了 APN 令牌设置过程,因此我必须自己手动设置:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    print("Registered for Apple Remote Notifications")
    Messaging.messaging().setAPNSToken(deviceToken, type: .unknown)
}
Run Code Online (Sandbox Code Playgroud)

当然,您必须事先注册远程通知才能通过以下方式接收通知application.registerForRemoteNotifications()

  • 这解决了它。这要么是他们的 swizzling 的错误,要么是 firebase 的错误文档。 (4认同)
  • 你把这个放在哪里了? (2认同)

Vla*_*ica 24

也许有人之前提到过......你必须使用真实设备,而不是模拟器,否则这个错误将始终显示。


Mil*_*eld 23

对于其他人,如果您忘记在 Xcode 中的项目目标选项卡Push Notification中添加该功能,也会出现此警告/错误Signing & Capabilities


小智 18

对于 Flutter,只需请求权限即可。

FirebaseMessaging.instance.requestPermission();
Run Code Online (Sandbox Code Playgroud)


fl0*_*034 9

就我而言,它在某个时候停止工作。后来我发现,这是因为我从 AppDelegate 中删除了这两个函数

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    print("application didRegisterForRemoteNotificationsWithDeviceToken")
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("application didFailToRegisterForRemoteNotificationsWithError")
}
Run Code Online (Sandbox Code Playgroud)

Firebase Messaging 使用 swizzling 将这些函数替换为自己的实现。但是,如果您的代码中不存在这些函数,则似乎无法将它们的代码混合到.


Pra*_*ana 6

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


Ism*_*rri 6

如果有人 100% 确定他们正确执行了所有步骤,例如

  • 将GoogleService-Info.plist添加到您的项目中
  • 将身份验证密钥 p8 添加到 Firebase
  • 为您的应用程序提供“推送通知”和“后台模式(后台获取、远程通知) ”功能
  • 确保您的应用程序 Bundle Id 与 firebase 中添加的 ID 匹配
  • 另外,我建议允许 Xcode 自动管理签名,方法是选中常规选项卡“自动管理签名”中的复选框,然后选择一个团队,当您执行此操作时,Xcode 将为您生成正确的证书和配置文件,而无需转到您的开发人员帐户
  • 您的手机已连接互联网

如果您执行了所有这些操作,但仍然没有收到远程通知并收到错误“在检索发件人 ID 的 FCM 令牌之前未设置 APNS 设备令牌

检查您的info.plist 是否将此记录“ FirebaseAppDelegateProxyEnabled ”设置为“ false

    <key>FirebaseAppDelegateProxyEnabled</key>
    <false/>
Run Code Online (Sandbox Code Playgroud)

然后你需要将以下代码添加到你的AppDelegate.m中

    -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken: (NSData *)deviceToken
    {
      [FIRMessaging messaging].APNSToken = deviceToken;
      NSString *fcmToken = [FIRMessaging messaging].FCMToken;
      NSLog(@"++APNST deviceToken : %@", deviceToken);
      NSLog(@"++FCM device token : %@", fcmToken);
    }
Run Code Online (Sandbox Code Playgroud)

然后

  • “pod install --repo-update”
  • “cmd + Shift + k”
  • 构建并尝试


maz*_*maz 5

添加此后Info.plist我终于得到了推动:

    <key>FirebaseAppDelegateProxyEnabled</key>
    <false/>
Run Code Online (Sandbox Code Playgroud)