【问题标题】:didRefreshRegistrationToken is not being called if user Don't Allow notifications swift如果用户不允许快速通知,则不会调用 didRefreshRegistrationToken
【发布时间】:2017-09-21 08:54:11
【问题描述】:

在我尝试了很多可能解决此问题的问题后,我的问题仍未得到解决:

我想得到FCM Token。在AppDelegate.swift我已经导入了

import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging

之后,我使用UNUserNotificationCenterDelegate, MessagingDelegate 对其进行了扩展,然后在didFinishLaunchingWithOptions 中添加了:

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()

然后我添加了这四个函数:

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)")
    }

现在的问题是,当我运行应用程序时,它会显示要求发送通知权限的警报。允许或禁止它不是我关心的问题,我已经实现了 MessagingDelegate 方法 didRefreshRegistrationToken 所以应该调用它,但它没有调用该方法。所以我错过了什么吗?

【问题讨论】:

    标签: ios firebase swift3 firebase-cloud-messaging


    【解决方案1】:

    你必须像这样添加一个观察者

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

    然后像这样在你的 Appdelegate 中添加这个方法

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

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      Firebase Docs 中所述,您可以:

      1. 提供符合FIRMessagingDelegate 协议的委托
      2. 监听名为kFIRMessagingRegistrationTokenRefreshNotification的NSNotification

      如果选择第一种,需要设置委托

      Messaging.messaging().delegate = self
      

      你可以在quickstart example code看到这个完成

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-07
        • 1970-01-01
        • 2015-12-28
        • 1970-01-01
        • 1970-01-01
        • 2018-03-07
        • 1970-01-01
        相关资源
        最近更新 更多