【问题标题】:APNS device token not set before retrieving FCM Token for Sender ID - React Native Firebase在检索发件人 ID 的 FCM 令牌之前未设置 APNS 设备令牌 - React Native Firebase
【发布时间】:2020-02-03 09:29:30
【问题描述】:

我一直在关注this 教程,使用 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) });

任何帮助将不胜感激!谢谢!

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        FirebaseMessaging.instance.requestPermission();
        

        【讨论】:

          【解决方案4】:

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

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

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

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

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

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

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

          希望对你也有帮助!

          【讨论】:

          • 就我而言,我错过了Background Modes 谢谢!
          【解决方案5】:

          我正在使用 Flutter,就我而言,我忘记请求处理消息的权限。

          【讨论】:

          • 你能分享代码吗?
          【解决方案6】:

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

          【讨论】:

            【解决方案7】:
            let token = await firebase.messaging().getToken();
            await firebase.messaging().ios.registerForRemoteNotifications();
            

            解决了我的问题

            【讨论】:

              【解决方案8】:

              简答,重新启动我的电脑,它又可以工作了。

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

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

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

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

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

              【讨论】:

                【解决方案9】:

                颤振解决方案

                1. 启用后台模式:

                1. 请求通知权限:

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

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

                【讨论】:

                  【解决方案10】:

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

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

                  【讨论】:

                    【解决方案11】:

                    在我的情况下,我刚刚在 Apple Developer Portal 上重新创建了 APNs 密钥,上传到 Firebase Cloud Message 并开始推送。

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2021-11-27
                      • 2021-06-28
                      • 2022-11-02
                      • 2020-09-24
                      • 1970-01-01
                      • 2016-09-24
                      • 2017-10-23
                      • 1970-01-01
                      相关资源
                      最近更新 更多