srikanth stackflow
srikanth stackflow

Reputation: 61

FCM token doesn't call didReceiveRegistrationToken

Hi using FCM into my application.

When I am trying to read FCM token even compiler don't call this delegate method also is any way please help.

when I put breaks and check this method is don't call I using ios11 swift4

extension AppDelegate: MessagingDelegate{

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")

        let dataDict:[String: String] = ["token": fcmToken]
        NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
        // TODO: If necessary send token to application server.
        // Note: This callback is fired at each app startup and whenever a new token is generated.
    }

    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        print("Message data:", remoteMessage.appData)
    }  
}

Upvotes: 3

Views: 9042

Answers (4)

Taras
Taras

Reputation: 1909

The path should be correct to entitlements. It fixed my issue

enter image description here

Upvotes: 0

RyanTCB
RyanTCB

Reputation: 8234

I knew this and still came here for answers as I had forgot. So just a polite reminder. The calls will not be made in the simulator. Ensure to be on real device :-)

Upvotes: 6

srikanth stackflow
srikanth stackflow

Reputation: 61

I using the following function it is working fine

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        if let refreshedToken = InstanceID.instanceID().token() {
           print("InstanceID token: \(refreshedToken)")
        }
    }

Upvotes: 0

Sahil Manchanda
Sahil Manchanda

Reputation: 10557

 didReceiveRegistrationToken

is a delegate function. it will get called upon receiving token form firebase. if it's not getting called make sure you've following code in AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    Messaging.messaging().delegate = self
    return true
}

with Messaging.messaging().delegate = self we are registering AppDelegate for receiving the method call from Firebase

Upvotes: 7

Related Questions