pusher / push-notifications-android

Android SDK for Pusher Beams
https://www.pusher.com/beams
MIT License
21 stars 21 forks source link

Android Kotlin error when app come back from background #109

Closed AnD010 closed 3 years ago

AnD010 commented 4 years ago

Hi...I have a ugly error when app come back from background.... When I detected that the app is going to background I do this

 override fun closePrincipalChannelConnection() {
        Log.d(TAG,"closePrincipalChannelConnection")
        channel?.unbind("chat-message") {
            Log.d(TAG,"onEvent")
        }
        channel?.unbind("new-follower") {
            Log.d(TAG,"onEvent")
        }
       pusher?.unsubscribe(channelName)
    }

Then when 5-10 pass and the app come back from background, I mean, now the app is in foreground, then call

  override fun connectToGeneral(){
            channelName = "presence-$profileId"
            channel= pusher?.subscribePresence(channelName, object : PresenceChannelEventListener {
                override fun onAuthenticationFailure(message: String, e: Exception) {
                    Log.d(TAG,String.format("Authentication failure due to [%s], exception was [%s]", message, e))
                }

                override fun onSubscriptionSucceeded(channelName: String?) {
                    Log.d(TAG,"onSubscriptionSucceeded$channelName")

                }

                override fun onEvent(event: PusherEvent?) {
                    Log.d(TAG,"onEvent"+event?.data)
                }

                override fun onUsersInformationReceived(channelName: String?, users: MutableSet<User>?) {
                    Log.d(TAG, "onUsersInformationReceived$channelName${users.toString()}")
                }

                override fun userUnsubscribed(channelName: String?, user: User?) {
                    Log.d(TAG, "userUnsubscribed$channelName${user.toString()}")

                }

                override fun userSubscribed(channelName: String?, user: User?) {
                    Log.d(TAG, "userSubscribed$channelName${user.toString()}")
                }
            })

            channel?.bind("chat-message", object: PresenceChannelEventListener{
                override fun onAuthenticationFailure(message: String?, e: java.lang.Exception?) {
                    Log.d(TAG,String.format("Authentication failure due to [%s], exception was [%s]", message, e))

                }

                override fun onUsersInformationReceived(channelName: String?, users: MutableSet<User>?) {
                    Log.d(TAG, "onUsersInformationReceived$channelName${users.toString()}")
                }

                override fun userUnsubscribed(channelName: String?, user: User?) {
                    Log.d(TAG, "userUnsubscribed$channelName${user.toString()}")

                }

                override fun userSubscribed(channelName: String?, user: User?) {
                    Log.d(TAG, "userSubscribed$channelName${user.toString()}")
                }

                override fun onEvent(event: PusherEvent?) {
                    Log.d(TAG,"onEvent"+event?.data)

                    logicTypeMessage(PusherEvents.EVENT_TYPE_MESSAGE,event?.data ?:"")
                }

                override fun onSubscriptionSucceeded(channelName: String?) {
                    Log.d(TAG,"onSubscriptionSucceeded$channelName")
                }
            })

            channel?.bind("new-follower", object: PresenceChannelEventListener{
                override fun onAuthenticationFailure(message: String?, e: java.lang.Exception?) {
                    Log.d(TAG,String.format("Authentication failure due to [%s], exception was [%s]", message, e))

                }

                override fun onUsersInformationReceived(channelName: String?, users: MutableSet<User>?) {
                    Log.d(TAG, "onUsersInformationReceived$channelName${users.toString()}")
                }

                override fun userUnsubscribed(channelName: String?, user: User?) {
                    Log.d(TAG, "userUnsubscribed$channelName${user.toString()}")

                }

                override fun userSubscribed(channelName: String?, user: User?) {
                    Log.d(TAG, "userSubscribed$channelName${user.toString()}")
                }

                override fun onEvent(event: PusherEvent?) {
                    Log.d(TAG,"onEvent"+event?.data)

                    logicTypeMessage(PusherEvents.EVENT_TYPE_CONNECTIONS,event?.data ?:"")

                }

                override fun onSubscriptionSucceeded(channelName: String?) {
                    Log.d(TAG,"onSubscriptionSucceeded$channelName")
                }
            })

    }

The app crash whit the following message

Fatal Exception: java.lang.IllegalArgumentException
Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter code
com.xxx.repository.pusher.PusherRepositoryImp$connect$1.onError (Origen desconocido:7)
com.pusher.client.connection.websocket.WebSocketConnection$5.run (WebSocketConnection.java:238)
com.pusher.client.util.Factory$1.run (Factory.java:119)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1162)
java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:636)
java.lang.Thread.run (Thread.java:764)

whats wrong in my code?? thanks

daniellevass commented 3 years ago

Hi,

This doesn't look like a beams issue but more rather an issue with the channels sdk.

It doesn't look like you're binding with the correct listener.

Either you can have one PresenceChannelEventListener and provide a list of the event names you're interested in as per https://github.com/pusher/pusher-websocket-java/blob/master/src/main/java/com/pusher/client/Pusher.java#L345

e.g.

channel = pusher.subscribePresence(channelName, presenceChannelEventListener, "event-two", "event-one");

Or, you can bind and listen for just the event info with a SubscriptionEventListener e.g.

channel.bind("my-event", new SubscriptionEventListener() {
    @Override
    public void onEvent(PusherEvent event) {
        // todo
    }
});

Additionally, you need only call unsubscribe on the channel - you do not need to unbind from the events first - calling unsubscribe will stop any events you're listening to come through too.

Does that make sense?

benw-pusher commented 3 years ago

We haven't had a response here so I will close this out - comment with the requested information if you need this to remain open.