繁体   English   中英

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

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

我一直在按照教程使用 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) });

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

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

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

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

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

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

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

将 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();

解决了我的问题

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

我在 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()

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

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

在此处输入图像描述

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

我希望这对你也有帮助!

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

FirebaseMessaging.instance.requestPermission();

Flutter解决方案

  1. 启用后台模式:

    在此处输入图像描述

  1. 请求通知权限:

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

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

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

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

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

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

确保不要重命名身份验证密钥文件

就我而言,我已将身份验证密钥文件重命名为其他内容,这导致了问题。 我尝试将其命名回默认格式AuthKey_<KeyID>.p8并且一切都开始工作了。

如果有人 100% 确定他们正确地完成了所有步骤,例如

  • GoogleService-Info.plist添加到您的项目中
  • 将 Auth Key p8 添加到 Firebase
  • 为您的应用提供“推送通知”和“后台模式(后台获取、远程通知) ”功能
  • 确保您的应用程序包 ID 与 firebase 中添加的 ID 匹配
  • 我还建议允许 Xcode 通过选中常规选项卡“自动管理签名”中的复选框然后选择一个团队来自动管理签名,当您执行此操作时 Xcode 将为您生成适当的证书和配置文件,无需 Z34D1F91FB2E5148A857A 到您的开发人员帐户776
  • 你的手机有网络连接

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

如果您将此记录“ FirebaseAppDelegateProxyEnabled ”设置为“ false ”,请检查您的info.plist

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

然后您需要将以下代码添加到您的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);
    }

然后

  • “吊舱安装--repo-update”
  • “cmd + shift + k”
  • 构建并尝试

重命名目标文件夹 Xcode 和捆绑 ID 后,我遇到了类似的问题。 如上所述尝试了很多事情。 重新创建证书。 在 Firebase 等中删除和添加应用程序。没有任何效果。 我最终重新创建了一个新项目并复制了所有资产和代码。 经过2个小时的工作(不是一个非常大的应用程序),它终于可以工作了。

解决我的问题的是在真实设备中调试它。 不幸的是,我忘记了通知在模拟器上不起作用......

我更新了我的 POD。 最新的 Firebase SDK 开始使用(从 Podfile.lock 文件确认),在 Xcode 日志中,以下错误消失了。 “在检索发件人 ID 'XXXXXXXXXXXXX' 的 FCM 令牌之前未设置 APNS 设备令牌。不会通过 APNS 传递到此 FCM 令牌的通知。请确保在设置 APNS 设备令牌后重新检索 FCM 令牌。”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM