Reputation: 4134
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>
Upvotes: 24
Views: 35338
Reputation: 520
In the Signing & Capabilities tab, make sure the following capabilities are enabled: Push Notifications Background Modes (Remote notifications option must be checked)
Upvotes: 0
Reputation: 641
final _firebaseMessaging = FirebaseMessaging.instance;
if (Platform.isIOS) {
String? apnsToken = await _firebaseMessaging.getAPNSToken();
print('APNS Token: $apnsToken');
await Future.delayed(Duration(seconds: 2));
}
add this code where you handle firebase messaging as it get apnstoken and we add delay two second to confirm that we already get it as if we not add delay the code will run and not get apns token
Upvotes: 1
Reputation: 1069
Did you add Capabilities: BackgroundModes and PushNotifications?
Upvotes: 25
Reputation: 4134
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);
}
Upvotes: 19
Reputation: 455
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
Upvotes: 17