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>
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);
}