I want to get getAPNSToken() on a real IOS device but it always returns null. It however returns the token on the IOS simulator.
code:
if (Platform.isIOS) {
String? apnsToken =
await FirebaseMessaging.instance.getAPNSToken();
if (value == true) {
print("The token is ........${apnsToken}");
}
I don't think the "value" here is needed to solve my problem because the code works on Andriod emulators and IOS simulators. It's only on real IOS devices that it doesn't seem to work. But if you need it I can provide it.
AppDelegate.swift
import UIKit
import Flutter
import GoogleMaps
import Firebase
import FirebaseMessaging
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
GMSServices.provideAPIKey("XXXXXXXXXXXXXXXX")
GeneratedPluginRegistrant.register(with: self)
application.registerForRemoteNotifications()
if #available(iOS 12.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
if(!UserDefaults.standard.bool(forKey: "Notification")) {
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UserDefaults.standard.set(true, forKey: "Notification")
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){Messaging.messaging().apnsToken = deviceToken
print("Token: \(deviceToken)")
super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}
override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
super.application(application, didReceiveRemoteNotification: userInfo, fetchCompletionHandler: completionHandler)
let firebaseAuth = Auth.auth()
Messaging.messaging().appDidReceiveMessage(userInfo)
print(userInfo)
if (firebaseAuth.canHandleNotification(userInfo)){
completionHandler(.noData)
return
}
}
}
Can someone tell me what I'm doing wrong? Thank you.