cybex-dev / twilio_voice

Flutter Twilio Voice Plugin
https://twilio-voice-web.web.app/
MIT License
39 stars 80 forks source link

check if the accessToken #53

Closed victor-padovam closed 1 year ago

victor-padovam commented 2 years ago

Good morning,

I would like to know if the package offers any function when pressing the call button. check if the accessToken is expired?

in the function below how do I pass my new changed token? I tried to pass it as a parameter but it doesn't accept.

String newTokenChange = apinewToken;
 TwilioVoice.instance.setOnDeviceTokenChanged((newTokenChange) {
  print("voip-device token changed");
});
victor-padovam commented 2 years ago

@cybex-dev any solution for me help ?

diegogarciar commented 2 years ago

You must log the time you obtained the token and do the calculation to know if it has expired.

For the example you mention, you can use the same funciton you used at startup to get your token from the backend and register it.

victor-padovam commented 2 years ago

As I pass my new changed token to this function, I find I can't pass it as a parameter. how does it work and how does it expect

cybex-dev commented 2 years ago

As I pass my new changed token to this function, I find I can't pass it as a parameter. how does it work and how does it expect

See this from the example provided.

https://github.com/diegogarciar/twilio_voice/blob/master/example/lib/main.dart#L42

cybex-dev commented 2 years ago

@VictorPadovan1997 did the documentation & example provide a solution?

victor-padovam commented 2 years ago

@cybex-dev you can forward me your e-mail to ask a question

cybex-dev commented 2 years ago

@VictorPadovan1997 please ask here as it may help others

victor-padovam commented 2 years ago

@VictorPadovan1997 did the documentation & example provide a solution? @cybex-dev now that I've updated to the updates it got in the package. I'm managing to pass my token as a parameter, but it never enters the print() function and register().

 if (updateToken != currentToken) {
    TwilioVoice.instance.setOnDeviceTokenChanged((updateToken) {
      print("voip-device token changed");
      print("token Update is : $updateToken");
       register(userId);
    });                 
}

so passing only as a parameter and not print them like this: TwilioVoice.instance.setOnDeviceTokenChanged((updateToken));

returns this: _TypeError (type 'String' is not a subtype of type '(String) => dynamic')

cybex-dev commented 2 years ago

@VictorPadovan1997 did the documentation & example provide a solution? @cybex-dev now that I've updated to the updates it got in the package. I'm managing to pass my token as a parameter, but it never enters the print() function and register().

 if (updateToken != currentToken) {
    TwilioVoice.instance.setOnDeviceTokenChanged((updateToken) {
      print("voip-device token changed");
      print("token Update is : $updateToken");
       register(userId);
    });                 
}

so passing only as a parameter and not print them like this: TwilioVoice.instance.setOnDeviceTokenChanged((updateToken));

returns this: _TypeError (type 'String' is not a subtype of type '(String) => dynamic')

The reason is setOnDeviceTokenChanged requires a function pointer or consumer named OnDeviceTokenChanged

See implementation [here](https://github.com/diegogarciar/twilio_voice/blob/master/lib/twilio_voice.dart#L38)

  OnDeviceTokenChanged? deviceTokenChanged;
  void setOnDeviceTokenChanged(OnDeviceTokenChanged deviceTokenChanged) {
    deviceTokenChanged = deviceTokenChanged;
  }

where the function OnDeviceTokenChanged is defined as

typedef OnDeviceTokenChanged = Function(String token);

i.e. a void function that requires a single String parameter (a consumer).

For your example, you need to do something like

OnDeviceTokenChanged updateToken = (String token) {
    print("Token changed to ${token}");
}; 
TwilioVoice.instance.setOnDeviceTokenChanged(updateToken);
victor-padovam commented 2 years ago

@cybex-dev I just couldn't understand here how I pass my Changed token inside this function you passed.

this is my function and the updated token is coming as a parameter in registerAndUpdateToken. Where I specify the tokenChanged within this method OnDeviceTokenChanged ?

registerAndUpdateToken(String tokenChanged) {
    OnDeviceTokenChanged updateToken = (String token) {
      print(token);
    };
    TwilioVoice.instance.setOnDeviceTokenChanged(updateToken);
  }

)

cybex-dev commented 1 year ago

@VictorPadovan1997 I'm certain you've resolved this issue by now, however I'll be answering it for the record.

The onDeviceTokenChanged only applies to the iOS implementation (see SwiftTwilioVoicePlugin.swift#L349 and SwiftTwilioVoicePlugin.swift#L578 as reference).

This onDeviceTokenChanged event is triggered when the iOS device receives a new Push Token from the APNS which is then used by Twilio to register for push notifications.