didRefreshRegistrationToken is not being called if user Don't Allow notifications swift

别来无恙 提交于 2019-12-11 16:55:15

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/46339505/didrefreshregistrationtoken-is-not-being-called-if-user-dont-allow-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!