Skip to content
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

Android Kotlin Pusher error Start method must be called before setUserId pusher #265

Open
AnD010 opened this issue Jun 1, 2020 · 2 comments
Labels

Comments

@AnD010
Copy link

AnD010 commented Jun 1, 2020

version pusher = "2.2.1"
version pusherNotifications = "1.6.2"
Hi,
I don´t understand this error... It only happens when I close session and call to

  PushNotifications.close()
  PushNotifications.clearAllState()

Then when go to my first Activity the app crash with the error
Pusher error Start method must be called before setUserId pusher.
But as you can see in my below code I call PushNotifications.start() first.
When the apps crash and open again, all works fine
My code
onCreate(savedInstanceState: Bundle?) {initPusher() }
initPusher is this code

        GlobalScope.launch(dispatchers.main) {
            PushNotifications.start(applicationContext, "xxxxxxx-xxx-xxxxx-xx-xxxxxx")
            viewModel.loadInfoPusherSubscribe {
                val userId = it.first
                val token = it.second
                val realToken = "Bearer "+ token
                val realURL = "https://xxxxxxxxxxx?user_id=$userId"
                val tokenProvider = BeamsTokenProvider(
                    realURL,
                    object : AuthDataGetter {
                        override fun getAuthData(): AuthData {
                            return AuthData(
                                // Headers and URL query params your auth endpoint needs to
                                // request a Beams Token for a given user
                                headers = hashMapOf( Pair("Authorization", realToken),
                                                     Pair("accept", "text/plain"),
                                                     Pair("Content-Type", "application/json-patch+json")),
                                queryParams = hashMapOf()
                            )
                        }
                    }
                )

                PushNotifications.setUserId(userId, tokenProvider,
                    object : BeamsCallback<Void, PusherCallbackError> {
                        override fun onFailure(error: PusherCallbackError) {
                            Log.e("BeamsAuth", "Could not login to Beams: ${error.message}");
                        }

                        override fun onSuccess(vararg values: Void) {
                            Log.i("BeamsAuth", "Beams login success");
                        }
                    }
                )
            }
        }
My viewModel
 suspend fun loadInfoPusherSubscribe(listener: InfoPusherListener)=
       viewModelScope.launch(dispatchers.io) {
           val profileId = async { profileDao.getProfileUser().id }
           val token =  async { session.getSessionUser().accessToken }
           withContext(dispatchers.main){
               listener.invoke(Pair(profileId.await(),token.await()))

           }

    }

What is the problem?
PushNotifications.start() is asynchronous method???

@haidarzamzam
Copy link

Halo @AnD010

I just got the same error, and I managed to solve the error by stopping everything from the pusher.

So the code to run before re-running PushNotifications.start() and setUserId() is:

PushNotifications.stop()
PushNotifications.clearAllState()

I hope this helps, maybe someone else is experiencing the same error. Thank you.

@mohkoma
Copy link

mohkoma commented Jan 20, 2024

I think the issue is that the documentation doesn't explain well what these methods are intend for.
But in short, Using stop before clearAllState doesn't do anything in reality, Just because clearAllState is just calling stop and then start (This is from source code).

fun clearAllState() {
    stop()
    start()
  }

Best way to avoid the problem is just by calling stop when the user sign out and maybe optionally call clearAllState in the start to prevent calling the methods twice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants