jefflinwood / twilio_client_phonegap

Phonegap plugins for the Twilio Client iOS and Android SDKs
MIT License
59 stars 53 forks source link

Releasing device not working properly #27

Open tombuyse opened 9 years ago

tombuyse commented 9 years ago

Hello,

I seem to have some problems releasing the device. I'm using following code:

Twilio.Device.setup("");

But when I setup a new device and I receive a call the incoming event is still called twice.

Anyone knows how to solve this issue?

jefflinwood commented 9 years ago

Hi,

Is the problem that your incoming function is called twice? Also, is the problem on Android or iOS?

Thanks! Jeff

tombuyse commented 9 years ago

Yes the incoming function is called twice. The problem occurs on Android, I have not yet tested it on iOS.

tombuyse commented 9 years ago

The weird thing is that the initial release works. After I have called the Twilio.Device.setup(""), no calls are being received. But when I call that same function again with a valid token, the incoming function is called twice

jefflinwood commented 9 years ago

Do you have some example code to look at? Thanks!

tombuyse commented 9 years ago

Ofcourse, I don't know if it's relevant but I'm using your plugin in an Ionic project (Ionic 1.0.0 rc4 and AngularJs 1.3).

This is the code I'm using for the twilio setup:

    TwilioService.setupTwilio = function() {
        $http.post($rootScope.baseUrl + '/twilio/token', account).success(function(token) {
            Twilio.Device.setup(token.token);

            Twilio.Device.ready(function() {
               $log.info('device is now ready');
            });

            Twilio.Device.error(function(e) {
                $log.error('There has occured an error while setting the twilio token ' + e.message);
            });

            Twilio.Device.offline(function() {
                $log.info('now offline');
            });

            Twilio.Connection.setSpeaker("on");

            Twilio.Device.incoming(function(conn) {
                conn.parameters(function(params) {
                    var from = $filter('NumberFilter')(params.From);
                    $rootScope.$broadcast('incoming-call', { from: from, conn: conn, callSid: params.CallSid});
                });
            });
        }).error(function(err) {
            $log.warn('unable to retrieve twilio token ' + err);
        });
    };

Then I'm running following code with help of the cordova background plugin to make sure when the Twilio token expires, a new token is retrieved and set.

 cordova.plugins.backgroundMode.onactivate = function() {
                $interval.cancel(interval);
                interval = $interval(function() {
                    Twilio.Device.setup("");
                    TwilioService.setupTwilio();
                }, 3600000);
          };
jefflinwood commented 9 years ago

Can you try replacing TwilioService.setupTwilio(); with a call that just refreshes the token? Something like: TwilioService.refreshTwilio = function() { $http.post($rootScope.baseUrl + '/twilio/token', account).success(function(token) { Twilio.Device.setup(token.token); }); };

Apologies for any typos in the above code!

tombuyse commented 9 years ago

I have tried your proposed solution, but unfortunately the incoming function is still called twice.

tombuyse commented 9 years ago

I have tested this problem in a newly made project with only the basic Twilio handlers and the problem still occurs.

Any update?

tombuyse commented 9 years ago

I seem to have fixed this issue by changing this code in the plugin

  if (arguments.optString(0).equals("")) {
            Log.d("TCPlugin","Releasing device");
            cordova.getThreadPool().execute(new Runnable(){
                public void run() {
                    mDevice.release();
                }
            });
            javascriptCallback("onoffline", callbackContext);
            return;
        }

into the following code

 if (arguments.optString(0).equals("")) {
            Log.d("TCPlugin","Releasing device");
            LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(cordova
                    .getActivity());
            lbm.unregisterReceiver(mBroadcastReceiver);
            cordova.getThreadPool().execute(new Runnable(){
                public void run() {
                    mDevice.release();
                }
            });
            javascriptCallback("onoffline", callbackContext);
            return;
        }

Unregistering the receiver solves the issue for me, the incoming function is now only called once when a new token is set.

jefflinwood commented 9 years ago

Thank you for tracking this down - I'm going to get this fix into the main code base.