首先,我对FCM没有任何问题,每次调用tokenRefreshNotification时,firebase令牌都不会为null。但之后,我添加了谷歌分析,我得到了Firebase令牌的奇怪问题。每次我关闭并在我的应用设置中启用通知
UIApplication.shared.registerForRemoteNotifications()
我的tokenRefreshNotification被连续调用,它不会停止循环,直到我强制关闭我的应用程序。起初,我的应用程序崩溃,当我尝试使用NsLog跟踪它时,我发现Firebase令牌为空。只有在我使用从TestFlight / production安装的应用程序时才会出现此问题。当我从我的Xcode构建器中尝试它时,firebase令牌只为null一次,但是第二次调用firebase令牌存在并且它停止工作。
对于谷歌分析,它工作正常,在我的GoogleService-info.plist中, 我已将IS_ANALYTICS_ENABLED设置为YES,并将IS_GCM_ENABLED设置为YES。另一方面,IS_ADS_ENABLED = YES,IS_APPINVITE_ENABLED = NO,IS_SIGNIN_ENABLED = YES
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FIRApp.configure()
NotificationCenter.default.addObserver(self, selector: #selector(self.registerNotification), name: NSNotification.Name(rawValue: "registerNotification"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification(_:)), name: .firInstanceIDTokenRefresh, object: nil)
setupGoogleAnalytics()
return true
}
//when firebase token is null, this function is working continously until my firebase token is exist
func tokenRefreshNotification(_ notification: Notification) {
print("token Refresh")
if FIRInstanceID.instanceID().token() == nil{
NSLog("firebase token is null")
}
if (UserDefaults.standard.object(forKey: "id") != nil) && FIRInstanceID.instanceID().token() != nil{
FIRInstanceID.instanceID().getWithHandler({ (instanceID, error) in
NSLog("instanceID: \(instanceID!)")
//save firebase token to my database, sorry i can`t show it
})
}
// Connect to FCM since connection may have failed when attempted before having a token.
connectToFcm()
}
注意:在第一次启动时,我调用我的RegisterForRemoteNotifications,在TestFlight版本中,当调用tokenRefreshNotification时,firebase令牌为空,但第二次调用firebase令牌存在,因此它可能会停止。但是,当我从Xcode运行我的应用程序时,第一次调用是成功的,因为firebase令牌不为空。
答案 0 :(得分:4)
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// With swizzling disabled you must set the APNs token here.
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.unknown)
}