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

时间:2019-10-05 08:27:32

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

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

  

未为获取“发件人ID”的FCM令牌检索到APNS设备令牌。不会通过APNS发出对此FCM令牌的通知。设置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) });

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

6 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

解决了我的问题

答案 2 :(得分:1)

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

答案 3 :(得分:0)

我能够解决此问题:说实话,这很简单。我的iPhone连接互联网时出现问题,我已将其修复,此问题也已修复! :)

答案 4 :(得分:0)

简短答案,重新启动计算机后又可以正常工作。

将本机0.59.9升级到0.61.2时,我有同样的错误。

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

即使我看到没有对导致此问题的本地iOS代码进行任何修改,推送通知也仅在iOS上停止工作。

我删除了node_modulesios/buildios/Pods,重新启动了计算机,然后重新安装了所有依赖项,并且通知再次起作用。

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

答案 5 :(得分:0)

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

FirebaseMessaging.instance.requestPermission();