火力地堡的iOS推送通知不工作的第一次安装

问题描述 投票:0回答:1

当我第一次安装并打开该应用,并接受来自苹果公司的通知权限警告,我得到这个日志从火力地堡:

5.16.0 - [火力地堡/实例id] [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()
}
ios swift firebase apple-push-notifications firebase-cloud-messaging
1个回答
0
投票

这是我如何解决它。

  1. 尝试订阅通知主题之前,我等到的委托方法application:didRegisterForRemoteNotificationsWithDeviceToken:被调用。
  2. 我重试,直到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)
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.