Flutter - APNS token has not been set yet. Please ensure the APNS token is available by calling getAPNSToken()
Asked Answered
T

3

13

While calling the subscribeToTopic method in iOS, this APNS token exception is coming. I have followed the documentation. I have used the Flutterfire configure command for configuring the Flutter project.

But this error is coming. Anyone know why this error is coming and how to resolve it?

I haven't called the getAPNSToken() method in the code.

Is it needed to initiate this method before calling subscribeToTopic method?

Note: We are not using tokens to send notifications to particular users. We are using the topic to send notifications. So I'm not calling the getToken and getAPNSToken methods.

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>
Trier answered 12/9, 2023 at 13:16 Comment(4)
"Please ensure the APNS token is available by calling getAPNSToken()" Have you done that? If no, why not, if yes why don't you say you've done so in your question so people know you are having this issue despite having called that.Tacmahack
@Tacmahack I have updated my question. I haven't called getAPNSToken() method in the code. Is it needed to call before subscribeToTopic() method call?Trier
I added call to getAPNSToken and I still have the same problem.Stockwell
My facepalm mistake was that for Signing & Capability I added Background Mode and Push Notifications only for Release, but forgot for Debug.Wentz
T
13

As per documentation an APNS token needs to be available before calling subscribeToTopic method.

So on the iOS platform, if apnsToken is not available, we have added a delay. After a delay, if we are calling getAPNSToken, it is getting a token. so we can call the subscribeToTopic method.

Answer Reference: Github Issue Comment

  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);
  }
Trier answered 21/10, 2023 at 11:15 Comment(0)
C
13

Did you add Capabilities: BackgroundModes and PushNotifications?

enter image description here

Comstock answered 12/5 at 11:45 Comment(2)
useful for me! thanksQuinque
Thanks, I didn't have push notificationsPeriderm
S
12

I added delaly like -

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

An error occurred while calling method Messaging#subscribeToTopic #10625

Stockwell answered 25/9, 2023 at 19:34 Comment(2)
This is so dumb - but, yes, it works - would be much nicer if Firebase had a call back to let you know it was ready for getToken to be called.Putnam
It didn't help any other solutionPatroclus

© 2022 - 2024 — McMap. All rights reserved.