1

I am using Firebase trying to get the FCM Token so I can test Push notifications on an actual device. I get the APNS token fine, set it with

Messaging.messaging().apnsToken = deviceToken,

but when I try to use Messaging.messaging().fcmToken to get the FCM token, it returns nil, as well as

InstanceID.instanceID().instanceID { (result, error)  in } //returning nil.

However, when I use Messaging.messaging().retrieveFCMToken or InstanceID.instanceID().getID I get results, yet I see no one advocating for the use of these functions to get the FCM token. Are these functions correct ways to get the FCM token?

Dimple
  • 788
  • 1
  • 11
  • 27
B Dub
  • 41
  • 2
  • 4
  • Also, didReceiveRegistrationToken is not firing off for some reason either, even though all my delegates are setup correctly and I am getting the apns token correctly – B Dub Sep 17 '18 at 04:05
  • Have you assigned delegate ? – Anuraj Sep 17 '18 at 05:20
  • Yes Messaging.messaging().delegate = self is set after FirebaseApp.configure(), and UNUserNotificationCenter.current().delegate = self is set properly as well. – B Dub Sep 17 '18 at 16:05

1 Answers1

0

Try this one it's work for me (swift 4 Code)

Request for permission

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    self.setupFirebase(applicatin: application)
}

Setup Firebase registerUserNotificationSettings

func setupFirebase(applicatin:UIApplication)
{
    if #available(iOS 10.0, *) {
        let authOptions : UNAuthorizationOptions = [.alert, .badge , .sound]

        UNUserNotificationCenter.current().requestAuthorization(
        options: authOptions,
        completionHandler: {_,_ in })
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        // For iOS 10 data message (sent via FCM)
        // FIRMessaging.messaging().remoteMessageDelegate = self
    } else {

        let notificationtype :UIUserNotificationType = [UIUserNotificationType.alert,UIUserNotificationType.badge,UIUserNotificationType.sound]
        let notificationsettings = UIUserNotificationSettings(types: notificationtype, categories: nil)

        applicatin.registerUserNotificationSettings(notificationsettings)
    }
    applicatin.registerForRemoteNotifications()
}

Getting device token

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    guard InstanceID.instanceID().token() != nil
        else {
            return
    }

    if let refreshedToken = InstanceID.instanceID().token()
    {
        print("InstanceID token: \(refreshedToken)")
    }
}

In case of error

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Filed to register : \(error.localizedDescription)")
}
Nikunj Kumbhani
  • 3,758
  • 2
  • 26
  • 51
  • *InstanceID.instanceID().token()* has depricated, plz read this : https://stackoverflow.com/questions/50945015/firebase-instanceid-instanceid-token-method-is-deprecated/50945350#50945350 – Anbu.Karthik Sep 17 '18 at 05:48