Firebase iOS推送通知在首次安装时不起作用

Firebase iOS推送通知在首次安装时不起作用,ios,swift,firebase,apple-push-notifications,firebase-cloud-messaging,Ios,Swift,Firebase,Apple Push Notifications,Firebase Cloud Messaging,当我第一次安装并打开应用程序并接受苹果的通知权限警报时,我从Firebase获得以下日志: 5.16.0-[Firebase/InstanceID][I-IID023004]无法更新密钥对的属性,以便在首次解锁后可以访问。更新 状况:-25300 之后,如果我关闭或将应用发送到后台,我不会收到任何通知。如果我再次打开应用程序,则通知将开始正常工作 这是我当前的设置: func application(_ application: UIApplication, didFinishLaunching

当我第一次安装并打开应用程序并接受苹果的通知权限警报时,我从Firebase获得以下日志:

5.16.0-[Firebase/InstanceID][I-IID023004]无法更新密钥对的属性,以便在首次解锁后可以访问。更新 状况:-25300

之后,如果我关闭或将应用发送到后台,我不会收到任何通知。如果我再次打开应用程序,则通知将开始正常工作

这是我当前的设置:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    setupPushNotificationsHandling(application)
    return true
}

private func setupPushNotificationsHandling(_ application: UIApplication) {
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, _) in
        guard granted else { return }

        DispatchQueue.main.async {
            application.registerForRemoteNotifications()

            FirebaseApp.configure()
            InstanceID.instanceID().instanceID { (result, _) in
                // Receive notifications from the "all" topic
                Messaging.messaging().subscribe(toTopic: "all")
            }
        }
    }
}

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

我就是这样解决的

  • 在尝试订阅通知主题之前,我会等待调用委托方法
    application:didRegisterForRemotionTificationsWithDeviceToken:
  • 我重试,直到调用
    InstanceID.InstanceID().InstanceID
    返回有效的设备令牌
  • 完成设置:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        setupPushNotificationsHandling(application)
        return true
    }
    
    private func setupPushNotificationsHandling(_ application: UIApplication) {
        FirebaseApp.configure()
    
        application.registerForRemoteNotifications()
    
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (_, _) in }
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // Receive notifications from the "all" topic
        subscribeToNotificationsTopic(topic: "all")
    }
    
    func subscribeToNotificationsTopic(topic: String) {
        // Retry until the notifications subscription is successful
        DispatchQueue.global().async {
            var subscribed = false
            while !subscribed {
                let semaphore = DispatchSemaphore(value: 0)
    
                InstanceID.instanceID().instanceID { (result, error) in
                    if let result = result {
                        // Device token can be used to send notifications exclusively to this device
                        print("Device token \(result.token)")
    
                        // Subscribe
                        Messaging.messaging().subscribe(toTopic: topic)
    
                        // Notify semaphore
                        subscribed = true
                        semaphore.signal()
                    }
                }
    
                // Set a 3 seconds timeout
                let dispatchTime = DispatchTime.now() + DispatchTimeInterval.seconds(3)
                _ = semaphore.wait(timeout: dispatchTime)
            }
        }
    }
    

    Firebase.configure(),将其放入应用程序委托中。然后,您可以在RegisterForemotentification发生后获取instanceId。@karthik-谢谢,但我的应用程序委托上已经有了
    Firebase.configure()
    ,就在我调用
    instanceId.instanceId().instanceId
    之前。我可以看到您正在调用RegisterForemotentification。在注册前进行配置。在注册前进行
    configure
    调用;同样的问题。我有同样的问题-有解决方案吗?为我工作。。。。。