I am trying to implement push notifications in my flutter app using FCM but calling getAPNSToken() is returning null whenever I test on a real device. It works in the simulator (can’t receive a notification but I can get the APNs token and the device registration token). I have enabled background fetch and remote notifications and added the Push Notifications capability in xcode and have uploaded my APN key to firebase. Here is my code for requesting notification permission and getting the APN token and device token:
final permissionRequest = await FirebaseMessaging.instance.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
String? token;
if (permissionRequest.authorizationStatus == AuthorizationStatus.authorized) {
debugPrint('notifications authorized');
if (Platform.isIOS) {
String? apnsToken = await FirebaseMessaging.instance.getAPNSToken();
debugPrint('APNS token: $apnsToken');
if (apnsToken != null) {
token = await FirebaseMessaging.instance.getToken();
} else {
await Future<void>.delayed(
const Duration(
seconds: 3,
),
);
apnsToken = await FirebaseMessaging.instance.getAPNSToken();
if (apnsToken != null) {
token = await FirebaseMessaging.instance.getToken();
}
}
} else {
token = await FirebaseMessaging.instance.getToken();
}
} else {
debugPrint('notifications not authorized');
debugPrint('notification settings: ${permissionRequest.authorizationStatus}');
}