ios - firebase ios 大部分时间都不工作

标签 ios swift firebase

我刚刚在我的 ios 应用程序 swift 3 上安装了 firebase。问题是有时通知永远不会发送,有时效果很好,但我从不更改 AppDelegate 中的代码。你建议我编辑什么,我找不到问题。

附言。如果我使用应用程序,我不会收到通知,只有当我不使用它时,我才会收到通知,为什么?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // Override point for customization after application launch.

    let lagFreeField = UITextField()
    self.window?.addSubview(lagFreeField)
    lagFreeField.becomeFirstResponder()
    lagFreeField.resignFirstResponder()
    lagFreeField.removeFromSuperview()


    // [START register_for_notifications]
    if #available(iOS 10.0, *) {
        let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions, completionHandler: {_,_ in })

        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
        // For iOS 10 data message (sent via FCM)
        FIRMessaging.messaging().remoteMessageDelegate = self as? FIRMessagingDelegate

    } else {
        let settings = UIUserNotificationSettings(types: [.alert, .badge , .sound], categories: nil)

        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    }

    application.registerForRemoteNotifications()

    // [END register_for_notifications]

    FIRApp.configure()

    print("AppDelegate")

   IQKeyboardManager.sharedManager().enable = true
    self.window = UIWindow(frame: UIScreen.main.bounds)
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let tab = storyboard.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController
    self.window?.rootViewController = tab
    if Defaults.hasKey(.logged), let logged = Defaults[.logged], logged == true{
        APIRequest.username = Defaults[.username]!
        APIRequest.password = Defaults[.password]!
        let tab = storyboard.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController
        self.window?.rootViewController = tab

    } else {
        let controller = storyboard.instantiateViewController(withIdentifier: "LoginViewController")
        self.window?.rootViewController = controller
    }
    self.window?.makeKeyAndVisible()

    return true
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    print("Userinfo \(userInfo)")
}

最佳答案

只要按照firebase说明中的步骤操作就可以了。火力基地说明: https://firebase.google.com/docs/cloud-messaging/ios/client

AppCoda 撰写了另一篇关于从头开始将 firebase 推送通知安装到实时应用程序的精彩文章:

https://www.appcoda.com/firebase-push-notifications/

关于ios - firebase ios 大部分时间都不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44074710/

相关文章:

ios - 避免在 Swift 中重复元组

ios - 如何规范化 iOS 中的视差数据?

firebase - 具有订单稳定性的 Firestore 查询

android - 将 HashMap 存储在数组中,反之亦然

ios - spritekit游戏项目中如何让节点围绕节点外的点旋转

ios - 如何将图像设置为在另一个viewController中异步获取的UIImageView?

ios - 使用 Realm 我如何更新数据,它不应该创建新对象只更新对象

ios - 访问核心数据中的子对象时出现关系错误

firebase - 使用 Provider 的 Flutter Web 和 Firebase List<String> 和 List <dynamic> 错误

ios - 如何从 iOS APP 刷新 FBSDK 访问 token