ios - Firebase Messaging shouldEstablishDirectChannel not establishing connection on first app launch -




problem

when launching app first time, or after deleting , re-installing, messaging.messaging().shouldestablishdirectchannel not establish socket connection. if shut down app, , re-open it, socket connection established.

steps reproduce:

1) place code in appdelegate: firebaseapp.configure() messaging.messaging().delegate = self messaging.messaging().shouldestablishdirectchannel = true

2) place code anywhere after check if connection established: messaging.messaging().isdirectchannelestablished returns false.

3) listen connection state change , observe notification never gets fired. notificationcenter.default.addobserver(self, selector: #selector(fcmconnectionstatechange), name: nsnotification.name.messagingconnectionstatechanged, object: nil)

that problem in nutshell. if kill app, , re-launch it, works expected. socket connection made , messagingconnectionstatechanged notification fired.

why messaging.messaging().shouldestablishdirectchannel not connecting on initial app launch?

relevant code
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() {        // never called on app's first launch!!!         print(messaging.messaging().isdirectchannelestablished)     } 

environment

  • xcode version: 8.3.3
  • firebase 4.1.0
  • firebaseanalytics 4.0.3
  • firebasecore 4.0.5
  • firebaseinstanceid 2.0.1
  • firebasemessaging 2.0.1
  • firebase product: messaging

fcm connection may have failed when attempted before having token.

i modified code, try this.

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 fcm since connection may have failed when attempted before having token.     connecttofcm() }  func connecttofcm() {     // won't connect since there no token     guard instanceid.instanceid().token() != nil else {         return;     }     messaging.messaging().shouldestablishdirectchannel = true }  func fcmconnectionstatechange() {     if messaging.messaging().isdirectchannelestablished {         print("connected fcm.")     } else {         print("disconnected fcm.")     } } 

update: add before using 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() 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -