Firebase 消息传递应该在首次启动应用时建立直接频道不建立连接



>问题

首次启动应用程序时,或者在删除并重新安装后,Messaging.messaging().shouldEstablishDirectChannel不会建立套接字连接。 如果我关闭应用程序,然后重新打开它,则套接字连接已建立。

重现步骤:1( 将此代码放在应用程序代表中: 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
  • 火力基地 4.1.0
  • FirebaseAnalytics 4.0.3
  • FirebaseCore 4.0.5
  • FirebaseInstanceID 2.0.1
  • FirebaseMessaging 2.0.1
  • Firebase 产品:消息传递

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.")
}
}

更新: 请在使用"通知中心"之前添加此项。

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()

最新更新