Skip to content

shouldEstablishDirectChannel not establishing connection on first app launch #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alanscarpa opened this issue Aug 22, 2017 · 11 comments
Closed

Comments

@alanscarpa
Copy link

alanscarpa commented Aug 22, 2017

Describe your 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

Describe the problem

When launching an app for the first time, or after deleting and re-installing, Messaging.messaging().shouldEstablishDirectChannel does not establish the socket connection. If I shut down the app, and re-open it, then the socket connection is established.

Steps to reproduce:

  1. Place this code in AppDelegate:
FirebaseApp.configure()
Messaging.messaging().delegate = self
Messaging.messaging().shouldEstablishDirectChannel = true
  1. Place this code anywhere after to check if connection is established:
    Messaging.messaging().isDirectChannelEstablished
    This always returns false.
  2. Listen for connection state change and observe that this notification never gets fired.
NotificationCenter.default.addObserver(self, selector:
            #selector(fcmConnectionStateChange), name:
            NSNotification.Name.MessagingConnectionStateChanged, object: nil)

That is the problem in a nutshell. If I simply kill the app, and re-launch it, everything works as expected. The socket connection is made and the MessagingConnectionStateChanged notification is fired.

Why is Messaging.messaging().shouldEstablishDirectChannel not connecting on my 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() {
       // This is never called on app's first launch!!!
        print(Messaging.messaging().isDirectChannelEstablished)
    }

@PaulRBerg
Copy link

+1

@alanscarpa
Copy link
Author

Anyone have a hunch as to why this might be occurring? Are there any steps that should definitely be taken before setting shouldEstablishDirectChannel = true?

@alanscarpa
Copy link
Author

@rsattar Any word on this or if you guys will have a chance to take a look at this?

@rsattar
Copy link
Contributor

rsattar commented Sep 14, 2017

Hmm, what you're doing makes sense, and that's what I'm doing too, but I'm connection on first launch also:

FirebaseApp.configure()
Messaging.messaging().delegate = self
Messaging.messaging().shouldEstablishDirectChannel = true
listenForDirectChannelStateChanges();

Where I've got:

extension AppDelegate {
  func listenForDirectChannelStateChanges() {
    NotificationCenter.default.addObserver(self, selector: #selector(onMessagingDirectChannelStateChanged(_:)), name: .MessagingConnectionStateChanged, object: nil)
  }

  func onMessagingDirectChannelStateChanged(_ notification: Notification) {
    print("Is FCM Direct Channel Established: \(Messaging.messaging().isDirectChannelEstablished)")
  }
}

And I'm being called on the first launch:

Is FCM Direct Channel Established: true

I'm also breakpointing in FIRMessagingClient's connect method, and it is getting called as well.

As a quick check, can you try explicitly setting your FirebaseInstanceID pod to point to 2.0.0? There is a startup regression in 2.0.1 that could be related to first launch experiences.

@rsattar
Copy link
Contributor

rsattar commented Sep 14, 2017

Okay, I've managed to reproduce the issue, and it is related to the bug in FirebaseInstanceID 2.0.2. A fix for this has already been made and should be getting released in the next release of Firebase.

For now, a quick workaround is to simply add:

application.registerForRemoteNotifications()

to your App Delegate.

NOTE: This is safe to call on app startup, as it does not show the user notifications permissions dialog to the user. The dialog is only shown when you call UNUserNotificationCenter.current().requestAuthorization(...) or application.registerUserNotificationSettings(...).

@alanscarpa
Copy link
Author

Setting FirebaseInstanceID to 2.0 fixed the issue. Thank you! I'll use this for now.

application.registerForRemoteNotifications() was already being called in one of my functions within app delegate (I just think I didn't include in my code snippet). I moved it to the very top of AppDelegate, still did not work.

What is the best way to stay up to date on Firebase releases so I can update when this issue is fixed in next release? Thanks for the help @rsattar !

@rsattar
Copy link
Contributor

rsattar commented Sep 14, 2017

Hmm, that is strange that it didn't fix the issue when you added registerForRemoteNotifications() at the top. That should have definitely fixed it, if you were able to get the APNs token.

We have a release notes page that shows the current releases and all the fixes/updates in them. Hope that helps!

@alanscarpa
Copy link
Author

It actually worked when I "Allow" pushes. But if I deny pushes - I do not get the FCM connection on first launch. Though, in 2.0.0, if I deny pushes, my FCM connection does work on initial launch - which is what I was looking for.

@rsattar
Copy link
Contributor

rsattar commented Sep 18, 2017

Hmm, thanks for the extra info @alanscarpa. I think I've found another issue which might be causing this!

@rsattar
Copy link
Contributor

rsattar commented Sep 19, 2017

FYI, I've added a bug fix in #290. Unfortunately it's half of the fix, and the other half is in the Instance ID SDK. These should be available in the release after next, as I missed the window for the next launch.

@paulb777
Copy link
Member

paulb777 commented Oct 3, 2017

Fix released today in Firebase 4.3.0, FirebaseMessaging 2.0.4, and FirebaseInstanceID 2.0.4

@paulb777 paulb777 closed this as completed Oct 3, 2017
@firebase firebase locked and limited conversation to collaborators Nov 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants