How to stop an ongoing phone authentication request with Firebase - java

I have added to my phone authentication to my sign up process, in a send code activity - which sends the sms code to confirm the phone authentication process. Then, I have also added a "go-back"/"return" button which moves the user back to the main activity.
If I make the following request which sends the user a sms code to his phone:
PhoneAuthProvider.verifyPhoneNumber(options);
I won't be able to make another request before the defined timeout duration ends. Therefore, I thought about the easy and not messy approach, that would be to cancel the ongoing request, but unfortunately couldn't find how to do so, if even possible nowadays. I have also saw the unanswered post here: Android Firebase OTP auth: Is there a way to cancel OTP code request programatically before the actual timeout?
Couldn't work with this, even though it's what I am looking for, but it has no related answers.
Note: I am programming my project with Java and not Kotlin.
I have also thought about the second approach, which is to save current activity's phone number and then extract it with onRestoreInstanceState and onSaveInstanceState, then resend a code sms again. But of course, it's much more complicated and messier.

It is possible to cancel an ongoing verification request by calling the verifyPhoneNumber method again with the same phone number, but with the forceResendingToken parameter set to null. This will cancel the previous request and allow to start a new one.
It is also possible to use the PhoneAuthProvider.getInstance() method to get a reference to the PhoneAuthProvider instance, and then call the verifyPhoneNumber method on that instance instead of calling it directly. This allows to call the verifyPhoneNumber method multiple times without canceling the previous request.
Timeout duration for verification requests is typically around 5 minutes, so if you want to allow the user to request a new code before the timeout expires, provide a way for them to do so, such as by adding a "Resend code" button to the app.
Overall, it's best to design apps in a way that minimizes the need for canceling ongoing verification requests, as this can lead to a confusing user experience. Instead, focus on providing clear instructions and options for the user, and consider using the getInstance method to avoid having to cancel requests altogether.

Related

Cancel firebase upload if its not finished after some seconds

I upload an int to the firebase. If the upload is not finished after the first try, a short time (some seconds) it should cancel the upload.
Right now it is the case, that when you upload the value, but you have no internet connection it waits until connections works again and upload the value than. This can be after 30 minutes for example.
The problem is that this uploaded value is already outdated for my purpose.
I need to cancel the upload if it is not finished after the first try. How can I do that?
mDatabase = FirebaseDatabase.getInstance().getReference().child("Survey").child("Solution1").child("Result");
mDatabase.setValue(i1);
As Doug said, there is no way to cancel a specific write once it's been queued.
There are two ways I can think of to implement (or at least approximate) your use-case:
Detect whether the client is connected to the Firebase backend before queueing the write. You'd use an additional listener to the .info/connected node for that as shown in the documentation on detecting connection state. Just have a top-level listener to that, and then check what the latest value was before you call setValue().
You can purge all pending writes by calling FirebaseDatabase.getInstance().purgeOutstandingWrites(). So you could call that before calling setValue().
Alternatively: consider just letting the multiple writes pass through. The server will usually process them in quick succession, and may not even send the outdated ones out to clients.
You can't cancel a write to Realtime Database. What you can do instead is simply re-write the prior value into the same location. If you don't know the prior value, you're kind of stuck, and you have to accept that the value you wrote will be eventually synchronized.
You could perhaps try to undo the write later in a Cloud Functions trigger, but you will have to figure out at what point that value is out of date, using only information provided to the trigger.

Facebook Android SDK Does Not Allow Consecutive Posting To Facebook Wall?

I have been struggling with this issue for weeks now and I don't want to give up! Here is my problem:
Problem Definition
I have successfully logged in a Facebook user using Facebook SDK. This happens immediately after they choose a photo to share on their wall.
The first attempt to post the image works effortlessly and the user gets redirected to a different view once they are done posting to their wall.
Here is where things don't work:
When the user tries to login again to post another photo for instance, they are allowed to login but then the screen goes blank and nothing gets posted to their Facebook wall.
I don't know why it is acting like this and I cannot figure out where it dies exactly.
Things I have tried so far
I have, since I am making three async calls, created a timer (Thread) which checks to see if the calls have been completed, then calls:
Session.getActiveSession().closeAndClearTokenInformation();
This, does not solve the problem.
I have also tried using a counter variable that gets incremented each time an async task is completed and then I check to see if the number is 3 before clearing everything up. This still fails.
I am stuck and I really need your help. Sorry for the long question - I just thought I should give enough details.
Thanks,
E
I ended up solving this problem on my own and here is what I did:
Define a local variable called isPosted which is a boolean and set its initial state to false.
In the first async task, either do nothing with it inside onCompleted() method or set it to false again just in case something set it to true;
Repeat this until you reach the last async task - the one to be executed last.
In the last request/async, set it to true.
After calling the methods that execute those async tasks, check to see if the variable is true - in that case, clear the session and token information. If needed, clear the cookies as well. This will logout the user completely and create room room for others.
This enabled multiple users to login one after another and a single user can login consecutively without issues and post to their wall.

Android : Phone call detect when connected?

I can use this code to make outgoing call:
Intent dial = new Intent(Intent.ACTION_CALL);
dial.setData(Uri.parse("tel:5556") );
context.startActivity(dial);
But how to detect whether the call is picked up or the call is refused?
I tried PhoneStateListener, but it is not working.
Unfortunately, Android gives no mean to know when an outgoing call has been answered.
PhoneStateListener works fine but the 3 states notified by onCallStateChanged are not enough. An additional state like CALL_STATE_CONNECTED would be welcome.
There is an open issue requesting this feature but it didn't get much attention so far:
https://code.google.com/p/android/issues/detail?id=14266
Some people (like me) falls back using logcat and tries to infer if an outgoing call has been answered but this is far from an ideal solution.
I am also searching for an answer to the same problem, apparently no straight forward method is present to do this. But, I think we can combine a Call Log content observer with PhoneStateListener to get the call duration.
We could set a flag in the shared prefs when an outgoing call is started, if anything changes in call log and our shared prefs flag is true we could get the call duration from the call log to see if the call was ever connected :)
You can Check these Duplicate questions:
How can my android app detect a dropped call?
Detecting outgoing call and call hangup event in android

GWT: Multithreading

I'm facing this problem: after clicking on a button, I make a request to the server and get some data; then, I display the data on a new page/view. This raises a problem: the UI has to wait while the request is being made and data is being received, parsed and set on the view. This results in the user having to wait until all the data is loaded before even being able to go back, and doesn't even have the chance to cancel the call. Multithreading would solve the issue, and that's where I need help.
The HTML5 Web Workers would do the trick for me, however I don't want to "hard code" them in JSNI and have all the calls written with Javascript instead of GWT Java (RequestBuilder). I've read about DeferredCommand but I also don't think it's the answer to my issue.
Any suggestions? Or this is an impossible optimization, at the moment?
In JS, therefore GWT, there is no multithreading. Instead you should use asynchronous calls with callbacks. Normally when you use GWT RPC for communication, you issue a request and handle result in onSuccess event. Alternatively you can always use Timer to check for result periodically. I'm not sure what kind of request you are making, so hard to be specific. Probably you should check appropriate section of Communicating with the server
EDIT: I've just noticed you mention RequestBuilder. The sendRequest() should not block execution and you should process result in RequestCallback.onResponseReceived() of provided callback. Which mean you somehow continue your button event handling in that callback.

If a REST web service call fails, should a message or event queue be used to retry later?

I'm building a web service with a RESTful interface (lets call it MY_API). This service relies on another RESTful webservice to handle certain aspects (calling it OTHER_API). I'd like to determine what types of best practices I should consider using to handle failures of OTHER_API.
Scenario
My UI is a single page javascript application. There are some fairly complex actions a user can take, which can easily take the user a minute or two to complete. When they are done, they click the SAVE button and MY_API is called to save the data.
MY_API has everything it needs to persist the information submitted by the user. However, there is an action that must take place that is handled by OTHER_API. For instance, OTHER_API might handle sending out an emails. Or perhaps it handles adding line items to my user's billing statement. In both cases, these are critical things than must be completed, but they don't have to happen right now, they just need to happen eventually.
If OTHER_API fails, I don't want to simply tell the user their action has failed, as they spent a lot of time doing it and this will make the experience less than optimal.
Questions
So should I create some sort of Message or Event Queue that can save these failed REST requests to OTHER_API and process them later?
Any advice or suggestions on techniques to go about saving REST requests for delayed processing?
Is there a recommended open source message queue solution that would work for this type of scenario with JSON-based REST web services? Java is preferred as my backend is written in it.
Are there other techniques I should consider?
Rather than approach this by focusing on the failure state, it'd be faster and more robust to recognize that these actions should be performed asynchronously and out-of-band from the request by the UI. You should indeed use a message/event/job queue, and just pop those jobs right onto that queue as quickly as possible, and respond to the original request as quickly as possible. Once you've done that, the asynchronous job can be performed independently of the original request, and at its own pace — including with retries as needed.
If you want your API to indicate that there are aspects of the request which have not completed, you can use the HTTP response Status Code 202 (Accepted).

Categories

Resources