didRefreshRegistrationToken is not being called if

2019-08-19 05:29发布

My problem is not being solved after I've tried a lot of questions which might have the solution for this issue:

I want to get FCM Token. In AppDelegate.swift I've imported

import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging

After that I've extended it with UNUserNotificationCenterDelegate, MessagingDelegate and then in didFinishLaunchingWithOptions I've added:

if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
            // For iOS 10 data message (sent via FCM
            Messaging.messaging().delegate = self
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()

        FirebaseApp.configure()

And after that I've added these four functions:

func application(_ application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken as Data

    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        print("Here")
    }

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

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

Now the issue is when I run the app it shows the alert which ask for permissions for sending notification. Allowing it or disallowing it is not my concern I have implemented MessagingDelegate method didRefreshRegistrationToken so it should be called, but it is not calling that method. So am I missing something?

2条回答
地球回转人心会变
2楼-- · 2019-08-19 06:11

You have to add an Observer like that

NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)

And add this method in your Appdelegate like that

func tokenRefreshNotification(_ notification: Notification) {
    if let refreshedToken = InstanceID.instanceID().token() {
        print("InstanceID token: \(refreshedToken)")
    }
}

Hope it helps.

查看更多
相关推荐>>
3楼-- · 2019-08-19 06:22

As mentioned in the Firebase Docs you can either:

  1. supply a delegate conforming to the FIRMessagingDelegate protocol
  2. listen for an NSNotification named kFIRMessagingRegistrationTokenRefreshNotification

If you take the first option, you need to set the delegate

Messaging.messaging().delegate = self

You can see this done in the quickstart example code

查看更多
登录 后发表回答