Flutter - APNS 令牌尚未设置。请通过调用 getAPNSToken() 确保 APNS 令牌可用

MSA*_*ish 5 apple-push-notifications ios firebase flutter firebase-cloud-messaging

在 iOS 中调用 subscribeToTopic 方法时,会出现此 APNS 令牌异常。我已遵循文档。我使用了 Flutterfire configure 命令来配置 Flutter 项目。

但这个错误就来了。有谁知道为什么会出现此错误以及如何解决它?

我没有在代码中调用 getAPNSToken() 方法。

调用 subscribeToTopic 方法之前是否需要启动该方法?

注意:我们不使用令牌向特定用户发送通知。我们正在使用该主题来发送通知。所以我没有调用 getToken 和 getAPNSToken 方法。

1970-01-02 04:49:38.216263+0300 Runner[675:161670] flutter: [firebase_messaging/apns-token-not-set] APNS token has not been set yet. Please ensure the APNS token is available by calling `getAPNSToken()`.
1970-01-02 04:49:38.216725+0300 Runner[675:161670] flutter: #0      MethodChannelFirebaseMessaging._APNSTokenCheck (package:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart:138:9)
<asynchronous suspension>
#1      MethodChannelFirebaseMessaging.subscribeToTopic (package:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart:390:5)
<asynchronous suspension>
#2      PushNotificationService.subscribeUserToFirebase (package:team/src/common/notification/firebase/push_notification.dart:70:7)
<asynchronous suspension>
#3      userAPI (package:team/src/features/master_data/application/master_data_providers.dart:116:5)
<asynchronous suspension>
#4      FutureHandlerProviderElementMixin.handleFuture.<anonymous closure>.<anonymous closure> (package:riverpod/src/async_notifier/base.dart:337:9)
<asynchronous suspension>
Run Code Online (Sandbox Code Playgroud)

MSA*_*ish 7

根据文档,在调用 subscribeToTopic 方法之前需要提供 APNS 令牌。

所以在iOS平台上,如果apnsToken不可用,我们添加了一个延迟。经过一段延迟后,如果我们调用 getAPNSToken,它将获取一个令牌。所以我们可以调用 subscribeToTopic 方法。

答案参考Github问题评论

  if (Platform.isIOS) {
    String? apnsToken = await _firebaseMessaging.getAPNSToken();
    if (apnsToken != null) {
      await _firebaseMessaging.subscribeToTopic(personID);
    } else {
      await Future<void>.delayed(
        const Duration(
          seconds: 3,
        ),
      );
      apnsToken = await _firebaseMessaging.getAPNSToken();
      if (apnsToken != null) {
        await _firebaseMessaging.subscribeToTopic(personID);
      }
    }
  } else {
    await _firebaseMessaging.subscribeToTopic(personID);
  }
Run Code Online (Sandbox Code Playgroud)


小智 6

我添加了延迟,例如 -

await Firebase.initializeApp();
await Future.delayed(Duration(seconds: 1));
String? token = await FirebaseMessaging.instance.getToken();
Run Code Online (Sandbox Code Playgroud)

调用方法 Messaging#subscribeToTopic 时发生错误 #10625