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

APNS device token not set before retrieving FCM Token for Sender ID - React Native Firebase

我一直在按照 this 教程使用 react-native-firebase 版本 5.2.0 在我的 react-native 应用程序上设置远程推送通知。在我配置了所有内容和 运行 应用程序后,我收到错误消息:

APNS device token not set before retrieving FCM Token for Sender ID ''. Notifications to this FCM Token will not be delievered over APNS. Be sure to re-retrieve the FCM token once the APNS token is set.

一直在想办法解决这个问题,但不太成功。 运行 在 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) });

如有任何帮助,我们将不胜感激!谢谢!

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

简答,重新启动我的电脑,它再次工作。

我在将 react-native 0.59.9 升级到 0.61.2 时出现了同样的错误。

我使用 react-native init 从头开始​​创建了一个新项目,然后修改它以包含我所做的本机配置。最后,我将文件复制到原始项目所在的目录,并解决了所需的更改和冲突。

推送通知刚刚在 iOS 上停止工作,尽管我可以看到没有对本机 iOS 代码的修改会导致此问题。

我删除了 node_modulesios/buildios/Pods,重新启动了我的机器,然后重新安装了所有依赖项,通知再次工作。

我不确定为什么,但仍然分享以防其他人遇到同样的错误。

let token = await firebase.messaging().getToken();
await firebase.messaging().ios.registerForRemoteNotifications();

解决了我的问题

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

我正在使用 Flutter,在我的情况下,我忘记请求处理消息的权限。

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

FirebaseMessaging.instance.requestPermission();

Flutter解决方案

  1. 启用背景模式:

  1. 请求通知权限:

    FirebaseMessaging.instance.requestPermission();
    
  2. 获取令牌 ID(可选)

    String? token = await FirebaseMessaging.instance.getToken();
    

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

Link 创建 p8 密钥

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

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

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

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

从 phone 中删除您的应用,然后再次 运行。

希望这对你也有帮助!

可能之前有人提到过...你必须使用真实设备,而不是模拟器,否则会一直显示此错误。

将此添加到 Info.plist 后,我终于得到了推送:

    <key>FirebaseAppDelegateProxyEnabled</key>
    <false/>

就我而言,我刚刚在 Apple Developer Portal 上重新创建了 APNs 密钥,上传到 Firebase Cloud Message 并且推送开始工作。

在浪费了我一整天的时间试图找到这个奇怪错误的修复程序之后,我找到了一个解决方案来修复它。就我而言,云消息以某种方式搞砸了 APN 令牌设置过程,所以我不得不自己手动设置它:

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

当然你必须事先注册远程通知才能通过application.registerForRemoteNotifications()

接收通知

我更新了 POD。最新的 Firebase SDK 开始使用(从 Podfile.lock 文件确认)并且在 Xcode 日志中以下错误消失了。 “在检索发件人 ID 'XXXXXXXXXXXXX' 的 FCM 令牌之前未设置 APNS 设备令牌。一旦 APNS 设备令牌被发送到 re-retrieve,发送到此 FCM 令牌的通知将不会通过 APNS.Be 发送到 re-retrieve FCM 令牌设置。”