问题
首次启动应用程序时,或者在删除并重新安装后,Messaging.messaging().shouldEstablishDirectChannel
不会建立套接字连接。如果我关闭了该应用程序,然后重新打开它,则建立了套接字连接。
重现步骤:
1)将此代码放在AppDelegate中:FirebaseApp.configure()Messaging.messaging().delegate = selfMessaging.messaging().shouldEstablishDirectChannel = true
2)将这段代码放在之后的任何地方,以检查是否建立了连接:Messaging.messaging().isDirectChannelEstablished
这总是返回false。
3)监听连接状态的变化,并观察该通知从不触发。NotificationCenter.default.addObserver(self, selector: #selector(fcmConnectionStateChange), name: NSNotification.Name.MessagingConnectionStateChanged, object: nil)
简而言之,这就是问题所在。如果我只是杀死该应用程序,然后重新启动它,那么一切都会按预期进行。建立套接字连接,并触发MessagingConnectionStateChanged
通知。
为什么Messaging.messaging().shouldEstablishDirectChannel
在我的初始应用启动时没有连接?
相关代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window!.rootViewController = RootViewController.shared
window!.makeKeyAndVisible()
setUpFirebase()
setUpPushNotificationsForApplication(application)
RootViewController.shared.goToLoginVC()
return true
}
// MARK: - Firebase
func setUpFirebase() {
NotificationCenter.default.addObserver(self, selector:
#selector(fcmConnectionStateChange), name:
NSNotification.Name.MessagingConnectionStateChanged, object: nil)
FirebaseApp.configure()
Messaging.messaging().delegate = self
Messaging.messaging().shouldEstablishDirectChannel = true
}
// MARK: - Firebase Notifications
func fcmConnectionStateChange() {
// This is never called on app's first launch!!!
print(Messaging.messaging().isDirectChannelEstablished)
}
环境
最佳答案
在拥有 token 之前尝试进行FCM连接可能会失败。
我修改了您的代码,请尝试此操作。
func setUpFirebase() {
NotificationCenter.default.addObserver(self, selector:
#selector(self.tokenRefreshNotification), name:
NSNotification.Name.InstanceIDTokenRefresh, object: nil)
NotificationCenter.default.addObserver(self, selector:
#selector(self.fcmConnectionStateChange), name:
NSNotification.Name.MessagingConnectionStateChanged, object: nil)
FirebaseApp.configure()
Messaging.messaging().delegate = self
}
func tokenRefreshNotification(_ notification: Notification) {
if let refreshedToken = InstanceID.instanceID().token() {
print("InstanceID token: \(refreshedToken)")
}
// Connect to FCM since connection may have failed when attempted before having a token.
connectToFcm()
}
func connectToFcm() {
// Won't connect since there is no token
guard InstanceID.instanceID().token() != nil else {
return;
}
Messaging.messaging().shouldEstablishDirectChannel = true
}
func fcmConnectionStateChange() {
if Messaging.messaging().isDirectChannelEstablished {
print("Connected to FCM.")
} else {
print("Disconnected from FCM.")
}
}
更新:
在使用NotificationCenter之前,请添加它。
if #available(iOS 10, *) {
print("iOS 10 up")
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
guard error == nil else {
print("regist fail = \(String(describing: error))")
return
}
if granted {
print("allow regist")
} else {
//Handle user denying permissions..
print("deny regist")
}
}
} else {
print("iOS 9 down")
let pushNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings( pushNotificationSettings )
}
application.registerForRemoteNotifications()