【发布时间】:2017-08-24 16:53:01
【问题描述】:
问题
第一次启动应用程序时,或删除并重新安装后,Messaging.messaging().shouldEstablishDirectChannel 不会建立套接字连接。如果我关闭了应用程序,然后重新打开它,那么套接字连接就建立了。
1) 将此代码放在 AppDelegate 中:
FirebaseApp.configure()
Messaging.messaging().delegate = self
Messaging.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)
}
环境
- Xcode 版本:8.3.3
- Firebase 4.1.0
- FirebaseAnalytics 4.0.3
- FirebaseCore 4.0.5
- FirebaseInstanceID 2.0.1
- FirebaseMessaging 2.0.1
- Firebase 产品:消息传递
【问题讨论】:
标签: ios swift sockets firebase firebase-cloud-messaging